Backup Linux Server with rsnapshot

rsnapshot is a backup software based on rsync. It can make an incremental backup. rsnapshot makes an automatic incremental backup using rsync and cronjob. It use linux hardlinks to save disk space. When you make the first backup, all files are copied to the backup location, every subsequent backup makes a copy of the previous backup using Linux hard links, then copies over new and changed files, deletes files that are removed from the source folder. This way, even if you have multiple copies of the files, only one file is stored in your disk. rsnapshot is available for installation from apt repositories in Ubuntu and Debian Linux.

https://rsnapshot.org/ (GitHub)

To install rsnapshot on Ubuntu/Debian, run

apt install rsnapshot -y

Configure rsnapshot

rsnapshot configuration file available in /etc/rsnapshot.conf, you need to edit this file to configure rsnapshot.

vi /etc/rsnapshot.conf

To change the location of backup folder, change

snapshot_root   /var/cache/rsnapshot/

I change this to /backup/

snapshot_root   /backup/

Below you will find

retain  alpha   6
retain  beta    7
retain  gamma   4

To easily understand the backup schedule, I renamed it as daily/weekly/monthly

retain  daily   6
retain  weekly    3
retain  monthly   3

This will store 6 daily backups, 3 weekly backups, and 3 monthly backups.

Uncomment following lines

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

The lines starting with “backup” define the backup. In these lines each parameter must be separated with TAB (space won’t work).

You can verify the configuration with the command

rsnapshot configtest

Test Backup

To see the commands used by rsnapshot to take backup, run

rsnapshot -t daily

Here is rsnapshot -t daily result for the backup command

backup	/root/		localhost/

To exclude a folder from the backup, you can use +rsync_long_args=–exclude=/full/path/to/folder

Example

backup  [email protected]:/home/serverok.in/  serverok.in/  +rsync_long_args=--exclude=/home/serverok.in/html/wp-content/cache

If you need to exclude multiple files, you can use exclude-from to specify a file with all paths to exclude.

backup	[email protected]:/home/	137.184.80.235/		+rsync_long_args=--exclude-from="/usr/serverok/exclude-server5"

NOTE: use TAB to separate each item in the backup line. For arguments, use space to separate.

Running Daily Backup

To run the daily automated backup, you need to set a cronjob

0 0 * * * /usr/bin/rsnapshot daily
0 6 * * 0 /usr/bin/rsnapshot weekly

This will run backup every day at 00:00 hours server time.

Bash script to schedule Backups

Here is a backup script, that checks the day of the month and the day of the week to decide what type of backup needs to run today, this avoids running multiple backups same day.

#!/bin/bash
# Author: Yujin Boby
# Web: https://serverok.in

DAY_OF_WEEK=$(date +%w)
DAY_OF_MONTH=$(date +%d)

if [[ "$DAY_OF_MONTH" -eq 1 ]]; then
    echo "Running monthly backup..."
    /usr/bin/rsnapshot monthly
elif [[ "$DAY_OF_WEEK" -eq 0 ]]; then
    echo "Running weekly backup..."
    /usr/bin/rsnapshot weekly
else
    echo "Running daily backup..."
    /usr/bin/rsnapshot daily
fi

Save the content in a file, for example, backup.sh, and set a cronjob to execute the file every day.

See Backup

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *