How to back up files periodically using rsnapshot and NFS in Ubuntu Linux

rsnapshot is a utility that uses rsync to synchronize files between two directories. rsnapshot makes it easier for system admin to backup crucial system data files regularly with minimal bandwidth and effort.

This guide assumes that you’ve already installed nfs-client and rsnapshot via “apt-get” utility in your Debian/Ubuntu Linux system.

Assumptions:
The backup server is connected in the same LAN as the main computer. The backup server is mounted as NFS on the main computer, ip address of the backup server is 192.168.1.100.

Step 1: Create a script to mount backup server filesystem
This is to ensure that the backup server is available at least hourly (or daily, depending on your requirements), save the file as “mount-backup-server.sh”

File content:

#!/usr/bin/bash
# mount-backup-server.sh
mount 192.168.1.100:/backup-point/  /media/backup/

Step 2: Ensure the mount script is run hourly

sudo cp mount-backup-server.sh /etc/cron.hourly
sudo chmod a+x /etc/cron.hourly/mount-backup-server.sh

Step 3: Edit /etc/rsnapshot.conf

sudo nano /etc/rsnapshot.conf

Change the “snapshot_root” directory to NFS mount as specified in Step 1

rsnapshot
#All snapshots will be stored under this root directory.
snapshot_root  /media/backup/rsnapshot/

Uncomment these lines:

cmd_cp  /bin/cp
cmd_rsync /usr/bin/rsync
cmd_rsnapshot_diff /usr/bin/rsnapshot-diff

For cmd_rsnapshot_diff ensure that you’ve changed the directory to /usr/bin/rsnapshot-diff instead of the default (at least in Ubuntu 11.04) /usr/local/bin/rsnapshot-diff!

Then look for “BACKUP POINTS / SCRIPTS” section (near the end of the file), and comment/uncomment or add other directories that you wished to backup :

# LOCALHOST
backup /home/  localhost/
backup /var/www/ localhost/

Save the file and exit.

Step 4: Crontab the “rsnapshot” command
Add rsnapshot to the crontab by running “sudo crontab -e” command.

Example crontab entry:

55 23 * * * root /usr/bin/rsnapshot daily

This will ensure that the snapshot will be made once a day at 11:55 pm. Make sure that you’ve tested this script and the consistency of the files between the backup server and the main computer before using this in production environment!