My daily backup

February 23, 2007 [Tech]

I recently recovered my files from my daily backup, which had actually worked!

(Pop quiz: this is how I deleted half my home dir - what is wrong with this Makefile?

TMP_DIR=/tmp/gssmp

blah: blah1
    blah

...

blah7:
    ...
    rm -rf $TMPDIR/

...

)

Anyway, I am extremely grateful that the backup did work, but a slight problem was that I have been backing up absolutely everything, and never deleting anything that was backed up until I ran out of disk space.

So when I restored from backup, every file I had ever deleted (since I started backing up) re-appeared. This included files and dirs (and email) that had just been moved somewhere else - I now had 2 copies of them.

I've spent the night deleting stuff that I didn't actually want restored, and this has encouraged me to create an improved backup script that puts deleted stuff into a separate folder, named after the day it was deleted. I can delete those folders at my leisure when they get too big, but when a disaster happens like it did on Wednesday, I can restore back to a sane state by just ignoring any irrelevant deleted folders.

It's quite simple, but I hadn't thought of it before, so it might be helpful to others. Enjoy:

#!/bin/bash

# Copy this to /etc/cron.daily and make it owned by root, and executable

# Back up everything important from my main drive (hda) to the backup
# drive (hdb).

# Keep stuff that has been deleted from the real drive in a separate
# backup dir, named after the day it was deleted.

BKP_DRIVE=/media/hdb1

FLAGS="-a --delete-during --max-delete 100 --backup"

DT=`date +%F`
DELETED_DIR=deleted-$DT

function run_rsync
{
    THIS_DIR=$1
    EXCLUDES=$2

    echo rsync $FLAGS --backup-dir=$BKP_DRIVE/$DELETED_DIR$THIS_DIR \
        $EXCLUDES $THIS_DIR/ $BKP_DRIVE$THIS_DIR/
    rsync $FLAGS --backup-dir=$BKP_DRIVE/$DELETED_DIR$THIS_DIR \
        $EXCLUDES $THIS_DIR/ $BKP_DRIVE$THIS_DIR/
}

run_rsync /home/andy \
    "--exclude /iRiver --exclude /downloads --exclude /.Trash --exclude /misc/qemu"

run_rsync /etc

run_rsync /var

run_rsync /boot