Rsnapshot (Rsync Based) – A Local/Remote File System Backup Utility for Linux

rsnapshot is an open source local / remote filesystem backup utility was written in Perl language that advantage the power of Rsync and SSH program to create, scheduled incremental backups of Linux/Unix filesystems, while only taking up the space of one single full backup plus differences and keep those backups on local drive to different hard drive, an external USB stick, an NFS mounted drive or simply over the network to another machine via SSH.

Install rsnapshot backup in Linux
Install Rsnapshot Backup Tool

This article will demonstrate how to install, setup and use rsnapshot to create incremental hourly, daily, weekly and monthly local backups, as well as remote backups. To perform all the steps in this article, you must be root user.

Step 1: Installing Rsnapshot Backup in Linux

Installation of rsnapshot using Yum and APT may differs slightly, if you’re using Red Hat and Debian based distributions.

On RHEL/CentOS

First you will have to install and enable third-party repository called EPEL. Please follow below link to install and enable under your RHEL/CentOS systems. Fedora users don’t require any special repository configurations.

  1. Install and Enable EPEL Repository in RHEL/CentOS 6/5/4

Once you get things setup, install rsnapshot from the command line as shown.

# yum install rsnapshot
On Debian/Ubuntu/Linux Mint

By default, rsnapshot included in Ubuntu’s repositories, so you can install it using apt-get command as shown.

# apt-get install rsnapshot

Step 2: Setting up SSH Password-less Login

To backup remote Linux servers, your rsnapshot backup server will be able to connect through SSH without a password. To accomplish this, you will need to create an SSH public and private keys to authenticate on the rsnapshot server. Please follow below link to generate a public and private keys on your rsnapshot backup server.

  1. Create SSH Passwordless Login Using SSH Keygen

Step 3: Configuring Rsnapshot

Now you will need to edit and add some parameters to rsnapshot configuration file. Open rsnapshot.conf file with vi or nano editor.

# vi /etc/rsnapshot.conf

Next create a backup directory, where you want to store all your backups. In my case my backup directory location is “/data/backup/”. Search for and edit the following parameter to set the backup location.

snapshot_root			 /data/backup/

Also uncomment the “cmd_ssh” line to allow to take remote backups over SSH. To uncomment the line remove the “#” in-front of the following line so that rsnapshot can securely transfer your data to a backup server.

cmd_ssh			/usr/bin/ssh

Next, you need to decide how many old backups you would like to keep, because rsnapshot had no idea how often you want to take snapshots. You need to specify how much data to save, add intervals to keep, and how many of each.

Well, the default settings are good enough, but still I would like you to enable “monthly” interval so that you could also have longer term backups in place. Please edit this section to look similar to below settings.

#########################################
#           BACKUP INTERVALS            #
# Must be unique and in ascending order #
# i.e. hourly, daily, weekly, etc.      #
#########################################

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

One more thing you need to edit is “ssh_args” variable. If you have changed the default SSH Port (22) to something else, you need to specify that port number of your remote backing up server.

ssh_args		-p 7851

Finally, add your local and remote backup directories that you want to backup.

Backup Local Directories

If you’ve decided to backup your directories locally to the same machine, the backup entry would look like this. For example, I am taking backup of my /tecmint and /etc directories.

backup		/tecmint/		localhost/
backup		/etc/			localhost/
Backup Remote Directories

If you would like to backup up a remote server directories, then you need to tell the rsnapshot where the server is and which directories you want to backup. Here I am taking a backup of my remote server “/home” directory under “/data/backup” directory on rsnapshot server.

backup		 [email protected]:/home/ 		/data/backup/

Read Also:

  1. How to Backup/Sync Directories Using Rsync (Remote Sync) Tool
  2. How to Transfer Files/Folders Using SCP Command
Exclude Files and Directories

Here, I’m going to exclude everything, and then only specifically define what I want to backed up. To do this, you need to create a exclude file.

# vi /data/backup/tecmint.exclude

First get the list of directories that you want to backed up and add ( – * ) to exclude everything else. This will only backup what you listed in the file. My exclude file looks like similar to below.

