Dead simple backup script

April 7, 2010 Tags: archlinux, bash, backup

Until a few days ago I didn't have any backup strategy. This one is quite straightforward and fits my needs...

After hearing about a few hard drive failures lately, I thought that it was time to adopt some kind of backup policy, at least something better than "occasionally copy the home folder to an external hard drive".

I've looked at a some open-source backup scripts or software in the past, but they mostly looked complicated and overkill to me. Since I have very simple needs I just implemented my solution in a simple bash script and using rsync:

#!/bin/sh

USECOLOR="yes"
BACKUP_DST="/media/drive/backup"
BACKUP_SRC="work code databases Documents Photos"
BACKUP_ABS_SRC="/etc /var/abs/local"
BACKUP_DOTFILES="yes"
RSYNC="rsync -avq --delete"

. /etc/rc.d/functions

if [ ! -d $BACKUP_DST ]; then
    echo "Error:: '$BACKUP_DST' does not exist."
    exit 1
fi

if [ "$BACKUP_DOTFILES" = "YES" -o "$BACKUP_DOTFILES" = "yes" ]; then
    stat_busy "Backing up dotfiles"
    mkdir -p $BACKUP_DST/dotfiles
    echo "Look for hidden files!" > $BACKUP_DST/dotfiles/HIDDEN
    for d in $(ls -A ~|grep '^\.'); do
        $RSYNC ~/$d $BACKUP_DST/dotfiles/
    done
    stat_done
fi

for d in $BACKUP_SRC; do
    stat_busy "Backing up '${C_BKGD}$d${C_MAIN}'"
    $RSYNC ~/$d/ $BACKUP_DST/$d/
    stat_done
done

for d in $BACKUP_ABS_SRC; do
    stat_busy "Backing up '${C_BKGD}$d${C_MAIN}'"
    mkdir -p $BACKUP_DST/root$d
    $RSYNC $d/ $BACKUP_DST/root$d/
    stat_done
done

What it does:

  • It "mirrors" all my content, this is not an incremental system. That prevents me from hard drive failures but not from stupidity: if I remove a directory and run the backup script, it will be removed from the backup drive.
  • What is mirrored:
    • All my dotfiles, which is the quick-and-dirty alternative to a versioned repository of all the relevant ones (activated with BACKUP_DOTFILES="yes")
    • Some folders of my home directory (set in BACKUP_SRC)
    • Some directories of my root filesystem (set in BACKUP_ABS_SRC), in this case my config files and my custom packages
  • The USECOLOR, stat_busy and stat_done are specific to my distribution (Archlinux). It just gives a nicer output than echo statements.

What it doesn't

  • Recovery: some day I might need this, but I'll do it by hand since I just have to put the files back in place.
  • Incremental backups: I already mentioned it, I just hope I won't be stupid.

I think backups serve their own purpose since they shorten hard drives' lifetime, but I prefer that than doing nothing and crossing fingers hoping the hard drive won't fail. Now I need to get a proper backup device, i.e. something with redundant storage instead of the 2.5" drive I carry all the time!

Here's a screenshot of the result:

Comments

April 26, 2010Sam

You should have a look at rdiff-backup.

http://rdiff-backup.nongnu.org/

Add a comment

Comments are closed for this entry.

Short URL

http://bruno.im/e7