How to easily back up your delicious bookmarks

Jason Scott:

Don’t trust the Cloud to safekeep this stuff. Hell yeah, use the Cloud, blow whatever you want into the Cloud. The Internet’s a big copy machine, as they say. Blow copies into the Cloud. But please:

  • Don’t blow anything into the Cloud that you don’t have a personal copy of.
  • Insult, berate and make fun of any company that offers you something like a “sharing” site that makes you push stuff in that you can’t make copies out of or which you can’t export stuff out of. They will burble about technology issues. They are fucking lying. They might go off further about business models. They are fucking stupid. Make fun of these people, and their shitty little Cloud Cities running on low-grade cooking fat and dreams. They will die and they will take your stuff into the hole. Don’t let them.
  • Recognize a Cloud when you see it. Are you paying for these services? No? You are a sucker. You are giving people stuff for free. I pay for Vimeo and I pay for Flickr and a couple other things. This makes me a customer. Neither of these places get my only copy of anything.

In that spirit, I’m going to demonstrate two ways to export your delicious bookmarks into an XML file for safety. As noted previously, this is really a pretty simple thing to do —  you just need to call the URL api.del.icio.us/v1/posts/all and pass in basic auth. For more information on this and everything else in the delicious API, please refer to the documentation.

cURL

cURL is definitely the easiest way to do this. Many web APIs will readily interact with cURL, and delicious’ is especially simple due to the use of basic auth (meaning there’s no need to do a login dance) and the usage of URLs rather than arguments.

curl --user 'username':'password' -o ~/Desktop/delicious.xml 'https://api.del.icio.us/v1/posts/all'

We can turn this into a simple little Bash script:

#!/bin/bash
user='username'
password='password'

curl --user $user:$password -o ~/Desktop/delicious.xml 'https://api.del.icio.us/v1/posts/all'

Ruby

The Ruby form is a little more verbose, but still quite simple. Please note that the following code has largely been borrowed from the nice example here, which unfortunately does not specify a license.

#!/usr/bin/ruby

require 'net/http'
require 'net/https'

user = 'user'
password = 'password'

http = Net::HTTP.new('api.del.icio.us', 443)
http.use_ssl = true
xml = http.start { |http|
  req = Net::HTTP::Get.new('/v1/posts/all', {'User-Agent' => 'Delicious backup'})
  req.basic_auth(user, password)
  http.request(req).body
}

dumpfile = File.new('delicious.xml', 'w')
dumpfile.puts xml
dumpfile.close

Automated backups

Now that you’ve got a script to make an XML backup, why not have it automatically run so you don’t have to remember?

Here’s my crontab, which automatically runs the backup every day at noon:

00 12 * * * /Users/stilist/deliciousbackup.sh

Where deliciousbackup.sh is the Bash script noted above.

Ideas for further development

  1. These examples download the entire bookmark collection regardless of whether anything has changed. Checking /posts/update could save on bandwidth.
  2. It’s sort of assumed that the service will always be around, and the API call will always succeed. Error handling would be useful.
  3. Only a single day’s archive is kept. This means that if something goes wrong with the online copy — accidentally deleting tags, account vandalism, or anything like that — you’ll have a maximum of twenty-four hours to fix it before the backup is overwritten.

Finally

The code here is provided under the MIT license. For a copy, drop by the git repository.

And if you code up any of the improvements noted in the previous section, I’d love to know!

written 21 January, 02009 Comments