Efficiently backup your server – Rsnapshot

Backup are often overlooked or set in “Procrastination” mode (I’ll do it tomorrow…) but it is probably one of the most important task you need to do it, and do it right.

When a failure or hack happen, no one want to loose all its work (Hundreds articles written? Super important files? etc…).

I think we have to differentiate RAID and Backup system.

For me, a RAID system is not for backup, but for availability. If you got an issue, you can quickly rebuild the system. But if a bad copy is shared to all your drives through your RAID, well it’s too late.

The most popular way to back up a server is probably through RSYNC (file synchronization and file transfer program). Very simple to use, it will only transfer files if there is a difference (New files? modified files? Deleted files?, etc…). It can then save a lot of bandwidth, better than simply copy everything again and again.

I’ve been using Rsync for years for my backup system, but decided to change over Rsnapshot, mainly because of the lack (or not efficient) versioning. I used to create a script to keep 7 days back up of my MySQL databases and websites and an additional copy every month. But my backup folder started to explode after few months, so I had to delete the old one manually to free up some space.
Rsnapshot can actually fix it, that’s way I’ve decided to use it instead.Rsnapshot is an open source backup software written in Perl, that uses Rsync and SSH to create incremental backups.

The differences of backup are kept with hardlinks (Keep only 1 version of the file).You can set up rotative backups on a daily, weekly and monthly basis while backups can be from a local or a remote filesystem (Through SSH).

Let’s see how to use it.

Installation

Simply run in root (or with sudo)

apt-get install rsnapshot

 

Rsnapshot configuration

  • Local Backup

1) Create a specific folder to keep all backups

In my case, I have a SSD for the system and 1 HDD for big files. So I’ll back up all the important stuff from the SSD on my HDD.

mkdir /media/Stockage/Backup

2) Configure the /etc/rsnapshot.conf file

nano /etc/rsnapshot.conf

and configure:

– snapshot_root (Where to store your backups)

snapshot_root   /media/Stockage/Backup

– Backup Intervals

Although the default settings are enough, you could modify this part to add a monthly backup for example.

#########################################
#           BACKUP INTERVALS            #
# Must be unique and in ascending order #
# i.e. hourly, daily, weekly, etc.      #
#########################################

retain          hourly  6
retain          daily   7
retain          weekly  4
#retain monthly 3

Hourly 6, means you keep 6 copies of the hourly backup (So one every 4 hours)

Daily 7 means you keep 7 copies of the daily backup (So one every day)

You can uncomment the

retain     monthly      3

(It’s not space it’s TAB between the values)

if you want monthly backup too.

– Local directories

Setup the folder your want to backup using the following syntax:

# LOCALHOST
backup  /home/          MyWebsite/
backup  /var/www/          MyWebsite/

In my case I will backup my /home and /var/www.

Note that the path must finish by /

– MySQL databases

You will need the mysql-client to be installed in order to use mysqldump.

apt-get install mysql-client

Then create a simple script in your /home/user/mysqlback.sh

nano /home/karibu/mysqlback.sh
chmod +x /home/karibu/mysqlback.sh

and paste:

mysqldump –-all-databases -uroot -pmypassword –-opt > mysqldump.sql

(replace the user/password)

and in the /etc/rsnapshot.conf at the localhost local directories, add:

backup_script   /home/karibu/mysqlback.sh  mysql/

(Still use TAB, not space, between the values)

3) Test the configuration

# rsnapshot configtest
Syntax OK

4) Run it

Give it a try by running

rsnapshot hourly

5) Set up cron tasks for the automatic back up

By default, rsnapshot comes with cron file under “/etc/cron.d/rsnapshot“ but are commented.

Simply uncomment them to enable hourly, daily, weekly and monthly backup.

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

The first line, will run every 4 hours the hourly backup. Rsnapshot will create the hourly directory and based on your configuration will keep “n” copies.

The second line will run daily at 3:30am and will create the daily directory.

The third will run the weekly backup every Monday at 3:00am

The forth will run the monthly backup at 2:30am.

 

  • Remote Backup

If you want to remotely backup another server/machine, here are the additional steps:

1) Create a SSH Password-less Login for the remote machine

On your local server, you will first need to create a RSA SSH key, simply run:

ssh-keygen -t rsa

and leave the password empty.

Then, add your public SSH key to the authorized keys on your remote machine.

Still using the terminal of your local server, run:

ssh-copy-id -i root@domain.tld

If you have a message about the authenticity of the host, just say yes.

It will then ask you the user password you mentioned on the command. Once enter, it will add your SSH key automatically to the authorized keys.

Simply give it a try by running

ssh root@domain.tld

2) Modify the /etc/rsnapshot.conf

– Enable remote backup over SSH:

Find and uncomment the line

cmd_ssh            /usr/bin/ssh

– Change SSH port if needed

If your SSH port is not the default 22, you can change it at the line:

ssh_args        -p 22

– Set the remote directories

At the end of the file you can add something similar:

## MyRemoteHost
backup     root@domain.tld:/home/     MyRemoteHost/

to copy the remote host’s home under the folder ‘MyRemoteHost’ that will be created on your local backup folder.

If you want to backup your remote MySQL databases, similar than local one, you can create a script to dump the databases, or directly set the command inside the /etc/rsnapshot.conf such as:

backup_script   /usr/bin/ssh root@domain.tld "mysqldump --all-databases -uUSER -pPASS --events --opt > /tmp/mysqldump.sql" Unused/
backup  root@domain.tld:/tmp/mysqldump.sql  MyRemoteHost/mysql/

As you can see, it needs to be broken in 2 parts, the first will run mysqldump on the remote host and we will use a “Unused” folder to avoid conflicts with others “used” folder like mysql in the second command. This will avoid the error:

ERROR: destination conflict between "mysql/" and "MyRemoteHosst/mysql/" in multiple backup_script entries

The second command will simply copy the mysqldump.sql file to your local backup folder.

3) Give it a try

You can manually run the first backup to give it a shot:

rsnapshot hourly

or for some verbose, simply add -v

rsnapshot -v hourly

 

You should now have a working and proper backup system. Obviously you will still need to make some others copies on different supports/locations to minimize the risk of loosing your data.

If any question or unclarity, please let me know

Gravatar

Loves to discover web-based apps to install on his own server@home and write articles about it

0 Comments:

Add a comment