[PLUG] Rsync Backup Solution over SSH

D. Cooper Stevenson cstevens at gencom.us
Tue Oct 21 10:15:03 UTC 2003


All,

Here's the scenario: you need a backup solution that will make a
complete backup of your server and automatically rotate the backup
snapshots using rsync.

These two scripts will take a snapshot once every four hours (depending
on your crontab entry) and automatically rotate the snapshots
"backward," that is, 'hourly.3' will be deleted, 'hourly.2' will rotate
to 'hourly.3,' 'hourly.1' will rotate to 'hourly.2' and so on. 

Moreover, a snapshot of the server will be taken and the three "dailys"
will rotate in a similar fashion to the "hourlys" at midnight each day.

Here is an excellent article for doing this:

  http://www-106.ibm.com/developerworks/ibm/library/i-metro17.html

 Why rsync? From the article:

* rsync is fast, stable, and mature; it's been around for years and has
served the author well in a multitude of diverse situations.

* rsync was designed to do exactly what we were trying to do -- create
an exact replica of a local data repository on a remote system (or
visa-versa), and keep the two repositories in "sync" with each other.

* rsync is readily available; it ships with most popular Linux
distributions.

While Tom's article is excellent, there's just one catch: this article
talks about performing synchronized backups when an NFS mount is
available. For security reasons NFS was in my case simply not an option.

I adapted the scripts to work via SSH.

Alert: Automating the scripts via cron requires that root on the origin
server be able to log in to the remote server via SSH without a
password. Of course, the origin server must still authenticate itself
with public/private key authentication. The best way to do this securely
is outlined here:

  http://www-106.ibm.com/developerworks/linux/library/l-keyc3/

For your convenience, here is the beginning of the SSH article here:

  http://www-106.ibm.com/developerworks/library/l-keyc.html
  
 To implement the system, perform the following:

1) Install the Korne Shell (ksh) if you do not already have it

2) Copy both hourly_snapshot.sh and daily_snapshot.sh to /usr/bin

3) Configure the origin server to log in as root via SSH without a
password

4) Create a file called /etc/excludes (example below) and add the
directories and/or files you do not wish to back up

5) Modify the REMOTE_SERVER variable in both scripts to point to the
server that will receive the backups and make them executable by root
(chmod 744 /usr/bin/script_name.sh)

6) Modify the SNAPSHOT_RW variable in both scripts to point to the
directory on the remote server you wish the system to backup to

7) Add the following to your crontab: 

  0 */4 * * * /usr/bin/hourly_snapshot.sh
  0 0 * * * /usr/bin/daily_snapshot.sh

Since the scripts are so small I have attached them. I hope you will
forgive me as sending the information as a small file increases the
likelihood of correctness in terms of line breaks, etc. As I mentioned,
an example /etc/excludes file is also included.

I wrote the script for the Korne shell. Why? <\start flame here\>
because ksh is the Cadillac of all shells. It has air conditioning and
power steering. In short, it makes writing scripts really nice.

However, if any of you would like to modify this for Bash, please let me
know; I would be very interested to have that.

I hope you find these scripts useful.


Very Truly Yours,
-- 
--------------------------------------------------------------
| Cooper Stevenson        | Em:  cooper at gencom.us            |
| General Computer        | Ph:  541.924.9434                |
| "Open For Business"     | Www: http://www.gencom.us        |
--------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hourly_snapshot.sh
Type: text/x-ksh
Size: 2408 bytes
Desc: not available
URL: <http://lists.pdxlinux.org/pipermail/plug/attachments/20031021/5c43bc9e/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: daily_snapshot.sh
Type: text/x-ksh
Size: 2218 bytes
Desc: not available
URL: <http://lists.pdxlinux.org/pipermail/plug/attachments/20031021/5c43bc9e/attachment-0001.bin>
-------------- next part --------------
# /etc/excludes
# This file tells rsync wich filesystems to exclude
# to invoke, add --exclude-from=/etc/excludes in the argument to 'rsync'

tmp/
mnt/
proc/
/dev/shm/


More information about the PLUG mailing list