Automating Backups with Rsnapshot on Linux

By Raman Kumar

Updated on Aug 09, 2024

In this tutorial, we'll learn automating backups with Rsnapshot on Linux. We shall configure remote server, as soon as rsnapshot takes the backup of the directory, it will copy to remote server using rsync automatically.

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 greatly reduce the disk space required. It allows you to efficiently backup and rotate backups, creating hourly, daily, weekly, and monthly backups of your system without requiring excessive disk space.

Prerequisites

  • A Linux server with root or sudo access.
  • A Ubuntu, AlmaLinux, or Linux based OS installed dedicated server or KVM VPS
  • Basic knowledge of Linux command-line operations.
  • Sufficient disk space for storing backups.


Automating Backups with Rsnapshot on Linux

Step 1: Installing Rsnapshot

First, you'll need to install Rsnapshot. It's available in most Linux distributions' package managers.

On Debian/Ubuntu:

sudo apt update
sudo apt install rsnapshot

On Red Hat/CentOS/AlmaLinux:

sudo yum install rsnapshot

Step 2: Configuring Rsnapshot

The main configuration file for Rsnapshot is located at /etc/rsnapshot.conf. Before editing the configuration file, it's a good idea to create a backup of the original:

sudo cp /etc/rsnapshot.conf /etc/rsnapshot.conf.bak

Now, open the configuration file in your preferred text editor:

sudo nano /etc/rsnapshot.conf

Here are the key configuration options you'll need to set:

1. Snapshot Root Directory

This is where Rsnapshot will store the backups. Modify the following line to specify your backup directory:

snapshot_root   /backup/

Replace /backup/ with the directory where you want to store your backups.

2. Backup Intervals

Rsnapshot allows you to define multiple backup intervals (alpha, beta, gamma, delta). Uncomment and adjust these lines according to your needs:

retain  alpha   6
retain  beta    7
retain  gamma   4
retain  delta   3

These settings will retain:

  • 6 hourly backups
  • 7 daily backups
  • 4 weekly backups
  • 3 monthly backups

3. Backup Points

Define the directories you want to back up. For example:

backup  /home/   localhost/
backup  /etc/    localhost/
backup  /var/www/    localhost/

This configuration will back up the /home, /etc, and /var/www directories. This is the example directories. You should replace those directories with your directories that you want to take backup.

Step 3: Testing Rsnapshot Configuration

Before scheduling backups, it's important to test the Rsnapshot configuration to ensure everything is set up correctly.

Run the following command to perform a dry run:

sudo rsnapshot configtest

If the output says Syntax OK, your configuration is correct. Now, perform a manual backup to test the setup:

sudo rsnapshot alpha

Check the backup directory (/backup/ in this example) to ensure that the backup was successful.

Step 4: Automating Backups with Cron

To automate the backup process, you'll need to set up cron jobs that run Rsnapshot at the specified intervals.

Edit the crontab file:

sudo crontab -e

Add the following lines to schedule the backups:

0 */4 * * * /usr/bin/rsnapshot alpha
30 3 * * * /usr/bin/rsnapshot beta
0 2 * * 7 /usr/bin/rsnapshot gamma
30 1 1 * * /usr/bin/rsnapshot delta

These cron jobs will:

  • Run the hourly backup every 4 hours.
  • Run the daily backup at 3:30 AM.
  • Run the weekly backup at 2:00 AM every Sunday.
  • Run the monthly backup at 1:30 AM on the first day of the month.

Step 5: Configure Remote Server For Backup

Ensure you have SSH access to the remote server. If you haven't already, generate SSH keys on your local machine and copy the public key to the remote server to enable passwordless login.

Generate SSH Key Pair (if needed):

ssh-keygen -t rsa -b 4096

Copy the SSH Key to the Remote Server:

ssh-copy-id user@remote_server_ip

Replace user with your remote server username and remote_server_ip with the IP address or domain name of your remote server.

Use Rsync to Copy Backups to Remote Server

You can use Rsnapshot's cmd_postexec option to run a command after each backup. In this case, you can use rsync to copy the backup directory to the remote server.

Edit Rsnapshot Configuration

sudo nano /etc/rsnapshot.conf

Add the following cmd_postexec command at the end of the configuration file to run rsync after each backup:

cmd_postexec    /usr/bin/rsync -avz /backup/ user@remote_server_ip:/path/to/remote/backup/

There is cmd_postexec in the file, you can uncomment and copy and paste rsync command too. Use one of the method, both works perfectly.

Replace /backup/ with your local backup directory, user with your remote server username, remote_server_ip with your remote server's IP or domain, and /path/to/remote/backup/ with the directory on the remote server where you want to store the backups.

Before automating the process, test the rsync command to ensure it's working correctly:

rsync -avz /backup/ user@remote_server_ip:/path/to/remote/backup/

Check the remote server to verify that the backups have been copied successfully.

Step 6: Automate the Backup and Transfer

Once you’ve confirmed that the rsync command works as expected, the backups will be copied to the remote server automatically after each Rsnapshot backup, thanks to the cmd_postexec command.

Step 5: Monitoring and Maintaining Backups

To ensure that your backups are running smoothly, you can check the Rsnapshot logs located in /var/log/rsnapshot.

You can also set up email notifications or use monitoring tools to alert you if a backup fails.

Conclusion

You've now set up Rsnapshot to automate backups on your Linux server. Regular backups are critical for disaster recovery, and with Rsnapshot, you can maintain an efficient and organized backup system. Be sure to periodically check your backups and test restoring data to ensure your backup strategy is effective.