Monday, December 26, 2011

Backup Using rsnapshot rsync Utility


1. Download rsnapshot Linux Backup Utility

# cd /usr/src
# wget http://www.rsnapshot.org/downloads/rsnapshot-1.3.1.tar.gz

# tar xvfz rsnapshot-1.3.1.tar.gz
# cd rsnapshot-1.3.1

2. Install rsnapshot to backup Linux direcotires

Install rsnapshot Linux backup utility using ./configure and make install as shown below.

# ./configure --sysconfdir=/etc

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
config.status: creating t/rsync.t
config.status: creating t/gnu_cp.t
config.status: creating t/relative_delete_bugfix.t
Type "make install" to install the program.
After rsnapshot is installed, don't forget to copy
/etc/rsnapshot.conf.default to /etc/rsnapshot.conf

# make install

/usr/bin/pod2man -c '' -n 'rsnapshot' -r '' rsnapshot > rsnapshot.1
/usr/bin/pod2man -c '' -n 'rsnapshot-diff' -r '' rsnapshot-diff > rsnapshot-diff.1
make[1]: Entering directory `/usr/src/rsnapshot-1.3.1'
test -z "/usr/local/bin" || mkdir -p -- "/usr/local/bin"
/usr/bin/install -c 'rsnapshot' '/usr/local/bin/rsnapshot'
/usr/bin/install -c 'rsnapshot-diff' '/usr/local/bin/rsnapshot-diff'
test -z "/etc" || mkdir -p -- "/etc"
/usr/bin/install -c -m 644 'rsnapshot.conf.default' '/etc/rsnapshot.conf.default'
test -z "/usr/local/man/man1" || mkdir -p -- "/usr/local/man/man1"
/usr/bin/install -c -m 644 './rsnapshot.1' '/usr/local/man/man1/rsnapshot.1'
/usr/bin/install -c -m 644 './rsnapshot-diff.1' '/usr/local/man/man1/rsnapshot-diff.1'
make[1]: Leaving directory `/usr/src/rsnapshot-1.3.1'


Now, create a new rsnapshot.conf based on the default configuration file that comes with the rsnapshot linux backup utility.

# cp /etc/rsnapshot.conf.default /etc/rsnapshot.conf

Open the /etc/rsnapshot.conf and uncomment the following lines.

# vi /etc/rsnapshot.conf

cmd_cp          /bin/cp
cmd_ssh /usr/bin/ssh
cmd_du          /usr/bin/du
cmd_rsnapshot_diff      /usr/local/bin/rsnapshot-diff
logfile /var/log/rsnapshot

Define your destination backup directories in /etc/rsnapshot.conf as shown below. In this example,

    /etc – source directory that should be backed-up
    localhost/ – destination directory where the backup will be stored. Please note that this directory will be created under /.snapshots/{internal.n}/ directory as shown in the last step.

# vi /etc/rsnapshot.conf

backup  /etc/           localhost/

4. Test rsnapshot Configuration

Perform configuration test to make sure rsnapshot is setup properly and ready to perform linux rsync backup.

# rsnapshot configtest
Syntax OK

5. Verify rsnapshot Hourly Backup Configuration

You can backup linux directories or files at various intervals. By default, the hourly and daily backups are configured.

Verify the hourly backup configuration.

# rsnapshot -t hourly
echo 6490 > /var/run/rsnapshot.pid
mkdir -m 0700 -p /.snapshots/
mkdir -m 0755 -p /.snapshots/hourly.0/
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /home \
/.snapshots/hourly.0/localhost/
mkdir -m 0755 -p /.snapshots/hourly.0/
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc \
/.snapshots/hourly.0/localhost/
mkdir -m 0755 -p /.snapshots/hourly.0/
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
/usr/local /.snapshots/hourly.0/localhost/
touch /.snapshots/hourly.0/

6. Verify rsnapshot Daily Backup Configuration

Verify the daily rsnapshot cwrsync backup process is configured properly.

# rsnapshot -t daily
echo 6493 > /var/run/rsnapshot.pid
mkdir -m 0700 -p /.snapshots/
/.snapshots/hourly.5 not present (yet), nothing to copy

7. Invalid rsnapshot Backup Interval

There is no monthly backup interval. So, if you try to verify monthly backup configuraiton, you may get this message as shown below — ERROR: Interval “monthly” unknown, check /etc/rsnapshot.conf

# rsnapshot -t weekly
echo 6495 > /var/run/rsnapshot.pid
mkdir -m 0700 -p /.snapshots/
/.snapshots/daily.6 not present (yet), nothing to copy

# rsnapshot -t monthly
echo 6496 > /var/run/rsnapshot.pid
mkdir -m 0700 -p /.snapshots/
----------------------------------------------------------------------------
rsnapshot encountered an error! The program was invoked with these options:
/usr/local/bin/rsnapshot -t monthly
----------------------------------------------------------------------------
ERROR: Interval "monthly" unknown, check /etc/rsnapshot.conf

8. Add Crontab Entry for rsnapshot

Once you’ve verified that the rsync hourly and daily backup configurations are setup properly in the rsnapshot cwrsync utility, it is time to set this puppy up in the crontab as shown below.

# crontab -e
0 */4 * * * /usr/local/bin/rsnapshot hourly
30 23 * * * /usr/local/bin/rsnapshot daily

Check out Unix crontab examples article to understand how to setup and configure crontab.
9. Verify the rsnapshot Job Manually (One time check-up)

For the first time, you may want to execute the rsnapshot backup manually to make sure it is working as expected.

By default all the rsync backup taken by rnspashot utility will be stored under /.snapshots directory.

Since rsnapshot was never executed, this directory may not exist as shown below.

# ls -al /.snapshots/
ls: /.snapshots/: No such file or directory

Execute the rsnapshot backup utility manually for hourly configuration as shown below.

# /usr/local/bin/rsnapshot hourly

Since we’ve executed the hourly backup, rsnapshot linux backup utility would’ve created the following /.snapshots directory with the hourly.0 directory

Browse through the /.snapshots/hourly.0/ directoy to make sure the files and directories that you’ve configured in step #3 are backed up accordingly.

# ls -al /.snapshots/
total 16
drwx------  3 root root 4096 Jul 13 05:07 .s
drwxr-xr-x 25 root root 4096 Jul 13 05:07 ..
drwxr-xr-x  3 root root 4096 Jul 13 05:07 hourly.0

# ls -al /.snapshots/hourly.0/localhost/etc/
total 2516
drwxr-xr-x 93 root root   12288 Jul 13 05:05 .
drwxr-xr-x  3 root root    4096 Jul 13 05:07 ..
-rw-r--r--  1 root root   15288 Mar 13  2008 a2ps.cfg
-rw-r--r--  1 root root    2562 Mar 13  2008 a2ps-site.cfg
drwxr-xr-x  4 root root    4096 Oct 31  2008 acpi
-rw-r--r--  1 root root      44 Jan 16 10:43 adjtime
drwxr-xr-x  4 root root    4096 Jan 16 10:36 alchemist
-rw-r--r--  1 root root    2155 Mar 19 08:22 aliases
-rw-r-----  1 root smmsp  12288 Mar 19 08:22 aliases.db

In our next article, let us review how to backup multiple remote hosts using rsnapshot utility.

No comments: