Wednesday, September 14, 2011

Rsnapshot - A backup Utility


There are many ways to backup Servers. One of the better ways to accomplish this is using rsnapshot. Rsnapshot is nothing but a filesystem snapshot utility for backing up local and remote systems.
Rsnapshot is written in Perl, and depends on rsync. With ssh access, it is possible to backup remote servers.

Why Rsnapshot?

We have many familiar ways to generate full backup and copy it to another server which includes ftp, scp and all. But now the question that comes to your mind will be “Why should I use rsnapshot ? or What is so special about this tool?”
Listed below are the major advantages of rsnapshot, which make you feel that it is worth using.
  • Using rsync and hard links, it is possible to keep multiple, full backups instantly available. The disk space required is just a little more than the space of one full backup, plus incrementals. This comes as a criteria when your drive is lacking enough free space to accommodate 3 copies of backup.
  • Depending on your configuration, it is quite possible to set up in just a few minutes. Files can be restored by the users who own them, without the root user getting involved.
  • There are no tapes to change, so once it’s set up, you may never need to think about it again.
  • Rsnapshot takes advantage of hard links (multiple directory pointers to the same data) to give the appearance of multiple full backups yet requires only enough disk capacity to store the complete data set plus any changed files. Thus we have the illusion of full hourly, daily, weekly, and monthly backups without having the physical space to hold that many copies.
  • Another benefit is that rsync is cross-platform, so it isn’t constrained to *nix systems.

Installation of rsnapshot

Rsnapshot can be installed in a few minutes of time. It requires the following prerequisites.
1) Perl
2) rsync
i). Download the latest source tarball from http://www.rsnapshot.org/downloads.html
ii). Untar the source code package
tar xzvf rsnapshot-1.3.0.tar.gz
iii). Change to the source directory.
cd rsnapshot-1.3.0/
iv). Select the directory to install the files. Usually the files are placed in /usr/local directory and the config files in the /etc directory.
v). Run the configure script
./configure --sysconfdir=/etc
vi). Install the program by
make install
Now rsnapshot is installed under /usr/local, with the config file in /etc.

Configuration

A sample copy of the rsnapshot config file is provided with the package. We need to just copy the file.
cp /etc/rsnapshot.conf.default /etc/rsnapshot.conf
The directives that need to be configured in rsnapshot.conf
1. snapshot_root
This is the snapshot root directory which holds the file system snapshots.
snapshot_root   /.snapshots/
2. Modify the path to the various programs like rm for removing files, rsync, ssh etc. Usually not much modification is needed here.
3. Specify thebackup intervals.
interval        hourly  6
interval        daily   7
interval        weekly  4
In this example, rsnapshot is taken every four hours, or six times a day (these are the hourly intervals), 7 times a week, 4 times a month. Thus it covers the whole month (4 weeks)
4. Select verbose level, loglevel and logfile path. Typical values used are
verbose         2
loglevel        3
logfile        /var/log/rsnapshot
5. Next main configuration to be done is BACKUP POINTS / SCRIPTS
This is the section where you tell rsnapshot what files you actually want to back up. You put a “backup” parameter first, followed by the full path to the directory or network path you’re backing up. The third column is the relative path you want to back up to inside the snapshot root.
# LOCALHOST
backup   /home/          localhost/
backup   /etc          localhost/
backup  /usr/local/      localhost/
For example, take case 1, where the parameter is “backup”. We are backing up the /home partition of the server on the rsnapshot root of our server itself. Thus with this backup parameter, a backup of /home is created in /root/.snapshots
In addition to full paths on the same server, we can also backup filesystems on remote systems using rsync over ssh. To get this done,
a) The ssh daemon must be running on the remote server.
b) You must have access to the account you specify the remote machine, in this case the root user on remote server.
c) You must have key-based logins enabled for the root user at remote server, without passphrases.
# EXAMPLE.COM
backup root@example.com:/home/ example.com/
backup root@example.com:/home/ example.com/ exclude=mtab,exclude=core
In example 1, /home partion of remote server example.com is backed up to the server where snapshot is running. In example 2, similarly /etc partion is backed up, excluding mtab and core directories.
This backup parameter is commonly used in live servers for backup configuration.
Allowing remote logins with no passphrase is a security risk that may or may not be acceptable in your situation. Make sure you guard access to the backup server very carefully! If we wish to perform backup as another user, we could specify the other user instead of root for the source (i.e. user@example.com).
Now with this we have completed the basic configuration of rsnapshot.
6. Testing configuration file.
rsnapshot configtest
If all is well, it should say Syntax OK, or else the errors are shown. We have to use tabs in the config file and not spaces.
7. Test run of rsnapshot
rsnapshot -t hourly
This tells rsnapshot to simulate an “hourly” backup. It will print out the commands it will perform when it runs for real.

Automating rsnapshot using cron job

Edit root’s crontab by command
crontab -e
Add the following entries,
0 */4 * * * /usr/local/bin/rsnapshot hourly
30 23 * * * /usr/local/bin/rsnapshot daily
Cron should be timed in a way that the hourly backup is finished before performing the daily backup.

How it works

All backups are stored in the snapshot directory. New directories inside the snapshot root are created when rsnapshot hourly and weekly are run. Thus when rsnapshot hourly is run 6 times, the directories with name hourly.0, hourly.1, ….hourly.5 are created. Similarly when rsnapshot weekly is run, 7 new directories are created namely weekly.0, weekly.1, weekly.2 till weekly.6.
Each subsequent time rsnapshot is run with the hourly command, it will rotate the hourly.X directories, and then “copy” the contents of the hourly.0 directory (using hard links) into hourly.1.
When rsnapshot daily is run, it will rotate all the daily.X directories, then copy the contents of hourly.5 into daily.0.
hourly.0 will always contain the most recent snapshot, and daily.6 will always contain a snapshot from a week ago.
Unless the files change between snapshots, the “full” backups are really just multiple hard links to the same files.
That is how rsnapshot saves disk space. If the file changes at any point, the next backup will unlink the hard link in hourly.0, and replace it with a brand new file.
When weekly, monthly, and yearly intervals defined (in that order), the weekly ones would get updated directly from the filesystem, the monthly ones would get updated from weekly, and the yearly ones would get updated from monthly.

Conclusion

When the aforesaid instructions are followed, rsnapshot installation is quite simple and is very efficient in performing automatic backups of your system. The amount of disk space taken up will be equal to one full backup, plus an additional copy of every file that is changed.

Reference: