How to install rsnapshot remote backup server on Red Hat/CentOS Linux

Posted on in Categories , , , , , , , last updated December 4, 2017

I have HP server with hardware RAID 6 running RHEL/CentOS Linux 5.x/6.x/7.x. I would like to act this box as a backup server for my other servers, desktops and laptops computers all powered by Linux/macOS or Unix-like system The server must keep backup in hourly, daily and monthly format. How do I configure my Red Hat Enterprise Linux or CentOS Linux server as remote backup or snapshot server using rsnapshot?

rsnapshot is a free and open source filesystem snapshot software. It is simple, reliable and disaster recovery backup solution. It is a remote backup program that uses rsync to take backup snapshots of filesystems.
How to setup and install rsnapshot on a CentOS/Red Hat (RHEL) Linux
It uses hard links to save space on disk and offers following features:

  • Filesystem snapshot – for local or remote systems
  • Database backup – MySQL/PgSQL backup
  • Secure – Traffic between remote backup server is always encrypted using openssh
  • Full backup – plus incremental daily, hourly, and monthly backups
  • Easy to restore – Files can restored by the users who own them, without the root user getting involved.
  • Automated backup – Runs in background via cron.
  • Bandwidth friendly – rsync can save bandwidth too

This tutorial shows you how to install and use rsnapshot on a CentOS or RHEL based server.

Sample setup

  • snapshot.example.com – HP box with RAID 6 configured with Red Hat / CentOS Linux act as backup server for other clients.
  • DNS ns1.example.com – Red Hat server act as primary name server.
  • DNS ns2.example.com – Red Hat server act as secondary name server.
  • www.example.com – Red Hat running Apache web server.
  • mysql.example.com – Red Hat mysql server.

Install rsnapshot on a RHEL/CentOS Linux box

Login to snapshot.example.com server. Type the following yum command to install rsnapshot (first enable EPEL repo on a CentOS/RHEL )
# yum install rsnapshot
Sample outputs:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * epel: ftp.cuhk.edu.hk
 * extras: mirrors.vinahost.vn
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package rsnapshot.noarch 0:1.4.2-2.el7 will be installed
--> Processing Dependency: rsync for package: rsnapshot-1.4.2-2.el7.noarch
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
===============================================================================
 Package            Arch            Version                Repository     Size
===============================================================================
Installing:
 rsnapshot          noarch          1.4.2-2.el7            epel          116 k
Installing for dependencies:
 rsync              x86_64          3.0.9-18.el7           base          360 k
 
Transaction Summary
===============================================================================
Install  1 Package (+1 Dependent package)
 
Total download size: 476 k
Installed size: 1.1 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): rsync-3.0.9-18.el7.x86_64.rpm                      | 360 kB   00:00     
(2/2): rsnapshot-1.4.2-2.el7.noarch.rpm                   | 116 kB   00:01     
-------------------------------------------------------------------------------
Total                                             265 kB/s | 476 kB  00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : rsync-3.0.9-18.el7.x86_64                                   1/2 
  Installing : rsnapshot-1.4.2-2.el7.noarch                                2/2 
  Verifying  : rsnapshot-1.4.2-2.el7.noarch                                1/2 
  Verifying  : rsync-3.0.9-18.el7.x86_64                                   2/2 
 
Installed:
  rsnapshot.noarch 0:1.4.2-2.el7                                               
 
Dependency Installed:
  rsync.x86_64 0:3.0.9-18.el7                                                  
 
Complete!

Configure rsnapshot

You need to perform the following steps.

Step # 1: Configure password less login

To perform remote backup you need to setup password less login using openssh (see how to create password less ssh keys). Create your rsa ssh key and upload they to all servers using scp or ssh-copy-id. You need to type following commands on snapshot.example.com server:
# ssh-keygen -t rsa
OR
# ssh-keygen -t rsa -b 4096 -C "My rsnapshot backup server key"
I strongly suggest that you password protect your
Copy public key to all boxes:
##############################
## WARNING OVERWRITING FILE ##
##############################
# scp .ssh/id_rsa.pub root@ns1.example.com:.ssh/authorized_keys2
# scp .ssh/id_rsa.pub root@ns2.example.com:.ssh/authorized_keys2
# scp .ssh/id_rsa.pub root@www.example.com:.ssh/authorized_keys2
# scp .ssh/id_rsa.pub root@mysql.example.com:.ssh/authorized_keys2

To avoid overwriting files using the ssh-copy-id command as follows:
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@ns1.example.com
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@ns2.example.com
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@www.example.com
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@mysql.example.com

Step # 2: Configure rsnapshot

The default configuration file is located at /etc/rsnapshot.conf. Open configuration file using a text editor, enter:
# vi /etc/rsnapshot.conf

Configuration rules

You must follow two configuration rules:

  • rsnapshot config file requires tabs between elements.
  • All directories require a trailing slash. For example, /home/ is correct way to specify directory, but /home is wrong.

