rsync-common.sh

From NetBSD Wiki

Jump to: navigation, search
  1. Shell script to run rsync updates
#!/usr/pkg/bin/bash
#$Id: rsync-common.sh 382 2009-02-21 00:03:23Z seklecki $

# Developed by Brian Seklecki <lavalamp@spiritual-machines.org>
# and Matthew Sporleder <msporleder@gmail.com> for use Mirroring NetBSD a la:
# http://wiki.netbsd.se/Mirroring_NetBSD
# Standard *BSD licensing terms please!

DEBUG=0

if [ ${DEBUG} -gt 0 ]; then
	echo "$0 Starting at $(date)" | logger -p user.info
fi

function usage () {
	echo "$0 [conf_File]"
}

if [ -z $1 ]; then 
	usage
	exit 145
fi

CONF_FILE=$1

if [ ! -r $CONF_FILE ]; then 
	echo "Unable to read configuration file: ${CONF_FILE}" 
	exit 146
else
	source ${CONF_FILE}
	if [ ${DEBUG} -gt 0 ]; then
		 echo "Reading config file: ${CONF_FILE}"
	fi
fi


if [ -z ${BASEDIR} ]; then
	BASEDIR=/export
fi

if [ -z "${EXCLD_FLAGS}" ]; then
	EXCLD_FLAGS="--exclude *wankers*"
fi

if [ -z "${RSYNC_FLAGS}" ]; then
	RSYNC_FLAGS="-Wartz -4 --no-g --no-o ${EXCLD_FLAGS} "
fi

if [ -z "${RSYNC_SOURCE}" ]; then 
	RSYNC_SOURCE=rsync://rsync.NetBSD.org/NetBSD-daily
fi

if [ -z "${LOCK_FILE}" ]; then
	LOCK_FILE="${BASEDIR}/.lock"
fi

if [ -z "${LOG_FILE}" ]; then
	LOG_FILE="${BASEDIR}/.log"
fi

if [ -z "${STATUS_FILE}" ]; then
	STAUS_FILE="${BASEDIR}/.log"
fi


# check to see if a lock file exists
if [ -f ${LOCK_FILE} ]; then
	echo "Lock file exists -- another instance running?  Possibly stale from previous unclean exit." | tee -a ${LOG_FILE}
	cat ${LOCK_FILE}
	exit 2
else
	echo $$ > ${LOCK_FILE}
fi


if [ ! -d ${BASEDIR} -a  ! -w ${BASEDIR} ]; then
	echo "Unable to switch to base dir or not valid destination .. " | tee -a ${LOG_FILE}
else
	cd ${BASEDIR}
fi

MAX_BUSY_RETRYS=5
BUSY_RETRY_SLEEP=1200

CURRENT_TRY=0

while [ ${CURRENT_TRY} -lt ${MAX_BUSY_RETRYS} ]; do
        $RSYNC $RSYNC_FLAGS $RSYNC_SOURCE ./ 2>&1  3>&1 >>${LOG_FILE}
        RSYNC_RETURN=$?

        if [ ${RSYNC_RETURN} -eq 0 ]; then
                if [ ${DEBUG} -gt 0 ]; then 
			echo "$0 clean exit - leaving loop" | tee -a ${LOG_FILE}
		fi
                break;
        fi

        if [ ${RSYNC_RETURN} -eq 5 ]; then
                echo "$0: Server busy at $(date)" >> ${LOG_FILE}
                echo "$0: Max cons or other resource contention?" >> ${LOG_FILE}
		echo "$0: Activating backoff algorithm: Sleep for $((${BUSY_RETRY_SLEEP} * ${CURRENT_TRY}))" >> ${LOG_FILE}
		if [ ${DEBUG} -gt 0 ]; then
			echo "$0: Server resource contention problems at $(date); backing off." | tee -a ${LOG_FILE}
		fi
                sleep $((${BUSY_RETRY_SLEEP} * ${CURRENT_TRY}))
        fi
	CURRENT_TRY=$((${CURRENT_TRY} + 1))
done

if [ $? -ne 0 ]; then
	echo "RSync did not exit cleanly -- check log files!! Status was: ${RSYNC_RETURN}" | tee -a ${LOG_FILE}
	tail -3 ${LOG_FILE}
	rm ${LOCK_FILE}

else 
        if [ ${DEBUG} -gt 0 ]; then
		echo "RSync existed cleanly -- updating last clean update status file." | tee -a ${LOG_FILE}
	fi
	touch -m ${STATUS_FILE}
	rm ${LOCK_FILE}

fi

echo ${RSYNC_RESULT} >> ${LOG_FILE}
Personal tools