Jamie's Blog

Ruby developer. CTO. Swimmer. Always trying to write more

How to backup a Linux server with Tarsnap

I picked up a dedicated server just before Christmas from OVH. It’s a nice little ‘Kimsufi’ 4GB Atom server which is more than enough for a couple of Wordpress blogs and some personal projects. As it’s a bare-bones dedicated server I really want a decent backup service to make sure I don’t lose 11years of blog posts. The ideal solution is to store those backups in a cloud service like Amazon.

I asked for suggestions twitter and Conor suggested Duplicity. It looks like a good solution but it did mean I’d have to remember my AWS account details (I think I have three account, with two different email addresses). Then Simon suggested Tarsnap, which I had bookmarked months ago but I wasn’t sure how complicate the “paranoid security” would make it. Actually, Tarsnap is much simpler because it uses Amazon S3 in the backend but it doesn’t require you to manage it.


Install Tarsnap

I install and use Tarsnap as root so that it has access to all files on the server

wget https://www.tarsnap.com/download/tarsnap-autoconf-1.0.33.tgz
apt-get install libssl-dev zlib1g-dev e2fslibs-dev
tar xzvf tarsnap-autoconf-1.0.33.tgz 
cd tarsnap-autoconf-1.0.33/
make all install clean

Configure Tarsnap

tarsnap-keygen --keyfile /root/tarsnap.key --user jamie@ideasasylum.com --machine ovh
mv /usr/local/etc/tarsnap.conf.sample /usr/local/etc/tarsnap.conf
vi /usr/local/etc/tarsnap.conf  

You don’t actually need to change any options in tarsnap.conf but double-check it anyway

Create a backup

tarsnap -c -f my_backup_name my_important_directory

List the backups

tarsnap --list-archives

View the files in a backup

tarsnap -t -f my_backup_name

Automating the Backups

Well, that’s Tarsnap setup and working but I really want something automated. Enter this delicious little script which manages the Tarsnap backup process for you. Install the script as root:

Install Tarsnap Generations

wget https://raw.github.com/Gestas/Tarsnap-generations/master/tarsnap-generations.sh
chmod ug+x tarsnap-generations.sh     

Configure it

Create a file with newline-seperated list of the folder you wish to backup (for me, that’s /srv/www and /etc/nginx)

vi /root/tarsnap.folders

Now we need to setup cron

crontab -e

and add the line

30 23 * * * /root/tarsnap-generations.sh -f /root/tarsnap.folders -h 1 -d 10 -w 4 -m 12

This will run the backup process at 2330hrs every day, keep 10 daily backups, 4 weekly backups and 12 monthly backups.