view cdli-archive-sync.sh @ 0:a8eac765e94d default tip

first checkin of mirror scripts.
author casties
date Thu, 16 Mar 2017 18:39:21 +0100
parents
children
line wrap: on
line source

#!/bin/bash
#
# script to synchronize the CDLI archive at UCLA to MPIWG
#
# V0.4.5 (23.3.2009 ROC)

DEBUG=1

LOCKFILE=/tmp/cdli-archive-sync.lock
LOGFILE=/var/tmp/cdli-archive-sync.log
EXCLUDEFILE=/home/mirror/scripts/rsync-exclude.list

MAILTO=casties@mpiwg-berlin.mpg.de

MIRDIR=/mpiwg/mirror/CDLI/ARCHIVE
REMOTEDIR=/Volumes

RSYNC=/usr/bin/rsync
RSYNCHOST="mpiwgsav@backup.cdli.ucla.edu"
RSYNCOPTS="-av --delete --ignore-errors --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 $RSYNCOPTS $MOREOPTS \"$RSYNCHOST:$REMDIR/$DIR/\" \"$LOCDIR/$DIR/\" >> $LOGFILE 2>&1" >> $LOGFILE ; fi
    $RSYNC $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 -s "$( hostname ): CDLI-ARCHIVE-MIRROR: already running!" <<EOF

`ls -l $LOCKFILE `
EOF
    exit 1
fi

touch $LOCKFILE

echo "***** started `date`" > $LOGFILE

#
# sync
#

syncdir "cdli_rawfiles"
syncdir "cdli_archivalfiles"
syncdir "cdli_varia"

#
# 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 -d' ' -f 5`
    if [ "$LOGSIZE" -gt 900000 ]
    then
	LOG="LOGFILE too big to send: $LOGSIZE"
    else
	LOG=`cat $LOGFILE`
    fi

    mail -t $MAILTO -s "$( hostname ): $ERROR" <<EOF

$LOG
EOF
fi

# remove lock

rm $LOCKFILE