How To Setup Backup Server Using Rsnapshot In Linux

Setup Backup Server Using Rsnapshot In Linux

Rsnapshot is a filesystem snapshot utility based on rsync for Unix-like operating systems. It allows you to easily make periodic snapshots of local machines, and remote machines over ssh. The good thing about rsnapshot is it makes extensive use of hard links whenever possible, to greatly reduce the disk space required. Since rsnapshot only keeps a fixed number of snapshots, the amount of disk space used will not continuously increase. Rnapshot is completely free, and open source utility that provides incremental backups. It is written entirely in perl, and it should work on most Unix-like systems.

This tutorial will walk you through setting up backup server using Rsnapshot in Linux. This tutorial will explicitly cover the installation instructions for CentOS 7, but these instructions should work on any other RPM based Linux distributions.

Setup Backup Server Using Rsnapshot In Linux

For the demonstration of this guide, I will be using two systems. One is backup server running with CentOS 7 and another one acts as client system. The client is running with Arch Linux. The details of backup server and client is given below.

Backup server:

  • OS : CentOS 7 64 bit minimal system
  • IP address : 192.168.43.150/24

Client :

  • OS : Arch Linux
  • IP address : 192.168.43.192/24

Installation

Rsnapshot is not available in in the default repositories of CentOS. You need to enable EPEL repository in-order to install Rsnapshot.

sudo yum install epel-release

Then, install Rsnapshot using command:

sudo yum install rsnapshot rsync

In Arch Linux and its derivatives, it is available in default repositories. So you can install using default package manager pacman as shown below.

sudo pacman -S rsnapshot rsync

Just in case, you want to use DEB based system like Ubuntu, you can install rsnapshot as shown below.

sudo apt-get install rsnapshot rsync

Backup Server Configuration

We need to create Root backup directory to save all back ups of your server and client systems.

Here, I am going to use /rsnapbackup/ as the root backup directory. It is the location where I am going to store all back ups.

You can define your own location of your choice to store the back ups. Let us create the Root backup directory in our backup server.

sudo mkdir /rsnapbackup/

To backup files of a remote client system over SSH, you need to setup password-less SSH authentication for remote client systems, so rsnapshot will automatically connect to the server system and save the back ups.

First, let us create SSH key pair.

ssh-keygen -t rsa

Do not enter any passphrase, because we want these systems to be able to connect to each other without having to enter the password:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ef:27:2a:3d:39:37:af:5e:36:05:10:b2:86:38:7f:e5 root@server.ostechnix.local
The key's randomart image is:
+--[ RSA 2048]----+
| . o. |
| . . o . |
| o . o . . |
| o . o . |
| . S E . |
| . . . |
| . .. + |
| . =.+o.. |
| ..*+*. |
+-----------------+

Next, copy the SSH public key to your all remote client systems.

ssh-copy-id -i /root/.ssh/id_rsa.pub sk@192.168.43.192

Sample output:

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
sk@192.168.43.192's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'sk@192.168.43.192'"
and check to make sure that only the key(s) you wanted were added.

Here, 192.168.43.192 is the IP address of my remote client system. You need to repeat the above command to copy the ssh public key to all your client systems to setup password-less authentication.

Now, we need to configure Backup server.

Please note that you should specify a trailing slash at the end of the directory names in /etc/rsnapshot.conf file. Say for example, you should specify the directory name as /rsnapbackup/, not /rsnapbackup. Also, you need tabs (not spaces) between elements/fields.

Rsnapshot default configuration is /etc/rsnapshot.conf. It is always recommended to make a back up of the default config file in case you need to reconfigure rsnapshot again.

cp /etc/rsnapshot.conf /etc/rsnapshot.conf.bak

Now, edit rsnapshot config file using command:

sudo nano /etc/rsnapshot.conf

The default configuration should just work fine. All you need to to define the backup directories and backup intervals.

First, let us setup the Root backup directory i.e We need to choose the directory where we want to store the file system back ups. In our case, I will store the back ups in /rsnapbackup/ directory.

# All snapshots will be stored under this root directory.
#
snapshot_root   /rsnapbackup/

Again, you should use TAB key between snapshot_root element and your backup directory.

