#!/bin/bash
#
# script to synchronize the CDLI archive at UCLA to MPIWG
#
# V0.4.3 (30.6.2006 ROC)
DEBUG=1
LOCKFILE=/var/tmp/cdli-archive-sync.lock
LOGFILE=/var/tmp/cdli-archive-sync.log
EXCLUDEFILE=/export/home/mirror/scripts/rsync-exclude.list
MAILTO=casties@mpiwg-berlin.mpg.de
MIRDIR=/mpiwg/mirror/CDLI/ARCHIVE
REMOTEDIR=/Volumes/cdli_archives
RSYNC=/usr/local/bin/rsync
RSYNCHOST="mpiwgsav@backup.cdli.ucla.edu"
RSYNCOPTS="--delete --exclude-from=$EXCLUDEFILE"
function syncdir () {
#
# sync given directory
#
DIR=$1
REMDIR=${2:-$REMOTEDIR}
if [ -n "$3" ] && [ ${3:1:1} == "-" ]
then
# if the third param starts with "-" it's the options
MOREOPTS=$3
shift
fi
LOCDIR=${3:-$MIRDIR}
echo "***** syncing $DIR on $(date)" >> $LOGFILE
if [ -n $DEBUG ] ; then echo "syncing $DIR on $(date)"; fi
if [ -n $DEBUG ] ; then echo " $RSYNC -ave ssh $RSYNCOPTS $MOREOPTS \"$RSYNCHOST:$REMDIR/$DIR/\" \"$LOCDIR/$DIR/\" >> $LOGFILE 2>&1" >> $LOGFILE ; fi
$RSYNC -ave ssh $RSYNCOPTS $MOREOPTS "$RSYNCHOST:$REMDIR/$DIR/" "$LOCDIR/$DIR/" >> $LOGFILE 2>&1
if [ $? != 0 ]
then
ERROR="${ERROR}ERROR syncing $DIR "
fi
}
#
# check if script is already running
#
if [ -f $LOCKFILE ]
then
if [ -n $DEBUG ] ; then echo "lockfile present! script seems to be running!"; fi
mail -t $MAILTO <<EOF
Subject: CDLI-ARCHIVE-MIRROR: already running!
`ls -l $LOCKFILE `
EOF
exit 1
fi
touch $LOCKFILE
echo "***** started `date`" > $LOGFILE
#
# sync
#
syncdir "cdli_archival"
syncdir "cdli_general" "/Volumes/cdli_freeze" "--exclude=englund/personal/"
syncdir "cdli_rawfiles" "/Volumes/cdli_freeze"
#
# send mail with error log if an error occurred
#
if [ -n "$ERROR" ]
then
# don't send logs bigger than 1MB
LOGSIZE=`ls -l $LOGFILE |cut -c 32-40`
if [ "$LOGSIZE" -gt 900000 ]
then
LOG="LOGFILE too big to send: $LOGSIZE"
else
LOG=`cat $LOGFILE`
fi
mail -t $MAILTO <<EOF
Subject: $ERROR
$LOG
EOF
fi
# remove lock
rm $LOCKFILE
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>