We all know backups are an essential part of running a server. Being able to restore in the worst case scenario of complete drive corruption (let’s say that your RAID setup failed somehow as well) as quickly as possible is key. Another situation could be one of your clients deleted their incredibly important file 3 days ago, but decided to call you about it now.
Rsnapshot is a series of scripts and commands which can automate the process of backing up your files to a remote location, as well as keeping a incremental copy of any changes. I have it set to keep 7 daily copies, 4 weekly copies (on Saturdays) and 6 monthly copies. Now this may sound like it will use up a great deal of disk space, but rsnapshot makes clever use of hard links, which means it only needs to store copies of files that have changed since the last backup as well as aslight overhead. In my case, this means that for every 100 megs that is backed up, on average only 1 extra meg is stored per copy. So, if I had 1GB of data and 17 old versions (7 days + 4 weeks + 6 months) it would require approximately 1.2GB of physical disk space.
Installing rsnapshot using Linux is pretty straight forward. You’ll need rsync installed, and ssh access to the remote server. In Debian based distributions, a simple
sudo apt-get install rsnapshot |
will install it for you on the client side. If you are using Fedora, or CentOS there is an rpm on the rsnapshot download page as well as gzip for any other distribution.
Once that step is complete, it’s time to configure it. The configuration file by default is read from /etc/rsnapshot.conf. There is an important message at the very top of this file that I managed to miss. Note that the file requires trailing slashes on any directories you specify (ie: /home/danklassen/) and it uses tabs and not spaces. Failing to follow these two simple standards can lead to untold amounts of grief (not that I would know of course).
Before going much further, it would be good to set up ssh keys between your client and server. If you’re not sure how to do this, I conveninently have a post about that titled Creating SSH Keys. This will keep you from having to type in a password each time rsnapshot tries to connect to the remote server.
Since we are going to be backing up a remote server, we’ll want to double check that cmd_ssh line is uncommented and points to where where the ssh command resides on your system (double check with ‘which ssh’ if you are unsure). Also, we’re using linux, so we will want to uncomment the cmp_cp line as well. Since linux’s cp command has a few extra features built it, this will allow rsnapshot to take care of normal files and special files all in one pass which should be quicker.
I found I had to play with the rsync_long_args line a little bit to get things to work as expected. What I ended up with is:
rsync_long_args --delete --numeric-ids --delete-excluded |
Which will delete any files that have been removed on the server, use numeric ids for user / group permissions, and delete any files that are in the excluded list. If you have any other fancy parameters you want to pass along to rsync, this would be the place to enter them.
Now the important part: setting what to back up, and how often. Near the top of the config file are the lines:
interval hourly 6 interval daily 7 interval weekly 4 interval monthly 6 |
These lines will define how many copies to store of each ‘interval’. If you want, you can add ‘interval yearly 5′ in there as well. You will need to set up cron jobs for each of these intervals.
The backup points section defines what to backup. An example for a remote server would be something like:
backup user@example.com:/home/danklassen/ danklassen/ |
What this will do is ssh over to example.com with the username ‘user’ and back up the /home/danklassen/ directory into a folder called danklassen/ in the snapshot_root of the client computer. Again, you’ll want ssh keys working first so that you can do this without having to enter a password.
The last step once you have gotten everything working is to cron your scripts because who are we kidding, you won’t remember to run them yourself. A quick crontab -e and enter something like
00 * * * * rsnapshot hourly 30 23 * * * rsnapshot daily 30 23 * * 6 rsnapshot weekly 30 23 1 * * rsnapshot monthly |
I hope this gets you on the right path. If you’re looking for any more details, the rsnapshot site has some example setups and typing ‘man rsnapshot’ will explain every setting for you. Now when your client calls and tells you they deleted their precious data a few days ago, you’ll be able to go back in time and save the day!
Coming up, I’ll show you how to do a similar setup for your mysql databases.
Tags: backup, server admin, system admin