Scroll down a bit, and make sure the following lines (marked in bold) are uncommented:

[...]
#################################
# EXTERNAL PROGRAM DEPENDENCIES #
#################################

# LINUX USERS: Be sure to uncomment "cmd_cp". This gives you extra features.
# EVERYONE ELSE: Leave "cmd_cp" commented out for compatibility.
#
# See the README file or the man page for more details.
#
cmd_cp /usr/bin/cp

# uncomment this to use the rm program instead of the built-in perl routine.
#
cmd_rm /usr/bin/rm

# rsync must be enabled for anything to work. This is the only command that
# must be enabled.
#
cmd_rsync /usr/bin/rsync

# Uncomment this to enable remote ssh backups over rsync.
#
cmd_ssh /usr/bin/ssh

# Comment this out to disable syslog support.
#
cmd_logger /usr/bin/logger

# Uncomment this to specify the path to "du" for disk usage checks.
# If you have an older version of "du", you may also want to check the
# "du_args" parameter below.
#
cmd_du /usr/bin/du

[...]

Next, we need to define the backup intervals:

#########################################
# BACKUP LEVELS / INTERVALS #
# Must be unique and in ascending order #
# e.g. alpha, beta, gamma, etc. #
#########################################

retain alpha 6
retain beta 7
retain gamma 4
#retain delta 3

Here, retain alpha 6 means that every time rsnapshot alpha is run, it will make a new snapshot, rotate the old ones, and retain the most recent six (alpha.0 – alpha.5). You can define your own intervals. For more details, refer the rsnapshot man pages.

Next, we need to define the backup directories. Find the following directives in your rsnapshot config file and set the backup directory locations.

###############################
### BACKUP POINTS / SCRIPTS ###
###############################

# LOCALHOST
backup /root/ostechnix/ server/

Here, I am going to backup the contents of /root/ostechnix/ directory and save them in /rsnapbackup/server/ directory. Please note that I didn’t specify the full path (/rsnapbackup/server/ ) in the above configuration. Because, we already mentioned the Root backup directory before.

Likewise, define the your remote client systems backup location.

# REMOTEHOST
backup sk@192.168.43.192:/home/sk/test/ client/

Here, I am going to backup the contents of my remote client system’s /home/sk/test/ directory and save them in /rsnapbackup/client/ directory in my Backup server. Again, please note that I didn’t specify the full path (/rsnapbackup/client/ ) in the above configuration. Because, we already mentioned the Root backup directory before.

Save and close /ect/rsnapshot.conf file.

Once you have made all your changes, run the following command to verify that the config file is syntactically valid.

rsnapshot configtest

If all is well, you will see the following output.

Syntax OK

Testing backups

Run the following command to test backups.

rsnapshot alpha

This take a few minutes depending upon the size of back ups.

Verifying backups

Check the whether the backups are really stored in the Root backup directory in the Backup server.

ls /rsnapbackup/

You will see the following output:

alpha.0

Check the alpha.0 directory:

ls /rsnapbackup/alpha.0/

You will see there are two directories automatically created, one for local backup (server), and another one for remote systems (client).

client/ server/

Check the client system back ups:

ls /rsnapbackup/alpha.0/client

Check the server system(local system) back ups:

ls /rsnapbackup/alpha.0/server

Automate back ups

You don’t/can’t run the rsnapshot command to make backup every time. Define a cron job and automate the backup job.

sudo vi /etc/cron.d/rsnapshot

Add the following lines:

0 */4 * * *     /usr/bin/rsnapshot alpha
50 23 * * *     /usr/bin/rsnapshot beta
00 22 1 * *     /usr/bin/rsnapshot delta

The first line indicates that there will be six alpha snapshots taken each day (at 0,4,8,12,16, and 20 hours), beta snapshots taken every night at 11:50pm, and delta snapshots will be taken at 10pm on the first day of each month. You can adjust timing as per your wish. Save and close the file.

Done! Rsnapshot will automatically take back ups on the defined time in the cron job. For more details, refer the man pages.

man rsnapshot

That’s all for now. Hope this helps. I will soon here with another interesting guide. If you find this guide useful, please share it on your social networks and support OSTechNix.

Cheers!

Resources:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...