Rsnapshot: A Powerful Backup Tool Based on Rsync

Rsnapshot is an open-source local/remote filesystem backup utility written in Perl, which leverages the power of Rsync and SSH to create scheduled incremental backups of Linux/Unix filesystems.

Rsnapshot only takes up the space of a single full backup plus the differences, allowing you to store backups on a local drive, external USB stick, NFS-mounted drive, or over the network to another machine via SSH.

In this article, we’ll walk you through the process of installing, setting up, and using Rsnapshot to create hourly, daily, weekly, and monthly local backups, as well as remote backups.

Installing Rsnapshot Backup in Linux

First, you need to install and enable the EPEL (Extra Packages for Enterprise Linux) repository, as Rsnapshot is not included by default in RHEL-based distributions.

sudo dnf install epel-release
sudo dnf install rsnapshot

On Ubuntu-based distributions, rsnapshot is available in the default repositories, so you can install it using the apt package manager:

sudo apt install rsnapshot

Once installed, you can verify the installation by checking the version.

rsnapshot -v

Setting Up SSH Passwordless Login

To back up remote Linux servers, you need to configure SSH for passwordless login between the backup server and the remote machine.

Generate SSH public/private key pairs by following these commands:

ssh-keygen -t rsa

Next, copy the public key to the remote server:

ssh-copy-id user@remote-server

Configuring Rsnapshot in Linux

The configuration file for rsnapshot is located in /etc/rsnapshot.conf, open this configuration file with a text editor like nano or vim:

sudo nano /etc/rsnapshot.conf
OR
sudo vi /etc/rsnapshot.conf

Some of the important settings you’ll need to configure include:

Snapshot Backup Directory

To set the directory where your backups will be stored, you need to edit the snapshot_root line in the configuration file.

snapshot_root   /data/backup/

Set Backup Intervals

Rsnapshot supports multiple backup intervals like daily, weekly, and monthly. You can set how often you want your backups by uncommenting the following lines:

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

Set Backup Directories

To back up local directories, add the directory paths.

backup    /home/     localhost/
backup    /etc/      localhost/

For remote backups, specify the remote server and directory to back up, like so:

backup    root@remote-server:/home/     /data/backup/

Enable Remote Backups

To enable remote backups over SSH, uncomment the cmd_ssh line:

cmd_ssh    /usr/bin/ssh

If you have changed the default SSH port, update the ssh_args line to reflect the custom port (e.g., port 7851):

ssh_args    -p 7851

Exclude Files and Directories

You can exclude certain files and directories from being backed up by creating an exclude file.

sudo nano /data/backup/exclude.txt

Add exclusions in the following format:

- /var/cache
- /tmp
+ /etc
+ /home

In your rsnapshot.conf file, reference the exclude file:

exclude_file    /data/backup/exclude.txt

After configuring Rsnapshot, verify that your setup is correct by running:

sudo rsnapshot configtest

You should see the message “Syntax OK“. If there are any errors, fix them before proceeding.

Finally, you can run Rsnapshot manually using the command for the interval you want to back up:

sudo rsnapshot hourly

Automating Rsnapshot with Cron

To automate the backup process, configure cron jobs to run Rsnapshot at specific intervals by adding the following to your /etc/cron.d/rsnapshot file:

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

Setting Up Rsnapshot Reports

Rsnapshot includes a script to send backup reports via email. To set it up, copy the script and make it executable:

sudo cp /usr/share/doc/rsnapshot/utils/rsnapreport.pl /usr/local/bin/
sudo chmod +x /usr/local/bin/rsnapreport.pl

Now, edit your rsnapshot.conf file and add the --stats flag to the rsync_long_args section:

rsync_long_args --stats --delete --numeric-ids --delete-excluded

Then, add the report to your cron job to email the report:

0 */4 * * * root /usr/bin/rsnapshot hourly 2>&1 | /usr/local/bin/rsnapreport.pl | mail -s "Hourly Backup Report" [email protected]

Monitoring Rsnapshot Backups

You can monitor your backups by checking the log files. By default, Rsnapshot logs backup activities in /var/log/rsnapshot.log.

cat /var/log/rsnapshot.log
Conclusion

Rsnapshot is an excellent choice for managing backups on Linux systems. With its efficient use of rsync, you can easily back up your files locally and remotely.

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.

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.