Automating incremental backups with Rsnapshot

Previously I wrote about using rsync to efficiently backup your server. Automating rsync backups is easier when you use a collection of perl scripts called rsnapshot. Rsnapshot is designed to maintain backups taken at different times. These backups are called snapshots and, apart from the very first one, they all tend to be incremental in nature taking up very little disk space and bandwidth.
rsnapshot

Installing and configuring rsnapshot

Installing rsnapshot is pretty straight forward in CentOS (as root):

yum install rsnapshot rsync

Once installed the system wide configuration file can be found in /etc/rsnapshot.conf. An important point to keep in mind is that options and their arguments in this file are separated using tabs and not spaces. Some interesting options:

  • The directory where the backups will be stored:

    snapshot_root   /.snapshots/
  • Short arguments passed to rsnapshot. By default this line is commented out:

    #rsync_short_args	-a

    I recommend uncommenting this line and add the “z” switch to enable compression during transfer. This should speed up backups considerably:

    rsync_short_args	-az
  • The backup intervals determine how many of the incremental backups are stored. So by default 6 hourly backups will be stored and 7 daily backups:

    interval        hourly  6
    interval        daily   7
    interval        weekly  4
    #interval       monthly 3

    You can call the backup intervals anything you like. The only rule is that the intervals are arranged in descending order of frequency i.e. from the most frequent to the least frequent interval. Rsnapshot rotates the backups between the various intervals such that the latest backup is stored in the first directory of the first interval. So in the example above the latest backup will be in the hourly.0 directory.

    In newer versions of rsnapshot the word “interval” is actually deprecated. Instead the author recommends using the word “retain”:

    retain        hourly  6
    retain        daily   7
  • The business end of the config file is where the backup commands are placed:

     
    backup_script   /path/to/executable_script.sh      unused
    backup  user@remotehost:/source/directory/      server1/

    Using the backup_script command you can create database SQL dumps or perform other actions before the files backup takes place. Rsnapshot will copy anything the script outputs in its working directory (read local directory) to the target you specify.

    Alternatively you can create a script that will dump the remote server’s database within the remote file system to be downloaded using the backup command. In that case the target directory argument will be unused as in the example above.

    The backup command is used to download entire directories. In the above example server1 is the target directory where the backup will be stored. So the complete path will be /.snapshot/[interval].x/server1. For example the most recent hourly backup will be stored in :

    /.snapshot/hourly.0/server1/source/directory/

    You can add multiple backup and backup_script commands for different servers.

Running rsnapshot

To run rsnapshot you type the following command:

rsnapshot [interval]

Replace interval with one of the predefined intervals in the configuration file. So for an hourly snapshot you would do:

rsnapshot hourly

By default rsnapshot will use the configuration file in /etc/ if you run it as root. But users other than root can also run rsnapshot using their own configuration file. You can specify it using the -c switch:

rsnapshot -c /home/user/rsnapshot/conf/rsnapshot.conf hourly

You can also simulate the backup without actually downloading any files using the -t switch. It’s especially useful to test the configuration file:

rsnapshot -tc /home/user/rsnapshot/conf/rsnapshot.conf daily

Automatic rsnapshot using cron

By default rsnapshot does not install any cron jobs. Once you’ve customized the config file you have to make your own cron schedule by editing /etc/crontab and adding lines like these for a system wide cronjob:

00 */3 * * * root /usr/bin/rsnapshot hourly
50 22 * * * root /usr/bin/rsnapshot daily
30 22 * * 6 root /usr/bin/rsnapshot weekly
15 22 28 * * root /usr/bin/rsnapshot monthly

Conclusion

Using rsnapshot you can automatically backup one or more servers. Each server backup will be placed in its own directory and incremental backups will be created and rotated each time rsnapshot called. And the best thing is that you can do all of this by editing just one configuration file!

Leave a Reply

Your email address will not be published. Required fields are marked *