RSNAPSHOT

[TUTORIAL] INSTALL AND CREATE AUTOMATED BACKUPS ON UBUNTU WITH RSNAPSHOT

As I dive deeper into server administration and working with linux it has become more and more apparent that it is important more than ever to have backups of your data. Especially if you are hosting on servers from the LowEnd market. [Lowendtalk/Lowendbox] There are times where I have experienced data loss whether it be at the hands of the provider or my own doing and to start from scratch is just a pain. Here is a simplified tutorial on how to set up / install and use RSNAPSHOT a incremental backup tool utilizing rsync to easily create and transfer backups to a remote location on Ubuntu completely automated.

http://geekfault.org/wp-content/uploads/2009/05/rsnapshot1.png

*all commands can be performed either root or sudo -s

Step 1: Installing Rsnapshot

The first step in getting your content backed up is to install rsnapshot on the machine that you would like to use as your backup server.

Rsnapshot is in Ubuntu’s default repositories, so we can install it using apt-get:

sudo apt-get install rsnapshot

Step 2: Configuring Rsnapshot

After you have installed rsnapshot, you will need to edit the configuration file.

sudo nano /etc/rsnapshot.conf

**note** One thing to keep in mind while you are working with the rsnapshot configuration file is that each directory needs a trailing slash (/) to be read correctly.

The first decision you will need to make is where you would like to store your backups. We will use the directory “/backup” as our backup location. Search for and edit the following variable to set the backup location.

snapshot_root			 /backup/ 

If this directory does not already exist, rsnapshot will create it when it runs.

You will also want to uncomment the cmd_ssh line to allow for remote backups. Remove the “#” from in front of the following line so that rsnapshot can securely transfer your data to the backup server.

cmd_ssh			/usr/bin/ssh

Remove the “#” from before the cmd_du line to enable accurate disk usage reports.

cmd_du				/usr/bin/du

Next, we need to consider how many old backups we would like to keep. Since rsnapshot uses incremental backups, we can afford to keep older backups for awhile before removing them.

We set these up under the “BACKUP INTERVALS” section of the configuration. We tell rsnapshot to retain a specific number of backups of each kind of interval. For the purposes of this guide, the default setting is good, Edit this section to reflect your backup needs:

#########################################
#           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

Another item that may need to be edited is the ssh_args variable. If you have changed the port number on the server you are backing up, you’ll want to uncomment the ssh_args section and use the same settings here.

ssh_args			-p PORTNUMBER

Finally, you’ll need to decide on what you would like to backup. If you are backing up locally to the same machine, this is as easy as specifying the directories that you want to save and following it with “local/” which will be a sub-directory in the snapshot_root that you set up earlier.

**note** when entering these use TAB for spacing do not insert spaces as that will cause errors later on but the test config will help you catch it quickly.

For example we choose to backup the home directory and the etc directory on the local machine, the lines we will enter will be as follows.

**note** by default both lines below are uncommented if you do not need to create backups either comment them out or delete the lines.

backup		/home/		local/
backup		/etc/		local/

If you are backing up a remote server, this can be configured here too. You just need to tell rsnapshot where the server is and which directories you would like to back up.

backup		 [email protected]:/home/ 		remote-server/

You’ll want to change the “server.com” portion with the IP address or domain name of the server you wish to back up. After your server name/IP address, you need to put a colon “:” followed by the directory path you would like to back up. The “remote-server” portion is the sub-directory where backups will be stored in the /backup folder. You can name it whatever you’d like; I typically name them by hostname for ease of keeping track of backups.

We are now finished with the initial configuration of rsnapshot. Save the /etc/rsnapshot.conf file before continuing. If you are using nano to edit, press “Ctrl-X” to exit and answer “Yes” at the prompt to save changes.

rsnapshot is pull-based. Meaning that, without additional workarounds and configuration, you cannot push the backups to a remote server. Rather, with the SSH keys, they will be pulled into the server with rsnapshot installed

http://www.yolinux.com/TUTORIALS/images/Rsync-SingleHost.png

Test Your Configuration

Before setting up the automation, we’ll want to test that everything works as expected. To test that your configuration has the correct syntax, run:

sudo rsnapshot configtest

If your file is error-free, you will receive a “Syntax OK” message. If you did not receive this message, you need to go back and fix the mistakes that it tells you about.

Next, we want to perform a dry run of one of the snapshot to make sure the backup process is working the way we want it to. We will use the “hourly” parameter because hourly backups are the basic snapshot type that the other intervals will build off of.

sudo rsnapshot -t hourly

Check if the generated output looks correct you can remove the “-t” option to try your setup for the first time.

sudo rsnapshot hourly

This will run the backup that we setup in our configuration file. For this tutorial, rsnapshot created a /backup directory and then created the directory structure under it that organizes our files. Verify the files were successfully backed up to the specified folder.

Automation

With rsnapshot working correctly, the only thing left to do is to schedule it to run at certain intervals. For this we will use cron, a linux scheduler, to make this happen.

Luckily, rsnapshot includes a default cron file that we can edit to our liking. We’re going to edit this file with nano again.

sudo nano /etc/cron.d/rsnapshot

The scheduling is currently commented out. We’re going to remove the “#” character from the beginning of the scheduling section to activate these values.

# 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

Settings explained:

0 */4         * * *           root    /usr/bin/rsnapshot hourly
*this will run a snapshot every 4 hours
30 3          * * *           root    /usr/bin/rsnapshot daily
*this will run a snapshot daily at 3:30am
0  3          * * 1           root    /usr/bin/rsnapshot weekly
*this will run a weekly snapshot every Monday at 3:00am
30 2          1 * *           root    /usr/bin/rsnapshot monthly
*this will run a snapshot on the first day of each month at 2:30am

It is important to stagger your backups and run larger backup intervals first. This means running the monthly backup first and progressing to shorter intervals from there in order, as we’ve done in this tutorial. This is necessary so that the program does not get caught up trying to do multiple backups at the same time, which can cause problems.

Done!