Sunday, April 28, 2013

HOWTO Backup Local Linux Host Using rsnapshot utility

rsnapshot is an awesome free Linux backup utility to perform rsync backup of both local and remote file system.

In this post, we will try to explain how to install and configure rsnapshot utility to take rsync backup of local file system.

rsnapshot uses the combination of rsync and hard links to maintain full-backup and incremental backups. Once you’ve setup and configured rsnapshot, there is absolutely no maintenance involved in it. rsnapshot will automatically take care of deleting and rotating the old backups.

It also takes very less disk space. The first full backup will take the same size as your original files and directories. Subsequent rsnapshot full backups takes only less space, as it takes backup of only the modified files from the previous full backup.

Install rsnapshot

For Ubuntu systems, run the following step:
root@anneke:~# apt-get install rsnapshot

Once rsnapshot is intalled, we will make a backup of the config file, to have always a default backup in case we need it. For this, run the following step:
root@anneke:~# cp -p /etc/rsnapshot.conf /etc/rsnapshot.conf.bckp.orig

Configure rsnapshot and specify backup source directory

Open the /etc/rsnapshot.conf and uncomment the following lines.

# vi /etc/rsnapshot.conf

cmd_cp          /bin/cp
cmd_ssh /usr/bin/ssh
cmd_du          /usr/bin/du
cmd_rsnapshot_diff      /usr/local/bin/rsnapshot-diff
logfile /var/log/rsnapshot

Define your destination backup directories in /etc/rsnapshot.conf as shown below. In this example,

/etc – source directory that should be backed-up
localhost/ – destination directory where the backup will be stored.

# vi /etc/rsnapshot.conf

backup  /etc/           localhost/
backup  /home/          localhost/

Note: Change /etc/ to the appropriate directory that you would like to backup.

Test rsnapshot configuration

Perform configuration test to make sure rsnapshot is setup properly and ready to perform linux rsync backup.

root@anneke:~# rsnapshot configtest
Syntax OK

If all is well, it should say Syntax OK. If there's a problem, it should tell you exactly what it is. Make sure your config file is using tabs and not spaces, etc.

Verify rsnapshot Backup Configuration

You can backup linux directories or files at various intervals. By default, the hourly and daily backups are configured.

Verify the hourly backup configuration

root@anneke:~#  rsnapshot -t hourly
echo 4369 > /var/run/ 
mv /var/cache/rsnapshot/hourly.1/ /var/cache/rsnapshot/hourly.2/ 
/bin/cp -al /var/cache/rsnapshot/hourly.0 /var/cache/rsnapshot/hourly.1 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /home \
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc \
touch /var/cache/rsnapshot/hourly.0/ 

Verify rsnapshot Daily Backup Configuration

Verify the daily rsnapshot backup process is configured properly.

root@anneke:~# rsnapshot -t daily
echo 4371 > /var/run/ 
/var/cache/rsnapshot/hourly.5 not present (yet), nothing to copy

Add Crontab Entry for rsnapshot

Once you’ve verified that the rsync hourly and daily backup configurations are setup properly in the rsnapshot utility, it is time to set up the crontab as shown below:

root@anneke:~# crontab -e
0 */4 * * * root /usr/local/bin/rsnapshot hourly
30 23 * * * root /usr/local/bin/rsnapshot daily

Verify the rsnapshot Job Manually (One time check-up)

For the first time, you may want to execute the rsnapshot backup manually to make sure it is working as expected.

By default all the rsync backup taken by rnspashot utility will be stored under /var/cache/rsnapshot directory.

Since rsnapshot was never executed, this directory may not exist as shown below.

root@anneke:~# ls -al /var/cache/rsnapshot
ls: /var/cache/rsnapshot: No such file or directory

Execute the rsnapshot backup utility manually for hourly configuration as shown below.

root@anneke:~# /usr/local/bin/rsnapshot hourly

Since we’ve executed the hourly backup, rsnapshot linux backup utility would’ve created the following /var/cache/rsnapshot directory with the hourly.0 directory

Browse through the /var/cache/rsnapshot/hourly.0/ directory to make sure the files and directories that you’ve configured are backed up accordingly.

root@anneke:~# ls -al /var/cache/rsnapshot/
total 16
drwx------  4 root root 4096 Apr 28 17:19 .
drwxr-xr-x 19 root root 4096 Apr 28 15:49 ..
drwxr-xr-x  4 root root 4096 Apr 28 17:20 hourly.0
drwxr-xr-x  3 root root 4096 Apr 28 17:14 hourly.1

root@anneke:~# ls -al /var/cache/rsnapshot/hourly.0/localhost/etc/
total 1212
drwxr-xr-x 149 root root    12288 Apr 28 17:19 .
drwxr-xr-x   4 root root     4096 Apr 28 17:19 ..
drwxr-xr-x   3 root root     4096 Oct 17  2012 acpi
-rw-r--r--   1 root root     2981 Oct 17  2012 adduser.conf
-rw-r--r--   1 root root       10 Apr  3 16:32 adjtime
drwxr-xr-x   2 root root     4096 Apr  8 12:49 alternatives
-rw-r--r--   1 root root      401 May 25  2012 anacrontab

Hope this guide helps!