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:
- rsnapshot_root: the destination folder of our backup.
- cmd_*: we tell rsnapshot what is the full path of those binaries in our system.
- verbose: the level of verbosity (in a scale from 1 to 5) for the printed out messages.
- loglevel: the same as verbose but for the log file.
- logfile: the location where we want to save the log file.
- backup: the first argument is the folder we want to backup while the second is the name of the folder where we want to save such backup inside of rsnapshot_root. In this case we will save the content of
/media/data/
inside of/media/backup/rsnapshot/data/
. It's worth noting that we can also define multiple backup entries in order to back up different folders. - retain: this is probably the most important part (in versions prior to 1.3.1 this field was called interval). With this options we tell rsnapshot how many copies of an backup interval to retain. Calling them hourly, daily, etc. is purely conventional, we could have called them alfa, beta, gamma and the result would have been the same. rsnapshot would have kept 6 hourly (alfa) backup, 7 daily (beta) backup and so on. Be careful because setting this options doesn't mean that rsnapshot will automatically start to make backups hourly, daily, weekly and so on, we are simply telling him how many copies of those backup it should keep on disk, the actual backup schedule is done using cron jobs.
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:
0 */4 * * * /usr/bin/rsnapshot hourly
runs the hourly backup every four hours on the 0th minute so as soon as the hour change.30 23 * * * /usr/bin/rsnapshot daily
runs the daily backup each day at 23:30.40 21 * * 0 /usr/bin/rsnapshot weekly
runs the weekly backup on the 0th day of the week which is Sunday, at 21:40.30 19 1 * * /usr/bin/rsnapshot monthly
runs the monthly backup at 19.30 of the first day of the month.
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--