chrispooledotcom

How to Backup Your Pinboard Bookmarks

01 June 2011

Reader beware: this article is a bit old!

I’ve been using Pinboard for several months now. It allows me view my bookmarks on different devices, and it grabs articles and links from my Instapaper and Twitter accounts.

If the bookmarks stored by Pinboard are important to you though, you shouldn’t trust Pinboard to back them up for you. This is a simple bash script to backup your Pinboard bookmarks to your local machine.

Assuming you have curl installed on your unix-like system, add the following to a file in your PATH (~/bin is the usual location), and change the username, password and backup directory values:

#!/bin/bash -

cookies=cookies.txt
username=johnsmith
password=hunter2
backup_dir="$HOME/"

printf "Backing up Pinboard bookmarks... "

curl -s -d "username=$username&password=$password" \
    'https://pinboard.in/auth/' -c $cookies > /dev/null 2>&1

curl -s -L -b $cookies -c $cookies \
    'https://pinboard.in/export/format:html/' | bzip2 -c > \
    "$backup_dir"/pinboard-bookmarks-`date "+%Y%m%d%H%M"`.htm.bz2

if [[ -f $cookies ]]; then
    rm $cookies;
fi

# Remove Pinboard bookmark backup files older than 14 days
find "$backup_dir" -name 'pinboard-bookmarks-*.htm.bz2' -type f -mtime \
    +14 -maxdepth 1 -print0 | xargs -0I{} rm {}

printf "Done.\n"

What it does

Firstly, curl visits the login page, entering your username and password, and grabbing the token that Pinboard returns, keeping it in the cookie jar.

Then curl sends a GET request to the page holding your bookmarks for export. I’ve chosen the html format, a legacy Netscape format that’s understood by lots of other services, but you could change this to json or xml. Before saving the bookmarks into a file (here, in my backup directory, but change this to suit your taste), it’s passed through bzip2 to compress the information to save space.

Finally, old backup files are removed.