First, specify root directory to store all snapshots such as /snapshots/ or /dynvol/snapshot/ as per your RAID setup, enter:

snapshot_root	/raiddisk/snapshots/

You must separate snapshot_root and /raiddisk/snapshots/ by a [tab] key i.e. type snapshot_root hit [tab] key once and type /raiddisk/snapshots/.

Define snapshot intervals

You need to specify backup intervals i.e. specify hourly, daily, weekly and monthly intervals:

interval	hourly	6
interval	daily	7
interval	weekly	4
interval	monthly	3

The line “interval hourly 6” means 6 hourly backups a day. Feel free to adapt configuration as per your backup requirements and snapshot frequency. You can yearly backup option too:

#########################################
#     BACKUP LEVELS / INTERVALS         #
# Must be unique and in ascending order #
#########################################
 
interval        hourly  6
interval        daily   7
interval        weekly  4
interval        monthly 12
interval        yearly  5

Remote backup directories

To backup /var/named/ and /etc/ directory from ns1.example.com and ns2.example.com, append:

backup	root@ns1.example.com:/etc/	ns1.example.com/
backup	root@ns1.example.com:/var/named/	ns1.example.com/
backup	root@ns2.example.com:/etc/	ns2.example.com/
backup	root@ns2.example.com:/var/named/	ns2.example.com/

To backup /var/www/, /var/log/httpd/ and /etc/ directory from www.example.com, enter

backup	root@www.example.com:/var/www/	www.example.com/
backup	root@www.example.com:/etc/	www.example.com/
backup	root@www.example.com:/var/log/httpd/	www.example.com/

To backup mysql database files stored at /var/lib/mysql/, enter:

backup	root@mysql.example.com:/var/lib/mysql/	mysql.example.com/dbdump/

It is also possible to use this bash shell script to backup mysql database when using rsnapshot server. Save and close the file. To test your configuration, enter:
# rsnapshot configtest
Sample output:

Syntax OK

Schedule cron job

Create a new file named /etc/cron.d/rsnapshot cron file. Following values used correspond to the examples in /etc/rsnapshot.conf.

0 */4 * * *         /usr/bin/rsnapshot hourly
50 23 * * *         /usr/bin/rsnapshot daily
40 23 * * 6         /usr/bin/rsnapshot weekly
30 23 1 * *         /usr/bin/rsnapshot monthly
# yearly if set
# 0  0  1 1 *         /usr/bin/rsnapshot yearly

Save and close the file. Now rsnapshot will work as follows to backup files from remote boxes:

  1. 6 hourly backups a day (once every 4 hours, at 0,4,8,12,16,20)
  2. 1 daily backup every day, at 11:50PM
  3. 1 weekly backup every week, at 11:40PM, on Saturdays (6th day of week)
  4. 1 monthly backup every month, at 11:30PM on the 1st day of the month

How do I see my backups?

To see backup change directory to
# cd /raiddisk/snapshots/
# ls -l

Sample output:

drwxr-xr-x 4 root root 4096 2008-07-04 06:04 daily.0
drwxr-xr-x 4 root root 4096 2008-07-03 06:04 daily.1
drwxr-xr-x 4 root root 4096 2008-07-02 06:03 daily.2
drwxr-xr-x 4 root root 4096 2008-07-01 06:02 daily.3
drwxr-xr-x 4 root root 4096 2008-06-30 06:02 daily.4
drwxr-xr-x 4 root root 4096 2008-06-29 06:05 daily.5
drwxr-xr-x 4 root root 4096 2008-06-28 06:04 daily.6
drwxr-xr-x 4 root root 4096 2008-07-05 18:05 hourly.0
drwxr-xr-x 4 root root 4096 2008-07-05 15:06 hourly.1
drwxr-xr-x 4 root root 4096 2008-07-05 12:06 hourly.2
drwxr-xr-x 4 root root 4096 2008-07-05 09:05 hourly.3
drwxr-xr-x 4 root root 4096 2008-07-05 06:04 hourly.4
drwxr-xr-x 4 root root 4096 2008-07-05 03:04 hourly.5
drwxr-xr-x 4 root root 4096 2008-07-05 00:05 hourly.6
drwxr-xr-x 4 root root 4096 2008-07-04 21:05 hourly.7
drwxr-xr-x 4 root root 4096 2008-06-22 06:04 weekly.0
drwxr-xr-x 4 root root 4096 2008-06-15 09:05 weekly.1
drwxr-xr-x 4 root root 4096 2008-06-08 06:04 weekly.2

How do I restore backup?

Let us say you would like to restore a backup for www.example.com. Type the command as follows (select day and date from ls -l output):
# cd /raiddisk/snapshots/
# ls -l
# cd hourly.0/www.example.com/
# scp -r var/www/ root@www.example.com:/var/www/
# scp -r etc/httpd/ root@www.example.com:/etc/httpd/

How do I exclude files from backup?

To exclude files from backup, open rsnapshot.conf file and add following line:

