Installing rsnapshot On CentOS

Want to support HowtoForge? Become a subscriber!
 
Submitted by Netzen (Contact Author) (Forums) on Wed, 2011-08-03 15:28. :: CentOS | Backup

Installing rsnapshot On CentOS

Working for an IT Support company I needed a solution for automatically backing up a bunch of Linux servers, this guide will walk you through setting up rsnapshot on CentOS for automated rsync & ssh binary diff backups.

Installing rsnapshot on CentOS / RHEL requires the EPEL repository please follow our guide on installing EPEL on CentOS / RHEL when using the epel repo I would advise the use of YUM priorities.

Once the epel repo is installed enter the following as root:

yum install rsnapshot

This will install Rsnapshot and pull down any packages it depends on.

Open up the file /etc/rsnapshot.conf with you’re chosen text editor (I use vi), let's take a look at the first section snapshot_root this is the location where rsnapshot stores its backups. By default it places them in the root directory, but I have changed mine to /backup/snapshots/.

# All snapshots will be stored under this root directory.
#
snapshot_root   /backup/snapshots/

Next I uncommented no_create_root 1 ; this stops rsnapshot creating the snapshot_root dir (meaning you have to create it yourself). The benefit of this being, if you're backing up to a USB drive and you forget to connect it rsnapshot will not backup to the mount point filling your drive space up and possibly causing a server crash (if you have partitioned incorrectly).

# If no_create_root is enabled, rsnapshot will not automatically create the
# snapshot_root directory. This is particularly useful if you are backing
# up to removable media, such as a FireWire or USB drive.
#
no_create_root  1

We are using Linux, so uncomment the cmd_cp line.

# LINUX USERS:   Be sure to uncomment "cmd_cp". This gives you extra features.
# EVERYONE ELSE: Leave "cmd_cp" commented out for compatibility.
#
# See the README file or the man page for more details.
#
cmd_cp          /bin/cp

Next uncomment cmd_ssh and give it the correct path to the ssh binary.

# Uncomment this to enable remote ssh backups over rsync.
#
cmd_ssh        /usr/bin/ssh

I uncommented the du option as well, cool little tool that shows you the size of each of your snapshots. (remember if you just did a du -sh * in the snapshot dir it would read wrong due to the hard linking).

# Uncomment this to specify the path to "du" for disk usage checks.
# If you have an older version of "du", you may also want to check the
# "du_args" parameter below.
#
cmd_du          /usr/bin/du

This brings us onto Backup Intervals, as you can see I have kept the defaults, so rsnapshot will keep 6 copies of my hourly backup before it starts over writing my old one, 7 copies of my daily backups, 4 of my weekly and 3 monthly backups! Worth noting that rsnapshot will not create the daily.0 backup until hourly.5 has been created in the snapshot_root, the same with weekly and monthly.

#########################################
#           BACKUP INTERVALS            #
# Must be unique and in ascending order #
# i.e. hourly, daily, weekly, etc.      #
#########################################
interval        hourly  6
interval        daily   7
interval        weekly  4
interval        monthly 3

 

Global Options

The defaults for these are normally fine, however I wanted to a bit more verbosity until I am happy the backup system is working. I changed the setting to 4 instead of 3, 4 displays the commands on the command line as if you were entering them by hand, this was good enough for me!

# Verbose level, 1 through 5.
# 1     Quiet           Print fatal errors only
# 2     Default         Print errors and warnings only
# 3     Verbose         Show equivalent shell commands being executed
# 4     Extra Verbose   Show extra verbose information
# 5     Debug mode      Everything
#
verbose

logfile, I uncommented this as logs are always handy for finding out what went wrong.

# If you enable this, data will be written to the file you specify. The
# amount of data written is controlled by the "loglevel" parameter.
#
logfile /var/log/rsnapshot

exclude - after running my first backup it became apparent that I had a bunch of VMWare disk image files in my home dir, so I excluded them with:

# 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. Please look up the --include and --exclude options in the
# rsync man page for more details on how to specify file name patterns.
#
#include        ???
#include        ???
#exclude        ???
exclude         /home/keith/vmware-disks/

The next thing I configured was the backup points, what I wanted backed up from each of my machines, starting with the localhost.

###############################
### BACKUP POINTS / SCRIPTS ###
###############################
# LOCALHOST
backup  /home/          localhost/
backup  /etc/           localhost/
backup  /usr/local/     localhost/
backup  /var/log/               localhost/
backup  /srv/           localhost/
backup  /boot/          localhost/
backup  /opt/           localhost/

