How to backup remote linux servers using rnapshot in Debian / Ubuntu

Say you want to backup 2 remote linux servers, a great tool for this is rsnapshot. So we’re going to backup those 2 servers and will make incremental snapshots of local and remote filesystems for any number of machines on 2nd hard disk located at /disk1 ( /dev/sdb2).

rsnapshot is perfect open source solution for making backups on local system. It supports both remote and local systems. From the man page:

rsnapshot saves much more disk space than you might imagine. The amount of space required is roughly the size of one full backup, plus a copy of each additional file that is changed. rsnapshot makes extensive use of hard links, so if the file doesn’t change, the next snapshot is simply a hard link to the exact same file. The following instructions are compatible with both Debian and Ubuntu Linux.

Required software / setup on local backup system

  1. rsnapshot
  2. rsync
  3. ssh client
  4. 2nd hard disk ( RAID array is suggested) – you can also use primary hard disk
  5. Password less login configured using ssh keys
  6. /disk1/backup – Backup directory
  7. /disk1/backup/server1 – Backup directory for remote server called server1
  8. /disk1/backup/server2 – Backup directory for remote server called server2
  9. /disk1/backup/localhost – Backup directory for local server

Required software on remote server

  1. OpenSSH sshd server
  2. Password less login configured using ssh keys

Step #1: Install rsync and rsnapshot software

Use apt-get command, enter:
$ sudo apt-get install rsync rsnapshot

Step #2: Configure passwordless login / public key based login

Type the following command
# ssh-keygen -t rsa
# scp .ssh/id_rsa.pub [email protected]:.ssh/authorized_keys2
# scp .ssh/id_rsa.pub [email protected]:.ssh/authorized_keys2

See how to configure RSA / DSA SSH public key based authentication.

Step #3: Configure rsnapshot utility

The configuration file is located at /etc/rsnapshot.conf. The configuration file requires tabs between elements and all drectories require a trailing slash. Just open config file using a text editor such as vi or gedit:
# vi /etc/rsnapshot.conf
OR
$ sudo vi /etc/rsnapshot.conf
Set snapshots root directory:

snapshot_root	/disk1/backup/

Note you must separate snapshot_root and /disk1/ by a [tab] key i.e. type snapshot_root hit [tab] key once and type /disk1/backup/. All snapshots will be stored under this root directory (/disk1/backup/).

Configure backup policy

You can make hourly, daily, weekly or monthly snapshots of local and remote systems. To make a snapshot every four hours (six times a day) and keep a second set, which are taken once a day, and stored for a seven days, enter:

interval    hourly  6
interval    daily   7

Feel free to adapt configuration as per your backup needs.

Specify local and remote backup directories

Find out comments that read as follows:

###############################
### BACKUP POINTS / SCRIPTS ###
###############################

You need to comment out / delete default backup directories. To make snapshots for /home/, /etc/, /webroot/ directories to /disk1/backup/localhost, enter:

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

To backup remote server1 /home/, /etc/, /var/spool/mail/, /webroot/ directories to /disk1/backup/server1, enter:

backup	[email protected]:/home/	server1/
backup	[email protected]:/etc/	server1/
backup	[email protected]:/webroot/	server1/
backup	[email protected]:/var/spool/mail/	server1/
backup	[email protected]:/home/	server2/

Save and close the file.

Test your config file for errors

Type the following to test your configuration file for errors
# rsnapshot configtest
Output:

Syntax OK

You can also run rsnapshot in a test mode to display its action:
# rsnapshot -t hourly

Step #4: Run rsnapshot for first time

To run first time, enter:
# rsnapshot hourly

Step #5: Configure cron job

Edit /etc/cron.d/rsnapshot file to setup backup snapshot job. This is a sample cron file for rsnapshot. The values used correspond to the examples in /etc/rsnapshot.conf. There you can also set the backup points and many other things. To activate this cron file you have to uncomment the lines below.
Feel free to adapt it to your needs.

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

See crontab related faq for more information about cronjob under UNIX / Linux.

How do I exclude files from backup?

rsnapshot allows you to set the include and exclude parameters, if enabled, simply get passed directly to rsync. If you have multiple include/exclude patterns, put each one on a separate line. For example,
exclude_file /etc/rsnapshot.server1.conf
Append exclude file list to /etc/rsnapshot.server1.conf:
# vi /etc/rsnapshot.server1.conf
Exclude files matching pattern from backup:

var/lib/php/session/*
/var/spool/mail/nobody/*
cache/wp-cache-*.????
/var/logs/apache/access.log.*
/var/logs/apache/error.log.*
/linux-kernel/*
/tmp/cache/*
/var/lib/mysql/mysql.sock*
/tmp/php.socket-*
/tmp/*socket*

Save and close the file.

How do I backup remote MySQL database?

You can backup default database directory /var/lib/mysql. However, you can backup remote or local MySQL database with the following script:

#!/bin/sh
NOW=$(date +"%d-%m-%Y")
# set mysql login info
MUSER="MySQL-UserNAME"                # Username
MPASS="MySQL-SERVER-PASSWORD"   # Password
MHOST="MySQL-SERVER-IP-ADDRESS"  # Server Name
# guess binary names 
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

# get all db names 
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

Now add following line to /etc/rsnapshot.conf file:
backup_script /root/scripts/mysql.backup.sh server1/mysql/

How do I restore backup?

You can simply copy back file using regular scp / rsync command. In this example, restore all *.html file to remote web server called www-03.example.com:

 
rsync -av *.html [email protected]:/home/httpd/example/html/