Linux remote incremental backup

Backup is something that we should all be doing all the time. Sometimes it’s simple, other times it’s not. I’m going to deal with one of those times it seems a lot harder than it is: a remote incremental backup. Let’s say you’ve got a server with a website that other people are editing and it’s not got any version control, for whatever reason. Let’s also say that you need to back it up and provide the ability to roll back a day or two, or even to any point in its history. This seems like the sort of thing you buy an off-the-shelf product for when it’s something you can do with one simple shell script. All you need is a Subversion repository and the usual tools one finds lying about on a Linux system. I’m going to have to assume a working knowledge of Subversion for this, but if you are not familiar with the workings, there are plenty of simple tutorials on the internet that will get you up and running.

#!/bin/bash
# Jared Earle, 2011-08-14, 23x.net Ltd

# Set the datestamp
export date=$(date +\%Y\%m\%d)

# Server name and paths - change these to your values
export server="host.tld"
export remotepath="/path/to/remote/files"
export localpath="/path/to/local/files"
export templog="/tmp/subscript${date}.log"

# Fetch the files and push them to subversion
cd ${localpath}
echo "Backup dated ${date}
----" > ${templog}
/usr/bin/rsync -avz -e ssh ${server}:${remotepath}/ ./ >> ${templog} 2>&1
/usr/bin/svn add --force .
/usr/bin/svn commit -F ${templog} .
rm ${templog}

You need to have SSH keys set up on your local system and on your remote system so as not to require passwords every time you run the script. You also need to check out the empty repository to the directory specified in $localpath in the script and get it to store the subversion password locally.

After it’s set up, you just put this script somewhere convenient and add it to crontab to run nightly. Yes, I realise this seems far too simple, but that’s the point. It works on Linux, BSD and OSX, as far as I can tell. Any comments, questions or improvements, feel free to comment below.

Jared Earle is a writer, photographer and systems administrator. You can find him on Twitter most of the time.