Rsnapshot

Updated 29 April 2019

Introduction

Rsnapshot is a backup utility, written in Perl and Rsync-based. It makes periodic backup of filesystems easier. Rsnapshot uses hard links for data deduplication. Backup is a necessary part of system administration, as there is always a risk of data loss, such as hard disk failure, accidental data deletion, malware, or else a file system failure.

Rsnapshot allows you to make several levels of snapshots (alpha, beta, gamma, etc.) and assign any names to them, as well as rotate them, removing obsolete ones. This manual refers to 6 alpha, 7 beta and 4 gamma copies.

To create a new snapshot, the rsnapshot sync command is used, which places all the specified data in the .sync directory in the backup path.

To put a snapshot at the alpha level, use the rsnapshot alpha command. At first start, the '.sync' directorysync will be renamed to alpha.0. On the second run, alpha.0 will be renamed to alpha.1 and the current copy will be saved as alpha.0. Thus, alpha.0 will always contain the latest snapshot. Subsequent snapshots will also be renamed and stored until their number reaches six - in this case, a copy of alpha.5 will be deleted and replaced with alpha.4.

To visualize creation of a snapshot, see the diagram below:

              +---------------------------+
              |           .sync           |
              +---------------------------+
                            |
                -------------
                |
                ∇          -->
              +---+---+---+---+---+---+---+
alpha level | 0 | 1 | 2 | 3 | 4 | 5 | - |
              +---+---+---+---+---+---+---+
                                    |
                ---------------------
                |
                ∇            -->
              +---+---+---+---+---+---+---+---+
beta level  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | - |
              +---+---+---+---+---+---+---+---+
                                        |
                -------------------------
                |
                ∇      -->
              +---+---+---+---+---+
gamma level | 0 | 1 | 2 | 3 | - |
              +---+---+---+---+---+

The beta snapshot is created from the oldest copy of the alpha level (alpha.5) with the rsnapshot beta command.

In other words, alpha.0 derives from the file system, alpha.1 from alpha.0, beta.0 from the latest alpla (alpha.5), beta.1 from beta.0, gamma.0 from the latest beta (beta.6).

Important

If alpha.5 does not exist at the moment rsnapshot beta is executed, while alpha.4 does exist, the beta.0 snapshot will not be created. Nonetheless, the other beta snapshots will be rotated. Therefore, you should not run the subsequent levels more often than the preceding ones.

Installation

First install rsnapshot:

emerge -a app-backup/rsnapshot

Configuration

Make basic settings by editing, if necessary, the backup path and/or the number and levels of snapshots:

/etc/rsnapshot.conf

# path to the local directory where snapshots will be stored
snapshot_root   /var/calculate/snapshots

# levels and number of snapshots
retain  alpha   6
retain  beta    7
retain  gamma   4

Important

Note that fields are Tab -spaced!

Check the list of paths that will not be backuped:

