With hard disks getting larger by the month, keeping all your data backed up can be tricky. Raid will give you reliability, but no possibility of taking your data off-site, and tape drives are expensive.
I found myself needing to keep backups of an 80GB raid array for a small office server, and recalled an article I had read a few months ago in Linux Journal. The article details how you can keep snapshot backups of thumb drives on your harddisk... and that made me think doing the reverse should be workable too.
It was workable, and I've since also set up automatic dumps of my home directory to a USB powered external harddisk. Here is what I did.
I started by writing a udev rules file. This rules file is where the magic happens. As you plug in hardware, udev processes all rules from /etc/udev/rules.d in order. It sets up device nodes, fixes permissions etc.
Mine is 96-backup.rules
KERNEL=="sd?1", NAME="%k", \
I've specified that if a USB device with serial number 2125000000006045 is plugged in, and detected as a scsi disk, a symbolic link called travelstar should be made to the first partition (which is the ext3 partition I store the snapshots on) and the script /usr/local/bin/backup.sh should be run.
To find out the serial number for your external hard drive, run
udevinfo -a -p $(udevinfo -q path -n /dev/sdX)
where sdX is the device node for your drive. If you run dmesg in a console or terminal after plugging the drive in, you should see what it is.
If you have a look at the backup script, you will see a few customisable values at the start. The important values to check are RUN, EMAIL, DEV, DIR and DIRLIST.
RUN is a file that needs to exist for the script to run.
EMAIL speaks for itself, the address that logs are sent to - but you will need to install say mailx and an smtp server, so command line email works.
DEV is is name of the symlink as specified in the udev rules file.
DIR is where you want the disk to be mounted (it should not exist before or after the script runs)
DIRLIST is a list of directories you want backed up.
So now when I plug in my travelstar harddisk it whirs away for a while, then is unmounted, and I get an email detailing what has happened.
To restore files, you need to remove /etc/.autobackup - which will prevent the script from running when the drive is connected. You can then mount the disk by hand and run rdiff-backup to restore files from your snapshots.
Originally posted on www.cc.com.au as "automagic backups".