cippaciong

Home | About | Writings

Simple file backup with rsnapshot

2015-08-07

Hello, in the last post we saw how to upgrade Debian Wheezy to the most recent release, Jessie and before doing anything we backed up the whole system with a simple rsync command. While this is great for occasional backup, it's pretty worthless having a backup which is not automated. This is why now we are going to set up an automated, incremental backup system using rsnapshot, so that our files will be safe from data loss and corruption.

A note on rsnapshot

First of all let's talk about the software we are going to use. As the website says: "rsnapshot is a filesystem snapshot utility based on rsync. rsnapshot makes it easy to make periodic snapshots of local machines, and remote machines over ssh. The code makes extensive use of hard links whenever possible, to greatly reduce the disk space required." The software is entirely written in Perl and, even if its development has stopped for some years, recently the project got a new leader and new releases are coming out in these days.

Configuration

Configuring rsnapshot is a really simple operation as everything is defined in a short configuration file. This is how a config file for rsnapshot looks like, its locations is (usually) /etc/rsnapshot.conf

Warning: rsnapshot config file requires you to use tabs between elements rather than spaces.

config_version          1.2
snapshot_root           /media/backup/rsnapshot/
cmd_cp                  /bin/cp
cmd_rm                  /bin/rm
cmd_rsync               /usr/bin/rsync
cmd_logger              /usr/bin/logger
cmd_du                  /usr/bin/du
cmd_rsnapshot_diff      /usr/bin/rsnapshot-diff
retain                  hourly  6
retain                  daily   7
retain                  weekly  4
retain                  monthly 3
verbose                 2
loglevel                3
logfile                 /media/backup/rsnapshot/rsnapshot.log
lockfile                /media/backup/rsnapshot/rsnapshot.pid
backup                  /media/data/       data/

Let's see the meaning of the most important options:

Once you have finished with the creation of your config file make sure there isn't any error running sudo rsnapshot configtest.

Backup automation

As we said, a backup which is not automated can hardly be called a backup so, now that rsnapshot is properly configured, we can focus on automating the backup process using cron. What we want is to make backup on regular interval of four hours for the hourly backup, and once in a day/week/month for the other three intervals. This result can be achieved quite easily using crontab, simply run # crontab -e to start editing the user's (root in this case) crontab, and paste this four lines:

0 */4 * * *       /usr/bin/rsnapshot hourly
30 23 * * *       /usr/bin/rsnapshot daily
40 21  *  *  0    /usr/bin/rsnapshot weekly
30 19  1  *  *    /usr/bin/rsnapshot monthly

of course you should replace hourly, daily, weekly, monthly with the retain names you used in your config file.

Here is what each line does:

Backup restoration

The setup outlined above will create a folder structure inside our backup destination which should look more or less like this:

drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-06 00:00 daily.0
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-05 00:00 daily.1
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-04 00:00 daily.2
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-03 00:00 daily.3
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-02 00:00 daily.4
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-01 00:00 daily.5
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-07-31 00:00 daily.6
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-07 12:00 hourly.0
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-07 08:00 hourly.1
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-07 04:00 hourly.2
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-07 00:00 hourly.3
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-06 20:00 hourly.4
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-08-06 16:00 hourly.5
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-02-01 00:00 monthly.0
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2014-11-09 00:00 monthly.1
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2014-10-10 00:00 monthly.2
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-03-06 00:00 weekly.0
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-02-22 00:00 weekly.1
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-02-15 00:00 weekly.2
drwxr-xr-x 3 cippaciong cippaciong 4,0K 2015-02-08 00:00 weekly.3
-rw-r--r-- 1 cippaciong cippaciong 926K 2015-08-07 12:00 rsnapshot.log

Now, in case of disaster, file restoration will be really simple thanks to the fact that rsnapshot doesn't save files in archives but it simply copy them as they are (or uses hard links). That said, we can restore single files by cherry picking them from the related backup folder or we can perform a full restore using rsync with the command # rsync -a /media/backup/rsnapshot/monthly.0/data/. /destination/folder/.

--end log report--