Perl Scripts

snapback

This is a backup tool, based upon ideas due to Mike Rubel and John Bowman. (There are lots of other scripts out there that do more or less the same thing. This one works for me.)

The script generates a series of backups, each of which is a complete "snapshot" of the appropriate file system at the time of the backup. Because of how the script works, however, the individual snapshots do not together consume as much space as they ordinarily would, since files that are unchanged are hard-linked. So, on my /backup partition, I've got snapshots of all my important files going back over two years:

[rgheck@rghstudy files]$ ll /backup/rgheck/files/
total 68K
drwxr-xr-x  3 rgheck rgheck 4.0K Oct  6 00:00 backup_1
drwxr-xr-x  3 rgheck rgheck 4.0K Sep 29 08:00 backup_11
drwxrwxr-x  3 rgheck rgheck 4.0K Jul 15  2004 backup_1227
drwxr-xr-x  3 rgheck rgheck 4.0K Jun 20 08:00 backup_139
drwxr-xr-x  3 rgheck rgheck 4.0K Sep 26 20:00 backup_19
drwxr-xr-x  3 rgheck rgheck 4.0K Oct  5 20:00 backup_2
drwxr-xr-x  3 rgheck rgheck 4.0K May 13 12:00 backup_203
drwxr-xr-x  3 rgheck rgheck 4.0K Sep 21 12:00 backup_27
drwxr-xr-x  3 rgheck rgheck 4.0K Oct  5 16:00 backup_3
drwxr-xr-x  3 rgheck rgheck 4.0K Feb 12  2006 backup_331
drwxr-xr-x  3 rgheck rgheck 4.0K Sep 10 08:00 backup_43
drwxr-xr-x  3 rgheck rgheck 4.0K Dec  9  2005 backup_459
drwxr-xr-x  3 rgheck rgheck 4.0K Oct  3 12:00 backup_5
drwxr-xr-x  3 rgheck rgheck 4.0K Oct  1 00:00 backup_7
drwxr-xr-x  3 rgheck rgheck 4.0K Jun 17  2005 backup_715
drwxr-xr-x  3 rgheck rgheck 4.0K Aug 16 16:00 backup_75
(Note that the father back you go, the less frequent the snapshots.) And while backup_1 occupies 1.9GB, all of those backups together only occupy 2.9GB! That's what hard-linking gets you.

The way the script is currently written, it requires the directory being backed up and the directory to which the backup is being done to be locally mounted filesystems. That said, however, if you wanted to backup your files to a remote server, you could use something like sshfs to mount the remote filesystem locally. Alternatively, you could tunnel NFS via SSH, though that is probably a lot harder to do.

Using snapback is then as easy as writing a shell script like this one:

#!/bin/bash
DATE=`date +%Y%m%d`;

/usr/local/bin/snapback -d /backup/rgheck/files \
  -l /home/rgheck/bin/log/backup.$DATE.log /home/rgheck/files
/usr/local/bin/snapback -m 180 -d /backup/rgheck/www \
  -l /home/rgheck/bin/log/backup.$DATE.log /var/www/

echo /home/rgheck/files and /var/www directories backed up at `date`
and installing it as a cron job.