How to create backups with Rsnapshot on Linux

Rsnapshot is a free and open source utility which works as a wrapper around Rsync to perform incremental snapshots of local or remote systems via SSH. Hard links are used to save space when backing up unchanged files, to preserve space while still displaying complete filesystems to the user. In this tutorial, we learn how to install and configure Rsnapshot on some of the most used Linux distributions.

In this tutorial you will learn:

  • How to install rsnapshot on some of the most used Linux distributions
  • How to configure rsnapshots to backup local and remote machines via SSH
how to create incremental backups with rsnapshot on linux
How to create incremental backups with rsnapshot on Linux

 

Category Requirements, Conventions or Software Version Used
System Distribution-agnostic
Software rsnapshot
Other Root privileges
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Installation

Being rsnapshot a free and open source utility, it is available in the official or trusted third party repositories of all the most used Linux distributions. To install the utility on Fedora, for example, all we have to do is to launch our favorite terminal emulator, and issue the following command:

$ sudo dnf install rsnapshot



We can install Rsnapshot on RHEL (Red Hat Enterprise Linux) and its clones, using the same command; on those distributions, however, we need to add the EPEL repository as a software source beforehand.

To perform the installation on Debian, Ubuntu, and other Debian-based distributions, we use the apt package manager:

$ sudo apt install rsnapshot

Rsnapshot is also available in the Archlinux “extra” repository. We can install it using pacman:

$ sudo pacman -S rsnapshot

Once we installed Rsnapshot in our favorite Linux distribution, we can use it to create incremental backups. Let’s see how.

Rsnapshot configuration

To configure rsnapshot, we edit its configuration file: /etc/rsnapshot.conf. As stated in the big warning at the beginning of the file, only tabs must be used between elements; the use of spaces would produce syntax errors:

#################################################
#                                               #
# PLEASE BE AWARE OF THE FOLLOWING RULE:        #
#                                               #
# This file requires tabs between elements      #
#                                               #
#################################################

The first directive we want to define in the file is snapshot_root. As its name suggests, it is used to specify the directory where all snapshots should be saved. In the following example, we use the /snapshotsdirectory as our snapshots root:

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

If the directory doesn’t exist, it is automatically created when the first backup is performed, unless the no_create_root directive is enabled (default value is normally 0):

# If no_create_root is enabled, rsnapshot will not automatically create the
# snapshot_root directory. This is particularly useful if you are backing
# up to removable media, such as a FireWire or USB drive.
#
#no_create_root 1

Pointing Rsnapshot to the rsync path

Rsnapshot uses rsync to perform backups, therefore we must specify the location of the rsync binary via the cmd_rsync directive. This is set to /usr/bin/rsync by default, and requires no changes, unless we installed rsync in some other location:

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

Establishing a retention policy

Before staring to create our backups, we need to establish a retention policy: we need to define the different backup types, and specify how many snapshots of a specific type should be retained. To establish the retention policy we use the retain keyword followed by the name of a backup “type” (name is completely arbitrary), and by an integer which specifies how many backups of that type should be retained. Here is the default configuration on an Ubuntu system:

#########################################
# 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



By default, “alpha”, “beta”, and “gamma” are used as identifiers, however, those names are not really indicative. We may want to use more friendly and meaningful ones, for example:

retain daily 6
retain weekly 7 
retain monthly 4

Again, remember that names are completely arbitrary, and Rsnapshot doesn’t know what they mean. We specify a backup type when we invoke rsnapshot: all it knows is how many backups of a specific type it should retain.

Specifying what to backup

To specify which files and directories should be included in snapshots, we use the backup directive, and pass the path we want to include, and the host it is located on. Here is a typical configuration, which includes the /home/, /etc/ and /usr/local/ directories on localhost as part of the backups:

backup /home/      localhost/
backup /etc/       localhost/
backup /usr/local/ localhost/

Excluding and including files from backups

Just as when using rsync directly, we may want to exclude or explicitly include specific files from our backups. To define include and exclude patterns when using rsnapshot, we use the include and exclude directives, and specify one pattern per line:

exclude <exclude-pattern>

Similarly, to specify an include pattern, we would write:

include <include-pattern>

As an alternative strategy we may write include and exclude patterns in dedicated files, and then pass the path of those files as values of the include_file and exclude_file directives:

include_file /path/to/include/file
exclude_file /path/to/exclude/file

Verifying Rsnapshot configuration

Once we modified the Rsnapshot configuration file to suite our needs, we may want to check our configuration is syntactically valid. We can do this by using the configtest command:

$ sudo rsnapshot configtest



If everything goes as expected, the command should return the following message:

Syntax OK

Once again, notice that only tabs should be used in the rsnapshot configuration file. Spaces are not allowed: their usage will be considered as a syntax error.

Testing and creating our first backup

To make Rsnapshot print the command it would run when performing a backup, without actually performing it, we can use the -t option, as in the example below:

$ sudo rsnapshot -t daily

The command should return an output similar to the one below:

echo 6080 > /var/run/rsnapshot.pid 
mkdir -m 0755 -p /snapshots/daily.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /home/ /snapshots/daily.0/localhost/ 
mkdir -m 0755 -p /snapshots/daily.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc/ \
    /snapshots/daily.0/localhost/ 
mkdir -m 0755 -p /snapshots/daily.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /usr/local/ /snapshots/daily.0/localhost/ 
touch /snapshots/daily.0/

To actually create our first backup, we just omit the -t option, so, to create our first daily backup, we would run:

$ sudo rsnapshot daily

Once the backup is complete, a directory related to our first daily backup will be created under the snapshots root:

/snapshots/
└── daily.0
    └── localhost
        ├── etc
        ├── home
        └── usr

Let’s now see what would happen if we were to create our second daily backup:

$ sudo rsnapshot -t daily

The command returns the following output:

bin/cp -al /snapshots/daily.0 /snapshots/daily.1 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /home/ /snapshots/daily.0/localhost/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc/ \
    /snapshots/daily.0/localhost/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /usr/local/ /snapshots/daily.0/localhost/ 
touch /snapshots/daily.0/

As you can see, first the daily.0 directory backup would be copied as daily.1, by running cp with the -a and -l options: the former is needed to preserve all files permissions and ownerships, while the latter makes so that files are hard linked instead of copied, in order to preserve space. The new backup will use the original daily.0 directory as a target, therefore it will always host be the most recent snapshot.

Creating a backup of a remote machine

We can use Rsnapshot to perform incremental backups of remote machines via SSH, implementing a “pull” backup strategy: instead of clients pushing files to the backup server, the server connects to the clients and performs the backups “pulling” files from them.

The first thing we need to do, in order to backup a remote machine, is to ensure the cmd_ssh directive exists and is uncommented in the rsnapshot configuration file. The value of the directive is the path of the ssh client executable, which normally is installed as /usr/bin/ssh:

cmd_ssh /usr/bin/ssh

Now, suppose we want to backup the content of the /etc directory of a remote machine with IP 192.168.122.49, logging in as the “ubuntu” user. Here is what we would write in the rsnapshot configuration file:

backup ubuntu@192.168.122.49:/etc client0

Where “client0” is just the identifier of the machine (it can be anything). For the backup to be successful, a working SSH server should be up and running on the target, and the public key we want to use for the connection, must already been authorized.


Conclusions

Rsnapshot is a free and open source utility which works as a wrapper around Rsync to perform incremental backups. In this tutorial, we learned how to install and configure rsnapshot on some of the most used Linux distributions. We saw how to specify the directories we want to backup on local and remote machines, how to establish a retention policy, and how to specify where snapshots should be saved.



Comments and Discussions
Linux Forum