scripting rsync

here’s the script use to run rsync — with a little modification to the names and ip addresses for security reasons — but its essentially the same. i have to run this through cron because the server is headless — no monitor. i just access it via the ssh or browser from another computer.

#!/bin/sh

DATETMP=`date +%Y.%m.%d`
RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/rsyncuser/.ssh/id_ppib
RHOST=remote.ip.addr.ess
RPATH=/remotedata/
LPATH=/localdata/
LOGFILE=/rsyncuser/rlog.$DATETMP.log
EXCLUDES=/rsyncuser/ppiexcludes
OPTS="--exclude-from=$EXCLUDES"
RUN=`ps x | grep rsync | grep -v grep | wc -l`

if [ "$RUN" -gt 0 ]; then
# echo rsync already running
exit 1
fi

$RSYNC -avz -e "$SSH -i $KEY" $OPTS $RHOST:$RPATH $LPATH >> $LOGFILE

i leave an entry in crontab to run the script once each day. sometimes a backup run goes longer than 24 hours so i needed to check if rsync is already running in the server. if the script doesn’t check, it will run another instance and would slow down the server or, worse, brings it down completely.

i use the exclude-from option to exclude files that shouldn’t be backed up — music and movies — or else the backup will take too long especially on just a dsl line.

note that the last two lines is really just one line in the script. the blog just wraps it for clarity.

if you notice anything wrong with the script, leave a comment.

comments are closed.