Daily Archive for January 20th, 2013

I hate backups.

Ah, backups. I hate backups. Everyone hates backups. But the only thing worse than taking backups is not taking backups.

I have a couple of scripts that I’ve written to take backups of my life and now I’ve gotten them to a point where I think other people might find them useful.

I used to use dar. dar is pretty cool because it lets you perform incremental, encrypted backups. It’s pretty not-cool because it’s hard as hell to use. In fact, I’ve never been able to get incremental backups working with encryption. I’ve gotten tired of trying.

So I decided to go back to the one true archiving utility, tar. Few people know that tar actually also supports incremental backups using the [cci]\g[/cci] command. So, for example, you can do:

[cc]$ tar -czp -g incrlist.snar -f first-backup.tgz /some/directory[/cc]

Edit files and stuff, and when you want to do the next backup, do

[cc]$ tar -czp -g incrlist.snar -f second-backup.tgz[/cc].

You can then restore these backups by first restoring first-backup.tgz and then second-backup.tgz, using the [cci]-g[/cci] switch as before. This will even delete files that you deleted between taking first-backup and second-backup.

Okay, cool, so let’s make that into a script. I wrote a script for backing up my home directory and other important stuff on my hard drive to an external drive. Unfortunately, I was running out of space on my RAID array, so I incorporated 640GB of a 1TB drive that I used to use as a “buffer zone” for backups into another drive for the array. So, now I just issue my backups directly to my external drive.

The system generates one full backup each month and then incremental backups for the rest of that month. It’s configured by sourcing a config file that sets the appropriate variables. An example config file might look like this:

[cc]backupdir=/mnt/backup
targetdir=/home/me
prefix=home
excludes=”–exclude=.VirtualBox –exclude=*.pkg.tar*”[/cc]

Line 1 specifies where to store the backup (and look for an existing full backup from the same month to diff against). 2 says what to backup. 3 specifies a prefix name to apply to the archive. Finally, 4 offers the ability to exclude files. Config files can use the variable [cci]$scriptdir[/cci] to reference the directory that the backup script is stored in and [cci]$configdir[/cci] to reference the directory the config file is stored in.

You can even specify commands to run before and after the backup by filling in the functions [cci]precmd()[/cci] and [cci]postcmd()[/cci]:

[cc]precmd() {
echo “This is run before tar.”
}

postcmd() {
echo “This is run after tar.”
}[/cc]

So what about encryption? Well, since I only take backups to one specific drive, I’ve decided to opt for drive-level encryption with LUKS/dm-crypt. This is a far more robust solution that has also proven to be secure.

I’ll talk about how I back up my server at some other point.