/etc/rsnapshot.d/rsnapshot.filter
- /dev/*
- /lost+found
- /tmp/*
- /usr/portage/*
- /var/lib/layman/*
- /lxc/*/snaps
- /lxc/*/rootfs/dev/*
- /lxc/*/rootfs/tmp/*
- /lxc/*/rootfs/usr/portage/*
- /lxc/*/rootfs/var/lib/layman/*
- /lxc/*/rootfs/var/log/*
- /lxc/**/calculate/packages
- /lxc/**/remote/distfiles
- /packages
- /remote/distfiles
- /snapshots/*
- /var/calculate/snapshots/*

Local snapshots

Path configuration

The example below describes the backup paths (root of the file system and the /var/calculate directory) for the current system via the backup parameter:

/etc/rsnapshot.d/localhost.conf

backup  /               localhost/rootfs/
backup  /var/calculate  localhost/calculate

From now on, paths ~localhost/rootfs~ and ~localhost/calculate~~~ will be created in the storage directory as the filesystem root and the /var/calculate locations, respectively.

Creating a snapshot

To prepare the data to be backuped, run:

rsnapshot sync

If necessary, this command can be interrupted or restarted as many times as you need. After the synchronization is complete, proceed to alpha level rotation:

rsnapshot alpha

Running again rsnaphost sync will prepare a new backup. rsnapshot alpha will perform the rotation and move the synchronized data to level alpha0.

When alpha snapshots have filled the level, you will be able to move the latest snapshot to the beta level instead of deleting it. To do this, run rsnapshot beta before running rsnapshot alpha.

              +---+---+---+---+---+---+
alpha level | 0 | 1 | 2 | 3 | 4 | 5 |
              +---+---+---+---+---+---+
                        alpha.6     |
                ---------------------
                |
                ∇
              +---+---+---+---+---+---+---+---+
beta level  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | - |
              +---+---+---+---+---+---+---+---+

Executing these commands in the reverse order will cause the last alpha copy to be deleted first, then an alpha level of 6 items to be formed, and then the last item to be moved to the beta level, which will reduce the alpha queue by 1.

              +---+---+---+---+---+---+ +---+
alpha level | 0 | 1 | 2 | 3 | 4 |   | | 6 |
              +---+---+---+---+---+---+ +---+
                        alpha.5     |
                ---------------------
                |
                ∇
              +---+---+---+---+---+---+---+---+
beta level  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | - |
              +---+---+---+---+---+---+---+---+

Scheduling snapshots

Add a daily .sync snapshotting procedure to cron. Synchronization may take a while; make sure you schedule sufficient time to complete it (12 hours approx.). Tell the system to make daily alpha and beta copies every Saturday, and gamma copies on the first of each month.

Important

Before you create an alpha copy, you must create a beta, and a gamma one before you create a beta.

As deleting an obsolete copy can take some time, a pause (1 hour) should be added between rotations.

/etc/cron.d/rsnapshot

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/

# run rsnapshot
0 0 * * *   root    /usr/bin/rsnapshot sync &>/dev/null
0 10 1 * *  root    /usr/bin/rsnapshot gamma &>/dev/null
0 11 * * 6  root    /usr/bin/rsnapshot beta &>/dev/null
0 12 * * *  root    /usr/bin/rsnapshot alpha &>/dev/null

Setting up snapshots for a remote node

Backup server configuration

Generate a ssh key for password-less access to the distant host on the backup server:

ssh-keygen -b 4096 -f /root/.ssh/backup.key

Add the key to be used for all clients:

/etc/rsnapshot.conf

rsync_long_args     --delete --numeric-ids --delete-excluded --rsh="ssh -i /root/.ssh/backup.key

Specify the locations for which snapshots will be created:

/etc/rsnapshot.d/example.conf

backup  example.org::rootfs     example/rootfs/
backup  example.org::calculate  example/calculate/

where:

  • example.org is the network name of the host,
  • rootfs and calculate are the locations to be described in the configuration file of the rsyncd client

Client configuration

Create a file on the remote computer to be snapshot, containing the description of the directories to be synchronized. Assign new names to them, rootfs and calculate:

/etc/rsyncd-backup.conf

uid = root
gid = root
read only = true

[rootfs]
    path = /

[calculate]
    path = /var/calculate

Copy the previously generated server public key to the remote host, adding rsync at the beginning of the line:

command="rsync --config=/etc/rsyncd-backup.conf --server --daemon .",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding

You should obtain something like that:

/root/.ssh/authorized_keys

command="rsync --config=/etc/rsyncd-backup.conf --server --daemon .",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCohMEmkUVd72YI+gRXyCSL2YWQGg3tmy1SiKJ4i3enTpVf7oY+SDSk/WDJUkBisOh1VGWbtylCqez6K4/rkIxtQmog/L6KmnVEv/MKPXCfRb0SletaLLRhoLB9yjPJltzS+q/983yBIJKYO9jBJQbDYYdqPIuGidQ6xjX+OuJoCL/akKoaQxBm+92k+hp9Yi7RxGX12Y0WMD6XCZEZcktjGX/FyzIaRV9Ikt+0uHRZBo9lwEtEWEXY3coYauBr9A9XbtXPy4sGDXJsePKi7lxGi880IVgZgABMgPo9B8hVPVBplvKa5tUaj5C7s+lrZ0VI3Nr7i3c4cVwSnOcFlg9Z root@backup

Distributing nodes to different snapshot repositories

If computers for which snapshots are made are accessed over a low-speed connection, it is reasonable to split the storage into multiple backups.

Create an rsnapshot configuration file for local computers:

/etc/rsnapshot.local.conf

include_conf    `/bin/cat /etc/rsnapshot.conf`
snapshot_root   /var/calculate/snapshots/local
lockfile        /var/run/rsnapshot.local.pid
include_conf    `/bin/cat /etc/rsnapshot.d/local.*.zone`
include_conf    `/bin/cat /etc/rsnapshot.d/localhost.zone`

Important

Note that fields are Tab -spaced!

These settings refer to the common ones described in / etc / rsnapshot.conf , and replace the values for the lock file and the root of the snapshot storage volume. /etc/rsnapshot.d provides all zone files starting with local and localhost.zone.

Create a rsnapshot config file for remote hosts:

/etc/rsnapshot.remote.conf

include_conf    `/bin/cat /etc/rsnapshot.conf`
snapshot_root   /var/calculate/snapshots/remote
lockfile        /var/run/rsnapshot.remote.pid
include_conf    `/bin/cat /etc/rsnapshot.d/remote.*.zone`

Unlike the previous configuration file, this one provides only zone file names starting with remote.

Local snapshots will be saved at /var/calculate/snapshots/local, and remote snapshots at /var/calculate/snapshots/remote

Specify the locations for which snapshots will be created:

/etc/rsnapshot.conf.d/local.example.org.zone

backup  example.org::rootfs     example.org/rootfs/
backup  example.org::calculate  example.org/calculate/

/etc/rsnapshot.conf.d/remote.example.org.zone

backup  remote.example.org::rootfs      remote.example.org/rootfs/
backup  remote.example.org::calculate   remote.example.org/calculate/