Now I want to backup my email server the-death-star, so I configured my backup server to pull its data in over SSH (kinda of like when that Star Destroyer pulled in the Millennium Falcon with its tractor beam, yeah… Kind of…).

backup  root@the-death-star.techspotting.org:/home/		the-death-star/
backup  root@the-death-star.techspotting.org:/etc/		the-death-star/
backup  root@the-death-star.techspotting.org:/usr/local/bin/   the-death-star/

This is the end of the config file editing, now it’s time to give your config a test with:

rsnapshot configtest

This should come back and say Syntax OK unless you messed something up, in which case it will tell you the line it errors on, so go back and fix it!

 

SSH Keys

It’s all great pulling in the data over SSH but we need to be able to login automatically to our remote servers, we can’t just grab the data and drag it to our server, what do you think this is, a tractor beam?

I use remote root logins for my servers, as I am backing up files in locations like /etc/passwd which are only readable by root, yes there are ways around this, but I do not want this document to get to complex. I will do a separate blog post in the future for this, and link to it here.

So we need to set up SSH keys without passphrases, otherwise we are going to get prompted each time the backup runs for the pass phrase. So let me walk you through creating some SSH keys for key based authentication.

Create the key pair, this will create the public and private keys (never give your private key out!).

ssh-keygen -t rsa

Accept all the defaults, just push enter when it asks you for a passphrase:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

You should now have a set of keys in /root/.ssh/. Now we need to copy them to the remote machine we want to login to, in this case the-death-star.techspotting.org so this would be the command:

ssh-copy-id -i ~/.ssh/id_rsa.pub root@the-death-star.techspotting.org

You will then get prompted for the password for your account on the remote host, go ahead and enter it, ssh-copy-id will then copy the key to the correct dir and sort out the correct file system permissions, cool huh?

Give it a test and make sure it’s working:

ssh the-death-star.techspotting.org

If you look in /etc/cron.d/rsnapshot you should see a file that looks like this:

# 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

Providing you are happy with the default cron config (which I was) remove the comments so it looks like this:

# 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

Now you have enabled cron to back up your files using rsnapshot, you had better make sure it is going to work!

rsnapshot -t hourly

Check everything looks sane and nothing nasty is going to happen and repeat for daily, weekly, monthly.

Now it’s time for your first backup!

rsnapshot hourly

If you have verbosity set to 4 like me (optional), you will be able to see all the files your backing up flying up the screen! The first backup has to move all the file the snapshot_root so this is going to take some time, depending on the speed of your machine / connection, might be a good time to grab another coffee.

You should start to see files in your snapshot_root, after a few weeks you should see a bunch of hourly.*, daily.*, weekly.*

Here is what my snapshot_root looks like:

/backup/snapshots# ls -l
total 48
drwxr-xr-x 3 root root 4096 2010-03-21 18:19 daily.0
drwxr-xr-x 3 root root 4096 2010-03-21 18:19 daily.1
drwxr-xr-x 3 root root 4096 2010-03-21 18:18 daily.2
drwxr-xr-x 3 root root 4096 2010-03-21 17:57 daily.3
drwxr-xr-x 3 root root 4096 2010-03-21 17:57 daily.4
drwxr-xr-x 3 root root 4096 2010-03-21 17:56 daily.5
drwxr-xr-x 3 root root 4096 2010-03-21 18:20 hourly.0
drwxr-xr-x 3 root root 4096 2010-03-21 18:19 hourly.1
drwxr-xr-x 3 root root 4096 2010-03-21 18:19 hourly.2
drwxr-xr-x 3 root root 4096 2010-03-21 18:19 hourly.3
drwxr-xr-x 3 root root 4096 2010-03-21 18:19 hourly.4
drwxr-xr-x 3 root root 4096 2010-03-21 17:56 weekly.0

That's it all done, any problems drop me a comment! I will try my best to reply on the same day for you. :)

Enjoy the peace of mind of having your servers backed up!


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Duke (not registered) on Wed, 2012-08-15 15:26.
I never used automatic processes, this looks really nice.
 
However, what about restoring data - without that the whole thing seems not complete? Thanks!
Submitted by Affinity (not registered) on Thu, 2011-08-04 13:41.
I use additional rsync options, including -F so that I can simply use .rsync-filter files at the top level of any file system area being backed up.
Submitted by Affinity (not registered) on Thu, 2011-08-04 10:26.
Also, edit your /etc/updatedb.conf and add your rsnapshot root to the PRUNEPATHS entry; that will save excessive indexing of the backups.