rsnapshot is a filsystem snapshot utility written in Perl. It is a remote backup utility used to take single or multiple backup of filesystems from local or remote systems using rsync.
In this tutorial, my scenario will be as follows:
My local system (Backup server) is Ubuntu 13.04 desktop with IP address 192.168.1.100/24 and remote system is CentOS 6.x server with IP address 192.168.1.200/24.
My Root (Main) backup directory: /home/backup (on Ubuntu desktop)
My local system backup directory: /home/backup/local (on Ubuntu desktop)
My remote system backup directory: /home/backup/remote (on Ubuntu Desktop)
Install rsnapshot and rsync on Backup server
As I mentioned, my backup server is Ubuntu 13.04 desktop. So let us install rsync and rsnapshot packages.
On Ubuntu:
sk@sk:~$ sudo apt-get install rsnapshot rsync
On CentOS:
rsnapshot will not be found in the official repository. So add the EPEL repository to install rsnapshot.
To install EPEL repository enter the following command:
[root@server ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Now install rsnapshot and rsync:
[root@server ~]# yum install rsync rsnapshot -y
Create backup directories on Backup server:
sk@sk:~$ sudo mkdir /home/backup sk@sk:~$ sudo mkdir /home/backup/local sk@sk:~$ sudo mkdir /home/backup/remote
Configure password less SSH login for Remote systems
In order to take remote backup, you need to setup password less SSH login for your remote systems:
sk@sk:~$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/sk/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/sk/.ssh/id_rsa. Your public key has been saved in /home/sk/.ssh/id_rsa.pub. The key fingerprint is: b7:01:ae:c9:2d:f8:af:20:e1:da:65:fc:70:e2:7b:3f sk@sk The key's randomart image is: +--[ RSA 2048]----+ | | | | | . | | . . | | . S o | | . o o + . o | | o O * . . | | o = B..E | |. . oo++o. | +-----------------+
Copy the id_rsa.pub key file to remote systems:
sk@sk:~$ ssh-copy-id -i /home/sk/.ssh/id_rsa.pub root@192.168.1.200 root@192.168.1.200's password: Now try logging into the machine, with "ssh 'root@192.168.1.200'", and check in: ~/.ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
Now you can login to server using ssh without password.
Configure rsnapshot
Before proceed further, you need to know important configuration rules.
1. All directories in the config files should be end up with a slash (/). i.e You should use /home/ instead of /home.
2. Use TAB key between elements. DON’T hit Space bar.
Setup Main backup directory:
Open /etc/rsnapshot.conf file. Set main backup root directory. And also uncomment the line cmd_ssh to allow remote backups over ssh:
sk@sk:~$ sudo vi /etc/rsnapshot.conf [...] # All snapshots will be stored under this root directory. # snapshot_root /home/backup/ [...] # Uncomment this to enable remote ssh backups over rsync. # cmd_ssh /usr/bin/ssh [...]
Don’t forget to hit TAB key between snapshot_root and your backup directory. All backups will be stored under this root directory.
Define backup intervals:
sk@sk:~$ sudo vi /etc/rsnapshot.conf [...] ######################################### # BACKUP INTERVALS # # Must be unique and in ascending order # # i.e. hourly, daily, weekly, etc. # ######################################### retain hourly 6 retain daily 7 retain weekly 4 [...]
This will take backup every six hours on a day. Define your own schedule as per your requirements.
Define local system backup directories:
sk@sk:~$ sudo vi /etc/rsnapshot.conf [...] ############################### ### BACKUP POINTS / SCRIPTS ### ############################### # LOCALHOST backup /home/ local/ backup /etc/ local/ backup /usr/local/ local/ [...]
We have already defined the rsnapshot root directory. So you don’t have to specify the full path i.e /home/backup/local. Enter the directory path that you want to make backup.
Define Remote system backup directories:
sk@sk:~$ sudo vi /etc/rsnapshot.conf [...] ############################### ### BACKUP POINTS / SCRIPTS ### ############################### [...] # REMOTEHOST backup root@192.168.1.200:/home/ remote/ [...]
We have already defined the rsnapshot root directory. So you don’t have to specify the full path i.e /home/backup/remote.
In the above example, I take the backup of my remote system /home directory.
Check rsnapshot for any configuration errors
Enter the following command to test for any configuration errors:
sk@sk:~$ rsnapshot configtest Syntax OK
Test backups
Now run the hourly basis backup to test the configuration:
sk@sk:~$ sudo rsnapshot hourly
It will take a while depending upon your backup size:
Automate backup jobs using Cron
Open the cron job file /etc/cron.d/rsnapshot and uncomment the lines:
sk@sk:~$ sudo vi /etc/cron.d/rsnapshot # This is a sample cron file for rsnapshot. # The values used correspond to the examples in /etc/rsnapshot.conf. # There you can also set the backup points and many other things. # # To activate this cron file you have to uncomment the lines below. # Feel free to adapt it to your needs. 0 */4 * * * root /usr/bin/rsnapshot hourly 30 3 * * * root /usr/bin/rsnapshot daily 0 3 * * 1 root /usr/bin/rsnapshot weekly 30 2 1 * * root /usr/bin/rsnapshot monthly
Verify backup files
To see the backup files, go to the directory /home/backup and list the contents:
sk@sk:~$ sudo ls -l /home/backup/ total 12 drwxr-xr-x 3 root root 4096 Jun 19 13:22 hourly.0 drwxrwxrwx 2 root root 4096 Jun 19 12:13 local drwxrwxrwx 2 root root 4096 Jun 19 12:13 remote
Restore Backups
Suppose if you want to restore a backup of local system, navigate to root backup directory. Copy the backup files/directories you want.
Restore backup to local system:
sk@sk:~$ cd /home/backup/ sk@sk:/home/backup$ ls -l total 12 drwxr-xr-x 3 root root 4096 Jun 19 13:22 hourly.0 drwxrwxrwx 2 root root 4096 Jun 19 12:13 local drwxrwxrwx 2 root root 4096 Jun 19 12:13 remote
Here I copy the Pictures folder from my root backup directory to /home/sk/myfiles directory of local system:
sk@sk:/home/backup$ sudo cp -r hourly.0/local/home/sk/Pictures/ /home/sk/myfiles/
Verify the backup file(s) exist:
sk@sk:/home/backup$ ls -l /home/sk/myfiles/ total 4 drwx------ 2 sk sk 4096 Jun 19 13:43 Pictures
Restore backup to remote system:
sk@sk:/home/backup$ sudo scp -r hourly.0/local/home/sk/Pictures/ root@192.168.1.200:/root/
Verify the backup file(s) exist:
[root@server ~]# ls
anaconda-ks.cfg install.log install.log.syslog Pictures
As you seen in the above output my local (ubuntu) files are restored/copied to remote (centos) system.
That’s it. For more information about rsnapshot configuration options refer the official documentation.