Skip to main content

How to Install and Use Rsnapshot (Making Server's Backups)

Updated over a week ago

Introduction

rsnapshot is a filesystem snapshot utility based on rsync. rsnapshot makes it easy to make periodic snapshots of local machines and remote machines over SSH. The code makes extensive use of hard links whenever possible to significantly reduce the disk space required.

This how-to will show you how to install and set up rsnapshot. We are using the Ubuntu 22.04 distribution; however, you should be able to use it for all Debian and Ubuntu 24.04 distributions as well.

1. Updating the system:

sudo apt-get update
sudo apt-get upgrade

2. Installing and configuring rsnapshot:

sudo apt-get install rsnapshot

To configure rsnapshot, we edit its configuration file: /etc/rsnapshot.conf. As is warned at the beginning of the file, only tabs can be used between elements:

Firstly, we need to choose where our backups will be stored.

Set snapshot_root to the path where you want to keep the backups:

snapshot_root /backup/

Note: Each directory needs a trailing slash (/) to be read correctly.
Note: If this directory does not already exist, rsnapshot will create it when it runs.

Also, uncomment (remove the “#” from before the line) lines:

cmd_ssh /usr/bin/ssh
cmd_du /usr/bin/du

Next, we need to consider how many old backups we would like to keep. In the following example, after 6 "hourly" snapshots, the oldest "hourly" is deleted. The top entry (in this case "hourly") is copied from the source, while the remaining entries simply link to the latest snapshot from one level above.

In other words, to keep six backups a day (four-hour interval) [alpha], seven daily backups (one week) [beta], and four weekly backups (one month) [gamma], specify:

retain alpha 6
retain beta 7
retain gamma 4

It looks like in the file:

Now, we need to specify what we want to back up. Suppose you are backing up locally to the same machine. In that case, this is as easy as selecting the directories you want to save and following it with “localhost/”, which will create a subdirectory in the snapshot_root that you set up earlier:

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

To include or exclude files from backup, use the commands below:

include <include-pattern>
exclude <exclude-pattern>

An example of the command line:

include_file /path/to/the/file 
exclude_file /path/to/the/file

If everything is set up, just save and close nano: CTRL + O and CTRL + X.

3. Verifying the rsnapshot configuration

To test that everything works as expected, use:

sudo rsnapshot configtest

If the configuration of the file is correct, you will get an output:

Syntax OK

If you got this output, you can perform a test of the snapshot to make sure we are producing the expected results:

sudo rsnapshot -t alpha

If everything seems right, you can try to set up backup for the first time, just remove "-t" from the previous command:

sudo rsnapshot alpha

4. Scheduling

Finally, we can schedule it to run at specific intervals. We will use cron to make this happen. rsnapshot includes a default cron file that we can edit to achieve what we want:

nano /etc/cron.d/rsnapshot

Output:

These settings will run and add a snapshot to the “alpha” directory within our “/backup/” directory every four hours, add a beta (daily) snapshot every day at 3:30, and add a gamma (weekly) snapshot every Monday at 3:00.


Conclusion

That's it! We have just configured our server to automatically create filesystem backups and save them to either the same server or a remote server.

Did this answer your question?