rsync backup

Wed, 30 May 2007 20:01:45 +0000
tech rsync article

Running with the explain things for Benno 5 months from now, here is how I'm currently making my backups work. The basic thing I want is incremental backups onto an external hard drive.

rsync makes this really easy to do. The latest version of rsync has --link-dest, which will hardlink to files in an existing directory tree, rather than creating a new file. This makes each incremental reasonably cheap, as most files are just hard links back to the previous backup.

So basically I do: sudo rsync -a --link-dest=../backups.0/ /Users/ backups.1/. Note that the trailing slash is actually important here.

A big gotcha is ensuring that the external disk is actually mounted with permissions enabled, or else the hard-links won't actual be made.

After this everything works nicely, when combined with a simple script:

#!/usr/bin/env python
"""
Use rsync to backup my /Users directory onto external hard drive
backup.
"""
import os

def get_latest():
    backups = [int(x.split(".")[-1]) for x in os.listdir(".") if x.startswith("backup")]
    backups.sort()
    return backups[-1]

os.chdir("/Volumes/backup2007")
latest = get_latest()
next = latest + 1
command = "sudo rsync -a --link-dest=../backups.%d/ /Users/ backups.%d/" % (latest, next)
os.system(command)

Currently I don't do any encryption, which might be useful in the future.