This guide explains what Rsnapshot is, how to install Rsnapshot in Linux, and how to use Rsnapshot in Linux to set up a backup server.

1. Introduction

Rsnapshot is an RSync-based file system snapshot utility for Linux and Unix-like operating systems that allows you to easily create periodic snapshots of local and remote machines over SSH.

The benefit of RSnapshot is that it uses hard links as widely as possible to significantly reduce the disk space required.

Because RSnapshot only holds a fixed number of snapshots, the amount of disk space used does not continue to grow.

Rnapshot is a free open source backup application that supports incremental backup, written entirely in Perl, and should run on most Unix-like systems.

Without further ado, let’s look at how to install, configure, and set up a backup server using Rsnapshot in Linux.

2. Install Rsnapshot in Linux

Rsnapshot is packaged for many Linux distributions.

Install Rsnaphot in Alpine Linux:

$ sudo apk add rsnapshot rsync
Copy the code

Installing Rsnapshot is Arch Linux, Endeavour ouros, and Manjaro Linux:

$ sudo pacman -S rsnapshot rsync
Copy the code

Install Rsnapshot on Debian, Ubuntu, Linux Mint, Pop_OS! :

$ sudo apt install rsnapshot rsync
Copy the code

Install Rsnapshot on CentOS, Fedora, RHEL, AlmaLinux, Rocky Linux.

Rsnapshot is not available in the default repositories of enterprise operating systems such as CentOS, RHEL, AlmaLinux, and Rocky Linux. You need to enable the [EPEL] repository to install Rsnapshot.

$ sudo dnf install epel-release
Copy the code

Then, install Rsnapshot using the command:

$ sudo dnf install rsnapshot rsync
Copy the code

Install Rsnapshot in openSUSE:

$ sudo zypper install rsnapshot rsync
Copy the code

3. In Linux, use Rsnapshot to set the backup server

For the purposes of this guide, I will use two test systems.

One is a backup server running AlmaLinux 8, and the other acts as a client system running Fedora 34 desktops.

Details about backup servers and clients are as follows:

Backup server:
  • Operating system: AlmaLinux 8 64-bit minimum system
  • IP address:192.168.122.25/24
  • Rsnapshot root directory:/rsnapbackup
  • Backup directory:/home/ostechnix/data/

Client:

  • Operating system: Fedora Workstation 34
  • IP address:192.168.225.37/24
  • Backup directory:/home/sk/data/

3.1. Configure password-free SSH authentication on the backup server

To back up the files of the remote client system over SSH, you need to set up password-free SSH authentication for the remote client system, so the backup will automatically connect to the client system without the password and back up the client data.

Log in to root as a user and create an SSH key pair on our backup server system.

# ssh-keygen
Copy the code

Do not enter any passwords because we want these systems to be able to connect to each other without any user intervention:

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: SHA256:k/yIpwuk2lqEVxW4O0/6evTjquxCcGffQU0Kgm1ZvPk root@alma8 The key's randomart image is: +---[RSA 3072]----+ | o.==. o. | | . =o..... | |... +. | |... + o... | |.ooo.o oS. | | o.oo +.E+ | | .o .*.. o . | | +.... ooo | |o.. o===+.. | +----[SHA256]-----+Copy the code

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

# SSH - copy - id [email protected]Copy the code

