LinuxMoz

Linux Stuff && Coffee

Rsnapshot Ubuntu Install

| Comments

The following Rsnapshot Ubuntu guide walks you through the process of setting up a fully automated Ubuntu backup solution that meets the following requirements:

  • Secure – Data is transfered securely using encryption
  • Automated – Runs each day without any user interaction
  • Reliable

You can think of Rsnapshot like Apple Time Machine, but on the command line, syncing only what has changed each day and creating snapshots and uses hard links to the old unchanged files to minimize disk space consumption (kind of like a poor mans netaps).

Another advantage of rsnapshot is that it logs into remote servers and pulls the data back over SSH, which means the backup server can sit behind a firewall with no ports forwarded in, this is very important as the server is going to be authenticating to other servers via ssh keys without passphrases.

For more info check out Rsnapshot

Rsnapshot Ubuntu install guide

The following guide walks you through the process of installing Rsnapshot on Ubuntu Linux.

Login as root (or sudo -s if you’re on Ubuntu)

1
aptitude 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), lets take a look at the first section “snapshot_root” this is the location where Rsnapshot stores it’s backups. By default it places them in the root directory, but I have changed mine to “/backup/snapshots/”.

1
2
3
# 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 your 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).

1
2
3
4
5
# 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.

1
2
3
4
5
6
# 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.

1
2
3
# 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).

1
2
3
4
5
# 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.

1
2
3
4
5
6
7
8
9
10
#########################################
#           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!

1
2
3
4
5
6
7
8
# 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 :)

1
2
3
4
# 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:

1
2
3
4
5
6
7
8
9
# 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/kjs/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.

1
2
3
4
5
6
7
8
9
10
11
12
###############################
### 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 it’s data in over SSH (kinda of like when that Star Destroyer pulled in the Millennium Falcon with it’s tractor beam, yeah… Kind of…).

1
2
backup  [email protected]:/home/      the-death-star/
backup  [email protected]:/etc/        the-death-star/
1
backup  [email protected]:/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:

1
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!

Ubuntu SSH Keys Setup

It’s all great pulling in the data over SSH but we need to be able to login automatically to our remote servers.

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.

We need to setup SSH keys without passphrases, otherwise we are going to get prompted each time the backup runs for the pass phrase.

Creating SSH keys for key based authentication

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

1
 ssh-keygen -t rsa

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

1
2
3
4
5
6
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:

1
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

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:

1
ssh the-death-star.linuxmoz.com

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

1
2
3
4
5
6
7
8
9
10
11
# 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:

1
2
3
4
5
6
7
8
9
10
11
# 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 backup your files using rsnapshot, you had better make sure it is going to work!

1
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!

1
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/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 concludes LinuxMOZ’s Rsnapshot Ubuntu install guide, don’t forget to subscribe to our RSS on the right for more Linux Guides updates.

Comments