Skip to content

Backup Your Server With Rsnapshot

Rsync Linux Rsync Linux

Backup your Ubuntu server with rsnapshot, an automated backup software relying on Rsync. It's free, open source, and reliable, while conserving disk space. This is due to rsnapshot’s incremental backup approach – after an initial full backup, only modified files are backed up.

Installation

You want to be able to restore the server to a previous state if something goes wrong. Easily set up rsnapshot on your server with the command sudo apt install rsnapshot.


Linux backup server

Configuration

You’ve successfully installed rsnapshot! Now, configure the backup directory's location, external dependencies, backup intervals, logging, and selection of files to backup.

Step-by-step guide

Backup directory

Open rsnapshot's configuration file:

sudo vi /etc/rsnapshot.conf

Add or modify the following lines to define where rsnapshot should store server backups. Replace $PATH with the path to the actual backup folder, which can be located on the server's hard drive, an external drive connected to the server or a remote location. Also set the flag no_create_root to avoid that rsnapshot automatically creates a backup folder:

snapshot_root   $PATH
no_create_root  1

Dependencies

Still in rsnapshot's configuration file /etc/rsnapshot.conf, remove the comments from the following lines to tell rsnapshot about external dependencies:

cmd_rsync           /usr/bin/rsync
cmd_du              /usr/bin/du
cmd_rsnapshot_diff  /usr/bin/rsnapshot-diff

To make sure rsnapshot is pointing towards the right binaries, run the following commands and double-check if all paths are correct:

whereis rsync
whereis du
whereis rsnapshot-diff

Backup intervals

Below settings in rsnapshot's configuration file /etc/rsnapshot.conf define the intervals at which backups are performed, as well as the number of backups to keep. Hourly, daily, weekly and monthly backups are supported. In this example, we'll keep the 3 last daily backups, 7 last weekly backups and 2 last monthly backups. Adjust according to your own backup strategy:

retain  daily   3
retain  weekly  6
retain  monthly 2

Logging

You might want to enable logs for debugging purposes. Create a log file:

sudo touch /var/log/rsnapshot.log

Enable logging in rsnapshots configuration file /etc/rsnapshot.conf:

verbose     5
loglevel    5
logfile     /var/log/rsnapshot.log

Backup files (and use Rsync exclude directory)

Decide which server files to backup. You can specify one or several directories of your choice. You can also exclude certain sub-directories using the exclude function. Make sure to use one line for each directory, and to add a trailing / at the end of each directory path. In this example, we'll create an entire Linux filesystem snapshot of the root folder /, excluding the folders tmp, proc, mnt and sys:

backup  /   .   exclude=tmp,exclude=proc,exclude=mnt,exclude=sys

Test

Verify the syntax of the configuration file (the terminal should prompt Syntax OK):

sudo rsnapshot configtest

Now test your hourly, daily, weekly or monthly backup strategies:

sudo rsnapshot -t hourly
sudo rsnapshot -t daily
sudo rsnapshot -t weekly
sudo rsnapshot -t monthly


Linux Rsync

Schedule backups

Schedule automatic backups at specific intervals with rsnapshot. Explore further details below.

Step-by-step guide

Run the shortest backup strategy to create a first full snapshot. In our example, that's the daily backup (adjust accordingly):

sudo rsnapshot daily

Open the cron configuration file to schedule backups at certain intervals:

sudo vi /etc/cron.d/rsnapshot

In this example, we'll schedule daily snapshots at 1:00am, weekly snapshots every Monday at 6:30pm and monthly snapshots on the 2nd day of every month at 7:00am. Define your own schedules as needed, the crontab guru can be of help:

#0 */4 * * *    root    /usr/bin/rsnapshot hourly
0 1  * * *      root    /usr/bin/rsnapshot daily
30 18 * * 1     root    /usr/bin/rsnapshot weekly
0 7 2 * *       root    /usr/bin/rsnapshot monthly


Even if you perform regular backups, what must be done to ensure that you are protected against data loss

Restore backups

Should an issue arise, you can restore your server to a previous state using one of rsnapshot's backups. More details below.

Step-by-step guide

The command below lists all existing snapshots, replace $PATH with the path to the backup folder defined earlier:

ls -l $PATH

Depending on your backup strategy, the output might look something like this:

/path/to/rsnapshot/backup/folder/daily.0
/path/to/rsnapshot/backup/folder/daily.1
/path/to/rsnapshot/backup/folder/daily.2
/path/to/rsnapshot/backup/folder/weekly.0
/path/to/rsnapshot/backup/folder/weekly.1
/path/to/rsnapshot/backup/folder/weekly.2
/path/to/rsnapshot/backup/folder/weekly.3
/path/to/rsnapshot/backup/folder/weekly.4
/path/to/rsnapshot/backup/folder/weekly.5
/path/to/rsnapshot/backup/folder/monthly.0
/path/to/rsnapshot/backup/folder/monthly.1

The following command restores the server to a previous state:

sudo rsync -avr $SOURCE_PATH $DESTINATION_PATH

Make sure to provide the correct $SOURCE_PATH (= the backup directory which is about to be restored) and $DESTINATION_PATH (= the target directory on the server to which the backup needs to be restored). For example, the following command will restore the weekly snapshot of the entire file system from 3 weeks ago to the root / of the server:

sudo rsync -avr /path/to/rsnapshot/backup/folder/weekly.3/ /

Caution! Here be dragons.

Exercise extreme caution during system restoration to prevent irreversible data overwrites and potential losses.