Tuesday, March 20, 2007

Quick Stikkit Backup

Now that I'm starting to get a lot of information into Stikkits, I wanted an easy way to back them up. What I've come up with so far is a Ruby script that utilizes the Stikkit API to backup each of my stikkits to individual text files. I end up with with a big directory of grepable text files that I can use if stikkit.com happens to be unavailable. cron is my friend and runs the script for me each night.

Here's the script, really simple and just a few lines longer than the example on the Stikkit API page (remember to put your API key in there):
#!/usr/local/bin/ruby

require 'net/http'

backup_directory = "/server/homes/alm/stikkits/"

page_number = 1
continue = true

while continue == true
response = Net::HTTP.start('api.stikkit.com') { |http|
req = Net::HTTP::Get.new('/stikkits.txt?api_key=xxxxxxxxxxxxxxxxxxx&page=' + page_number.to_s)
http.request(req)
}

page_number += 1

response.body.split("nf").each do |stikkit|

stikkit_file = File.open(backup_directory + stikkit.split("n")[0].gsub(/W/, '') + ".txt","w+")
stikkit_file << stikkit
stikkit_file.close
end

continue = false if response.body == ""
end

I know that there are probably a million better ways to do this, but I just like having them all backed up as text files. It just works for me.

0 comments:

Post a Comment