February 2005
rsync on Demand Script: Part OneIn the daily life of a system administrator (whether at work or play) the issue of backups invariably comes up. The eventual plan is to use a CDRW or DVDRW/CDRW to permanently and periodically backup select data files. Surprisingly, there is not much data that needs backed up so cdroms are ideal. In the meantime, however, duplication is the best bet. To tackle the problem, there are a variety of approaches.
The options considered were not very many and rest assured there are a myriad more to choose from. Note that the requirements are for disaster, not versioning (use CVS for that).
Each case has its own merits. Secure copying everything seems a bit heavy handed for the actual copying while using NFS for backups is somewhat inappropriate. FTP - too crude. Using rsync in one of two modes, daemon mode or on demand makes the most sense since the requirement is for a single machine disaster mitigation.
In all fairness, straight from cron on the system the files are going to, the following works just fine as long as public-key is setup for the ssh account in use:
rsync -az --delete -e ssh hostname_or_ip:/path/to/sync/src /path/to/dst
In cron the command can be run that way - but for a few things:
It is always nice to leave the door open, however.
sync_on_demand.sh ScriptThe script is, for now, incredibly simple. Here is the whole script, then a dissection to follow:
#!/bin/sh
progname=${0##*/}
toppid=$$
PROTO=ssh
UTIL=rsync
UTIL_FLAGS="-az --delete -e $PROTO"
bomb()
{
cat >&2 <<ERRORMESSAGE
ERROR: $@
*** ${progname} aborted ***
ERRORMESSAGE
kill ${toppid}
exit 1
}
for i in $PROTO $UTIL
do
if ! type ${i} >/dev/null; then
bomb "${i} not found"
fi
done
if ! type getopts >/dev/null 2>&1; then
bomb "/bin/sh shell is too old; try ksh or bash"
fi
while getopts s:d: ch; do
case ${ch} in
s)
SRC=${OPTARG}
;;
d)
DST=${OPTARG}
;;
esac
done
shift $((${OPTIND} - 1))
$UTIL $UTIL_FLAGS $SRC $DST ||
bomb "Could not run ${UTIL} ${UTIL_FLAGS} ${SRC} ${DST}
exit 0
#!/bin/sh
progname=${0##*/}
toppid=$$
PROTO=ssh UTIL=rsync UTIL_FLAGS="-az --delete -e $PROTO"
bomb() Routine
bomb()
{
cat >&2 <<ERRORMESSAGE
ERROR: $@
*** ${progname} aborted ***
ERRORMESSAGE
kill ${toppid}
exit 1
}
for i in $PROTO $UTIL
do
if ! type ${i} >/dev/null; then
bomb "${i} not found"
fi
done
if ! type getopts >/dev/null 2>&1; then
bomb "/bin/sh shell is too old; try ksh or bash"
fi
getopts.
while getopts s:d: ch; do
case ${ch} in
s)
SRC=${OPTARG}
;;
d)
DST=${OPTARG}
;;
esac
done
shift $((${OPTIND} - 1))
-s and -d options
for source and destination respectively.
$UTIL $UTIL_FLAGS $SRC $DST ||
bomb "Could not run ${UTIL} ${UTIL_FLAGS} ${SRC} ${DST}
Put it all together and run the rsync on demand:
sync_on_demand.sh -s atlas:/home/foo -d /home/foo/atlas_home
Note that the destination directory is not called something like
~/atlas_home.foo, because the particular arguments used
will create ~/atlas_home/foo which makes it easier to
add more home directories later.
The first and foremost is that it can be copied to any system and used. Note that the source can be going from one computer to another versus from the destination computer, they are in fact interchangeable and only distinguished to make sense of the script itself.
The script is missing a few things that might be nice:
In part two, the aforementioned missing items (and possibly more as they surface) will be addressed.
Next: Rsync Script Part Two
(based on last 2 months log reports)