Daily Incremental Backups With Rsnapshot
Daily Incremental Backups With Rsnapshot
A HOWTO guide for the QNAP TS-439 / TS-639, and an introduction to use of IPKG
Summary
rsnapshot is a file system backup utility based on rsync. Using rsnapshot is possible to take snapshots of
your file system(s) at different points in time. Using hard links, rsnapshot creates the illusion of multiple full
backups, while only taking up the space of one full backup plus changes. When coupled with SSH, it is
possible to securely take snapshots of remote file systems as well1.
This document describes how to set up rsnapshot on the QNAP TS-439 and TS-639 NAS boxes with a view
to schedule a nested and rotating series of incremental backups (but I imagine that it would work on all x86
models, and perhaps others with minor modification). For example, you could schedule:
So why should we care? Disk space! If we made a full physical copy of our file system each time we took a
snapshot we would run out of disk space very quickly. It is more efficient to make a copy of the directory
structure using hard links instead. Our snapshot will simply consist of links to the files already stored on the
backup disk, plus full copies of any files that are new or have changed.
When files are deleted they will (of course) not appear in the next snapshot. However, they will still be
available from older snapshots that contain hard links to them. If rsnapshot finds that some files have been
modified, it will delink them and drop the new version into the next snapshot – but the older versions of the
same files will still be available from older snapshots. So rsnapshot will give you a good time series of
backups.
Requirements
You will need to install:
• QPKG-IPKG. This is a package manager that you can use to automatically download and install
additional software packages onto your NAS, including rsnapshot.
• rsnapshot. This is a filesystem snapshot utility. It can take incremental snapshots of local and
remote file systems. It makes use of rsync and hard links, so it is very space efficient.
Both of these programmes are excellent and easy to use. In the case of IPKG, you will doubtless be tempted
to install some additional tools for your NAS after you have seen how easy it is.
Procedure
1. Install IPKG
IPKG is package manager that automates the download and installation of new software. We can use it to
easily download and install rsnapshot plus all its dependencies in one go. The installation process is as
follows:
That’s it! Like other package managers, IPKG can retrieve a list of available software packages from a ‘feed’
somewhere on the internet. To make IPKG retrieve the list, login to your NAS by SSH (Windows people can
use PuTTy), and type:
ipkg update
ipkg list
Or if you just want to see packages related to rsnapshot, you could type:
2. Install rsnapshot
To download and install the rsnapshot package (and its dependencies), type:
Your NAS will retrieve and install everything for you. Too easy!!
I’m going to stick to use of ‘nano’ in this post, but you can use the ‘vi’ editor instead if you prefer, which has
the advantage of being installed on the QNAP by default.
For instructions on how to configure this file (and set up your backup jobs), download the excellent
rsnapshot HOWTO1 guide from: http://rsnapshot.org/howto/. It is also worth downloading the rsnapshot
manual (man page) from the same site, as it contains additional details about some of the options
available.
It is IMPORTANT that you READ the [official] rsnapshot HOWTO guide THOROUGHLY, from section 4
(configuration) onwards, otherwise you are most certainly going to mess this up. The best way is to open
rsnapshot.conf in your editor and carefully check each item as you read through the HOWTO one item
at a time. To open the configuration file, type:
nano /opt/etc/rsnapshot.conf
Or if you insist on using the default editor, vi, you would type:
vi /opt/etc/rsnapshot.conf
rsnapshot configtest
If all is well, it will say ‘syntax ok’. If there is a problem, it will spit errors at you. If you get errors, check that
you have separated items in the file using tabs (spaces are not allowed in the configuration file).
nano /etc/config/crontab
Append your desired cron jobs, as discussed in the rsnapshot HOWTO. Note that it is important to make
sure your higher-level backups run before the lower ones. As an example, I’m using:
30 23 * * * /opt/bin/rsnapshot daily
30 22 * * 0 /opt/bin/rsnapshot weekly
30 21 1 * * /opt/bin/rsnapshot monthly
Save the modified crontab, then relaunch it to make sure your changes ‘stick’ when your NAS is rebooted
(otherwise they will be deleted). You do this by typing:
crontab /etc/config/crontab
Basically, you need to copy the public SSH key from your backup NAS (the machine running rsnapshot,
where all the backups will be pulled to) into the authorized_keys file on the target NAS (the machine(s) that
you want to backup). To do this, login to SSH on your *backup NAS*, and type the following2:
Where admin@hostname is the ssh login for the *target* NAS. You can use the hostname or its IP address,
whatever you prefer. You will be prompted to enter the admin password for the target NAS. Do it.
The last thing you need to do is set the correct access permissions on the configuration folder in the
*target* NAS, otherwise the key-based authentication won't work2 and you'll get an 'access denied (public
key)' error or password challenge when your backup NAS tries to login. The problem is the folder
permissions get reset when the NAS is rebooted (note that if the target machine is a normal computer you
won’t have to worry about this). However, there is a special file on your QNAP that can be used to run
commands on startup3. We are going to use it to restore our changes to the folder permissions on start up
(reboot). Login to your *target* NAS by SSH and open the autorun.sh file:
nano /tmp/config/autorun.sh
Add the following three lines to the file, then save it:
umount /tmp/config
This works for the x86 QNAP NAS (models TS-439, TS-509, TS-639, TS-809, TS-809U). If you are using a
different model, then you need to change the first line (see the QNAP Wiki for correct command):
http://wiki.qnap.com/wiki/Autorun.sh.
Once you have that working, reboot your NAS (both the target and backup machines) and manually run an
rsnapshot hourly job again. This is to make sure that all your changes can survive the reboot process. If
they don’t, you may need to go over the key-based authentication and rsnapshot configuration steps again,
depending on the error message you observe, to make sure you have them right.
Be warned that the first run of rsnapshot will take *ages* if you have a lot of data as it needs to physically
copy all the files, although subsequent runs are very fast if you don’t have too many files to update. The
first run of rsnapshot also puts a heavy load (100%) on the processor, so don’t do it while your system is
busy with other things. You can observe load average by running the ‘top’ tool in another shell.
8. Monitoring rsnapshot
Monitoring and testing is an essential part of any backup procedure. There’s nothing worse than finding out
that your backup hasn’t been working for six months on the day that your system crashes. rsnapshot has a
logfile that records warnings and error messages. You can find/open it here:
nano /opt/var/log/rsnapshot
Make *sure* you inspect it regularly to make sure your backup jobs are running smoothly. If there are
problems, they will be recorded here. You can increase or decrease the level of logging detail in the
configuration file if you need to, rsnapshot.conf. See the official HOWTO for more information.
Enjoy!
Crushdepth (http://www.crushdepth.net).
References
1. Rosenquist, N. (2004). rsnapshot HOWTO. Available online at: http://rsnapshot.org/howto/