+ /boot
+ /data
+ /tecmint
+ /etc
+ /home
+ /opt
+ /root
+ /usr
- /usr/*
- /var/cache
+ /var
- /*

Using exclude file option can be very tricky due to use of rsync recursion. So, my above example may not be what you are looking. Next add the exclude file to rsnapshot.conf file.

exclude_file    /data/backup/tecmint.exclude

Finally, you are almost finished with the initial configuration. Save the “/etc/rsnapshot.conf” configuration file before moving further. There are many options to explain, but here is my sample configuration file.

config_version  1.2
snapshot_root   /data/backup/
cmd_cp  /bin/cp
cmd_rm  /bin/rm
cmd_rsync       /usr/bin/rsync
cmd_ssh /usr/bin/ssh
cmd_logger      /usr/bin/logger
cmd_du  /usr/bin/du
interval        hourly  6
interval        daily   7
interval        weekly  4
interval        monthly 3
ssh_args	-p 25000
verbose 	2
loglevel        4
logfile /var/log/rsnapshot/
exclude_file    /data/backup/tecmint.exclude
rsync_long_args --delete        --numeric-ids   --delete-excluded
lockfile        /var/run/rsnapshot.pid
backup		/tecmint/		localhost/
backup		/etc/			localhost/
backup		[email protected]:/home/ 		/data/backup/

All the above options and argument explanations are as follows:

  1. config_version 1.2 = Configuration file version
  2. snapshot_root = Backup Destination to store snapshots
  3. cmd_cp = Path to copy command
  4. cmd_rm = Path to remove command
  5. cmd_rsync = Path to rsync
  6. cmd_ssh = Path to SSH
  7. cmd_logger = Path to shell command interface to syslog
  8. cmd_du = Path to disk usage command
  9. interval hourly = How many hourly backups to keep.
  10. interval daily = How many daily backups to keep.
  11. interval weekly = How many weekly backups to keep.
  12. interval monthly = How many monthly backups to keep.
  13. ssh_args = Optional SSH arguments, such as a different port (-p )
  14. verbose = Self-explanatory
  15. loglevel = Self-explanatory
  16. logfile = Path to logfile
  17. exclude_file = Path to the exclude file (will be explained in more detail)
  18. rsync_long_args = Long arguments to pass to rsync
  19. lockfile = Self-explanatory
  20. backup = Full path to what to be backed up followed by relative path of placement.

Step 4: Verify Rsnapshot Configuration

Once you’ve done with your all configuration, its time to verify that everything works as expected. Run the following command to verify that your configuration has the correct syntax.

# rsnapshot configtest

Syntax OK

If everything configured correctly, you will receive a “Syntax OK” message. If you get any error messages, that means you need to correct those errors before running rsnapshot.

Next, do a test run on one of the snapshot to make sure that we are generating correct results. We take the “hourly” parameter to do a test run using -t (test) argument. This below command will display a verbose list of the things it will do, without actually doing them.

# rsnapshot -t hourly
Sample Output
echo 2028 > /var/run/rsnapshot.pid 
mkdir -m 0700 -p /data/backup/ 
mkdir -m 0755 -p /data/backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /home \
    /backup/hourly.0/localhost/ 
mkdir -m 0755 -p /backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc \
    /backup/hourly.0/localhost/ 
mkdir -m 0755 -p /data/backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /usr/local /data/backup/hourly.0/localhost/ 
touch /data/backup/hourly.0/

Note: The above command tells rsnapshot to create an “hourly” backup. It actually prints out the commands that it will perform when we execute it really.

Step 5: Running Rsnapshot Manually

After verifying your results, you can remove the “-t” option to run the command really.

# rsnapshot hourly

The above command will run the backup script with all the configuration that we added in the rsnapshot.conf file and creates a “backup” directory and then creates the directory structure under it that organizes our files. After running above command, you can verify the results by going to the backup directory and list the directory structure using ls -l command as shown.

# cd /data/backup
# ls -l

total 4
drwxr-xr-x 3 root root 4096 Oct 28 09:11 hourly.0

Step 6: Automating the Process

To automate the process, you need to schedule rsnapshot to be run at certain intervals from Cron. By default, rsnapshot comes with cron file under “/etc/cron.d/rsnapshot“, if it’s doesn’t exists create one and add the following lines to it.

By default rules are commented, so you need to remove the “#” from in front of the scheduling section to enable these values.

# 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

Let me explain exactly, what above cron rules does:

  1. Runs every 4 hours and creates an hourly directory under /backup directory.
  2. Runs daily at 3:30am and create a daily directory under /backup directory.
  3. Runs weekly on every Monday at 3:00am and create a weekly directory under /backup directory.
  4. Runs every monthly at 2:30am and create a monthly directory under /backup directory.

To better understand on how cron rules works, I suggest you read our article that describes.

  1. 11 Cron Scheduling Examples

Step 7: Rsnapshot Reports

The rsnapshot provides a nifty small reporting Perl script that sends you an email alert with all the details as to what occurred during your data backup. To setup this script, you need to copy the script somewhere under “/usr/local/bin” and make it executable.

# cp /usr/share/doc/rsnapshot-1.3.1/utils/rsnapreport.pl /usr/local/bin
# chmod +x /usr/local/bin/rsnapreport.pl

Next, add “–stats” parameter in your “rsnapshot.conf” file to the rsync’s long arguments section.

vi /etc/rsnapshot.conf
rsync_long_args --stats	--delete        --numeric-ids   --delete-excluded

Now edit the crontab rules that were added earlier and call the rsnapreport.pl script to pass the reports to specified email address.

# 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 2>&1  | \/usr/local/bin/rsnapreport.pl | mail -s "Hourly Backup" [email protected]
30     3     * * *    root    /usr/bin/rsnapshot daily 2>&1  | \/usr/local/bin/rsnapreport.pl | mail -s "Daily Backup" [email protected]
0      3     * * 1    root    /usr/bin/rsnapshot weekly 2>&1  | \/usr/local/bin/rsnapreport.pl | mail -s "Weekly Backup" [email protected]
30     2     1 * *    root    /usr/bin/rsnapshot monthly 2>&1  | \/usr/local/bin/rsnapreport.pl | mail -s "Montly Backup" [email protected]

Once you’ve added above entries correctly, you will get a report to your e-mail address similar to below.

SOURCE           TOTAL FILES	FILES TRANS	TOTAL MB    MB TRANS   LIST GEN TIME  FILE XFER TIME
--------------------------------------------------------------------------------------------------------
localhost/          185734	   11853   	 2889.45    6179.18    40.661 second   0.000 seconds

Reference Links

  1. rsnapshot homepage

That’s it for now, if any problems occur during installation do drop me a comment. Till then stay tuned to TecMint for more interesting articles on the Open source world.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

28 Comments

Leave a Reply
  1. The ignore file seems not to work. I excluded some files and directories but rsnapshot copies these anyways. I tried it with + and - and without and changed the position… file is readable, with no errors.

    Reply
  2. Hi Ravi,

    Thanks for the article,

    Actually I have configured rsnapreport.pl in cron but I am getting null report of hourly. Could you please help me out why I am getting null mails instead of report. ?

    Reply
    • @Roshan,

      Have you checked backups? does backups happening? please confirm first and then check the cron entries correctly..

      Reply
  3. Using rsnapshot for about three years now. Nice introduction to rsnapshot. Thanks!
    Now I believe there’s not a word on how to recover / restore (as often on backup articles)…
    Here are tools I’d recommand you to check out:
    – I haven’t found rsnapreport.pl but there’s rsnapstat which is a fork in python [1].Gives a syntax error atm but I hope will be fixed
    – rsnapshot-once by Philipp C Heckel is freakin cool to insure consistent incremental backups [2] is the best wrapper

    @JJMacey, SystemBack does what you want. You might want to take a look, it’s a freaking kiss tool, that’s even able to backup a system on a live CD!
    Oh and Rtfazeberdee’s right on ksnapshot.

    [1] https://github.com/rmiceli/rsnapstats
    [2] https://github.com/binwiederhier/rsnapshot-once

    Reply
  4. A rather opening howto. Now it would be nice to include a few lines on **How to Restore**, might comes handy in a backup strategy ;) Especially as the rsnapshot official howto is very sparce in this field.

    Reply
  5. There’s a simple web app for backups with rsnapshot: ElkarBackup -www.elkarbackup.prg-.
    And it runs on any Debian/Ubuntu system. Raspberry PI included!

    Reply
  6. I’ve been using rsnapshot since Rubel’s original article 10+ years ago. In the current incarnation the syntax of paths is easy to screw up and so testing a configuration file without actually transferring files and with verbosity is essential for debugging the configuration. Attention to detail for setting up the cron job is similarly essential. When I get these worked out I then back up both so that it is relatively easy to reinstall when things disappear after a system upgrade.

    The automatic nature of rsnapshot is fantastic but it also can lead to complacency. It is wise to check the backups from time to time to make sure they’re still being made as planned. This is especially true after a system upgrade or the transfer of a system to new hardware. Particular attention made to confirming passwordless ssh login to target systems is still in effect when changes are made. It’s really annoying to find out that important data is not backed up because some months earlier an upgrade made ssh passwords obsolete.

    Reply
  7. All works fine, but the reports dont delivered to my email work, Why? I have to setup the email server function in this server? or where I put the pop and smtp servers to this work fine?
    Thanks

    Reply
  8. when i try to backup locally it works fine. but when i try to back up a remote server it gives me an error message.

    [kavinda@SVR1 ~]$ rsnapshot configtest
    —————————————————————————-
    rsnapshot encountered an error! The program was invoked with these options:
    /usr/bin/rsnapshot configtest
    —————————————————————————-
    ERROR: /etc/rsnapshot.conf on line 222:
    ERROR: backup [email protected].1.6:/home/ /data/backup/ – Backup destination \
    /data/backup/ must be a local, relative path
    ERROR: ———————————————————————
    ERROR: Errors were found in /etc/rsnapshot.conf,
    ERROR: rsnapshot can not continue. If you think an entry looks right, make
    ERROR: sure you don’t have spaces where only tabs should be.

    My /etc/rsnapshot.conf
    backup [email protected].1.6:/home/ /data/backup/

    Reply
  9. It looks like snapshot_root is a (temporary?) local directory where the backup will be stored. I can’t have that, since my server has used, say, 25GB of storage on a 40GB disk. NFS or USB mount is not an option. Replacing the disk is also not an option (besides, I don’t want to always have double the space I actually need/use). It’s a virtual server at a provider, so everything costs money. I won’t open up NFS over the internet, and I can’t reach the server to plug in anything.

    SSH to home is the only option for me. Which is there, but for what exactly? Can rsnapshot not “simply” backup directly via SSH without the use of a local directory? rsync can do this, but I don’t see how rsnapshot would.

    Another thing, how to restore?… Say I have to rebuild my server, Ubuntu is freshly installed. Then what?

    Reply
      • thank you very much for posting such a nice and useful concepts for linux administrators..

        my problem is ..

        i have configured rnapshot…..intervals i have kept is daily.0

        according to rsnapshot.conf it should take everyday backup at night 2 30 pm..

        and it should be incremental backup..

        every day it is taking backup..tats ok

        but for me problem is it is consuming lot of bandwidth for my internet..

        tat means my internet consuming will takes place every sunday..
        tat is every sunday it is taking or consuming 8 gb of internet ..

        plz help me on this..

        Reply
  10. If I am daily backing up a local directory, say /backup/ into rsnapshot directory /backup_r/, is there any way of NOT requiring /backup_r/daily.0/backup/ to be a copy, but rather a set of links to /backup, thereby saving disk space?

    Thanks.

    Lester

    Reply
  11. the claim “taking up the space of one single full backup plus differences” i do not think is true . All my 6 hoyrly backups have the same size, 13 Mb each.
    George Bouras

    Reply
    • No they don’t
      that’s what you see because of hard links
      actual physical space is full backups + changed files (not mere diffs)

      try creating hard links with ln and see if by yourself

      Reply
  12. Ravi,

    Thanks for the article!

    Normally to back-up files, before I wipe my H-P Spectre Ultrabook SSD, I use “ksnapshot”. The back-ups are ported to a USB drive. Obviously, “rsnapshot” has a few differences / advantages.

    Recently, I have been thinking of buying a back-up 128Gb SSD to use in case of disaster. Basically, I want to copy EVERYTHING to this new SSD, and use it to completely restore my system.

    Do you have an app preference to do this, and make my new SSD BOOTABLE?

    TIA for your response.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.