Replace skand in the above command with the user name and IP address of the remote system, 192.168.225.37 Enter your client system user password to copy the public key file:

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /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 [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "SSH '[email protected]'" and check to make sure that only the key(s) you wanted were added.Copy the code

Here, 192.168.225.37 is the IP address of my remote client system (that is, Fedora Desktop).

Now you can SSH into the client system without any password.

Repeat the above command to copy the SSH public key to all client systems to set up password-free authentication.

Next, we need to configure the backup server.

3.2. Rsnapshot Backup server configuration

Create a root backup directory to hold all backups of the server and client systems.

For the purposes of this guide, I use /rsnapbackup/ as the root backup directory. This is the directory where I want to store all my backups.

You can define a location of your choice to store backups.

Let’s create the Root backup directory on the backup server using the following command:

# mkdir /rsnapbackup
Copy the code

Next we need to edit and update the Root backup directory, the directory to be backed up, and the backup interval /etc/rsnapshot.conf in the default Rsnapshot configuration file.

It is always recommended to back up the default configuration file before editing the configuration in case you need to reconfigure RSnapshot again.

# cp /etc/rsnapshot.conf /etc/rsnapshot.conf.bak
Copy the code

Now, edit the rsnapshot configuration file using the command:

# nano /etc/rsnapshot.conf
Copy the code

The default configuration should work. All you need to do is define the backup directory and backup interval.

Note: Note that you should specify a slash at the end of the directory name in the file. /etc/rsnapshot.conf For example, you must specify the directory name as /rsnapbackup/ instead of /rsnapbackup. In addition, you need to use tabs (not Spaces) between elements/fields.

First, let’s set up the root backup directory, which defines the directory where we want to store the file system backups. In our case, I’ll store the backups in the /rsnapbackup/ directory.

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

Similarly, you should use the TAB key between the snapshot_root element and the backup directory.

Note: Rsnapshot does not support remote snapshot roots over SSH.

Scroll down a bit and make sure the following lines (highlighted 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 [...]Copy the code

Next, we need to define the backup interval:

######################################### # 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 3Copy the code

Retain alpha 6 here means that each time rSnapshot alpha is run, a new snapshot is created, the old snapshot is rotated, and named in the rSnapshot root directory alpha.0, alpha.1… Keep the last six backups in the directory, alpha.5 and then the next time you run the command, the alpha.5 directory will be deleted.

Similarly, every time we call rSnapshot beta, it creates a new snapshot, rotates the old snapshot and retains the last seven backups (delt.0-delt.6).

Note that alpha, Beta, Gamma, and delta are just the names that define backup intervals; you can also define your own intervals.

For example, you can replace the default backup level with some meaningful name of your choice, as follows:

retain  hourly  24
retain  daily   7
retain  weekly  4
retain  monthly 12
Copy the code

When we call it, it creates a new backup in the rnsapshot hourly directory.

/hourly.0/ Each time the command is run, new directories are created. The maximum value is

/hourly.

This is the same for daily, weekly, and monthly backups.

Next, if you want to set up log files for Rsnapshot, uncomment the following lines:

logfile /var/log/rsnapshot
Copy the code

If you change the SSH default port, you need to uncomment this line and mention a valid port number here:

ssh_args -p 2222
Copy the code

Finally, you need to define the directory to back up, find the following instructions in your RSnapshot profile and set the backup directory location.

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

# LOCALHOST
backup /home/ostechnix/data/ myserverbackup/
Copy the code

Here, I will backup/home/ostechnix/data/directory and save them in the contents of/rsnapbackup myserverbackup/directory.

Note: please note that I have no/rsnapbackup/myserverbackup/specified in the configuration of the full path (that is), because we’ve already mentioned the Root backup directory, so we don’t need to specify here Rsnapshot Root directory.

Also, define your remote client system backup location.

# REMOTEHOST backup [email protected]: / home/sk/data/myclientbackup /Copy the code

Here, I will backup my remote client system/home/sk/data/directory and save them/rsnapbackup/myclientbackup/in my backup server directory.

Please note again, I don’t have/rsnapbackup/myclientbackup/specified in the configuration of the full path to the (), because, we have defined the Root backup directory.

Save and close the /etc/rsnapshot.conf file.

After all the changes are made, run the following command to verify that the configuration file is syntactically valid.

$ rsnapshot configtest
Copy the code

If all goes well, you should see the following output.

Syntax OK
Copy the code

3.3. Start backup

Run the following command to manually start the backup using Rsnapshot.

# rsnapshot alpha
Copy the code

The first backup will take anywhere from a few minutes to a few hours depending on the size of the backup, and subsequent backups will be much faster as Rsnapshot only backs up differences.

Similarly, you can start other backup levels:

# rsnapshot beta
Copy the code
# rsnapshot gamma
Copy the code
# rsnapshot daily
Copy the code

3.4. Verify and view backups

Let’s check that the backup is actually stored in the root backup directory of the backup server.

#ls / rsnapbackup /
Copy the code

You should see the following output:

alpha.0
Copy the code

Check the contents of the alpha.0 directory:

#ls /rsnapbackup/alpha.0/
Copy the code

You will see that two directories are automatically created, one for local backup (MyServerBackup) and one for remote system (myClientBackup).

myclientbackup  myserverbackup
Copy the code

To check the client system backup, run:

#ls /rsnapbackup/alpha.0/myclientbackup/
Copy the code

Check server system (local system) backup:

#ls /rsnapbackup/alpha.0/myserverbackup/
Copy the code

Each time you run Rnaspshot, it creates new directories based on the number of reservation levels you configured, such as alpha.0,alpha.1, and so on.

# ls / rsnapbackup / 
alpha.0 alpha.1
Copy the code

The alpha.0 directory will contain the most recent backup.

3.5. Plan backups

You don’t need to run the rsnapshot command every time to create a backup. You just need to define a Cron job to perform the backup job automatically on a regular basis.

To do this, create a new CRon job file for RSnapshot:

# nano /etc/cron.d/rsnapshot
Copy the code

Add the following line:

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

The first line represents 6 alpha snapshots per day (0, 4, 8, 12, 16, and 20 hours), Beta at 11:50 per night and Delta at 10 PM on the first day of each month, you can adjust the time as you wish to save and close the file.

Finished! From now on, Rsnapshot will automatically back up your data at the defined time.

See the man page for more details.

# man rsnapshot
Copy the code

4. Restore the file

Restoring files is easy! If you have sudo or root access to the backup server, you can simply copy the files from the Rsnapshot root, that is, /rsnapbackup/.

Conclusion 5.

Setting up a backup server with Rsnapshot in Linux is not difficult, and the initial configuration may seem difficult and take a little time, but once you set up the Rsnapshot backup server correctly, it will always be out of the box without any user intervention.