exclude_file	/etc/rsnapshot.exclude.www.example.com

Create /etc/rsnapshot.exclude.www.example.com as follows:

/var/www/tmp/
/var/www/*.cache

Further readings:

  1. rsnapshot home page
  2. Man pages – rsnaspshot(1),ssh(1),ssh-keygen(1),ssh-copy-id(1)

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 30 comments/add one below):

30 comment

  1. Uncle T;
    did you see this?:

    you have to setup passwordless login.
    well i cant post a link so go back up and find the link in blue

  2. I think I have it set up correctly. I ran the rsnapshot -t minutes and took that output and copied it to the command line and it created the files correctly. My problem is creating the cron job.
    1. I created a new file /etc/cron.d/rsnapshot with the following text.
    2. It contains 5 * * * * /usr/bin/rsnapshot minutes

    This should be all, correct. Am I missing something?

    And another question, when I do the backup of /usr/mydirectory/font/ to /usr/TEST/
    I get /usr/TEST/minutes.1/localhost/usr/TEST/usr/mydirectory/font/
    Is this right?

  3. The only reason I was using minutes was to confirm that the program was running correctly as a CRON job.

  4. To test just enter the following:
    rsnapshot -v hourly

    And it will make backup from command line.

    1. No compress mode is used to store backup. So if you backup 20GB from remote server you are going to need 20-25GB for last 7 days.

  5. Thanks for supporting Vivek.
    I ‘m able to take backup hourly using cronjob and command line.
    when i tried to run backup as daily m getting the below log info:
    logs:
    [08/Jan/2010:23:50:01] /usr/bin/rsnapshot daily: started
    [08/Jan/2010:23:50:01] /usr/bin/rsnapshot daily: ERROR: Lockfile /var/run/rsnapshot.pid exists and so does its process, can not continue
    [09/Jan/2010:00:00:01] /usr/bin/rsnapshot hourly: started
    [09/Jan/2010:00:00:01] /usr/bin/rsnapshot hourly: ERROR: Lockfile /var/run/rsnapshot.pid exists and so does its process, can not continue
    [09/Jan/2010:00:59:59] /usr/bin/rsnapshot hourly: started
    [09/Jan/2010:00:59:59] /usr/bin/rsnapshot hourly: ERROR: Lockfile /var/run/rsnapshot.pid exists and so does its process, can not continue
    [09/Jan/2010:01:00:17] /usr/bin/rsnapshot hourly: started
    [09/Jan/2010:01:00:17] echo 27149 > /var/run/rsnapshot.pid
    [09/Jan/2010:01:00:17] mkdir -m 0755 -p /media/disk/intrabackup/hourly.0/
    Please help me out from this.

    Thanks,
    Narendra

    1. Hi Narendra,
      I am having similar issues with daily back ups. Did you ever get your problems resolved?

      Greg

  6. Maybe it’s not the best idea to use “scp -r” for data restore on the remote host – if you would like to keep the file permissions and also the ownership informations you shall use “scp -rp” (-p = preserve permissions) or “rsync -avH –progress /path/to/backupdir/ root@targethost:/path/to/restoredir” ;-)

    But nevertheless it’s a great HOWTO which makes it also possible for beginners to manage simple backup strategies without keeping terms like “full”, “incremental” and “differential” backups in mind!

    keep up the good work

  7. >To backup mysql database files stored at /var/lib/mysql/, enter:
    >backup root@mysql.example.com:/var/lib/mysql/ mysql.example.com/dbdump/

    Is it really correct way to backup MySQL database without previous stopping server itself or at least blocking tables?

  8. Hi Vivek,

    In this how to u mentioned DB backup.

    So while we are using mysqldump for DB backup and this shown method will be producing same result.
    means suppose i have a db in /var/lib/mysql

    so can i copy that directory as bakcup and when it will be needed……?

    can i use it as backup…….

    what is the difference between myqldump backup approach and this on which used in this article…..?

    and what is the suggested method …..?

    Regards,
    Rocky

    1. Both will backup database. However, with rsnapshot you can quickly select and restore backup based on days, week or month.

  9. Is there any way to do remote backup of system without root access (PermitRootLogin set to “no” in sshd_config)?

  10. Don’t you need to specify a username in the cron file? for a user file its not needed, but if its in /etc/cron.d/rsnapshot, you should need it.

  11. There’s one line that you forgot to set:

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

    I kept getting “cmd_ssh not defined in /etc/rsnapshot.conf” after following your guide until I uncommented that line from rsnapshot.conf.

  12. Greetings! Veery helpful advice within this article!
    It’s the llittle changes that will make the most significant
    changes. Many thanks for sharing!

  13. Vivek,
    how can we do the backup on 2 homes? like,
    snapshot_root1 /raiddisk1/snapshots/
    snapshot_root2 /raiddisk2/snapshots/

    is this possible to back up one set of data on to one partition and the other on to the other?
    best regards,

    Have a question? Post it on our forum!