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
\g command. So, for example, you can do:
$ tar -czp -g incrlist.snar -f first-backup.tgz /some/directory
Edit files and stuff, and when you want to do the next backup, do
$ tar -czp -g incrlist.snar -f second-backup.tgz
You can then restore these backups by first restoring first-backup.tgz and then second-backup.tgz, using the
-g 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:
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
$scriptdir to reference the directory that the backup script is stored in and
$configdir 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
echo "This is run before tar."
echo "This is run after tar."
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.