128 lines
3.1 KiB
Bash
Executable file
128 lines
3.1 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
LOCKFILE="/var/run/sync-mirrors/lockfile"
|
|
LOGFILE="/var/log/sync-mirrors/sync-mirrors-$(date +%Y%m%d%H%M%S).log"
|
|
CONFDIR="/etc/sync-mirrors"
|
|
|
|
usage() {
|
|
echo "Usage: $(basename "$0") [-v] [mirror]" 1>&2
|
|
echo " $(basename "$0") -l" 1>&2
|
|
}
|
|
|
|
logmsg() {
|
|
[ "${VERBOSE}" -eq 1 ] && echo "$1"
|
|
echo "$(date '+%Y/%m/%d %H:%M:%S') [$$] $1" >> "${LOGFILE}"
|
|
}
|
|
|
|
if [ -d ${CONFDIR} ]; then
|
|
MIRRORLIST="$(find ${CONFDIR}/ -name \*.conf | while read -r f ; \
|
|
do basename "${f}" | sed -e 's/\.conf$//' ; done)"
|
|
if [ "${MIRRORLIST}" = "" ]; then
|
|
echo "ERR: No configured mirrors found" 1>&2
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "ERR: Config directory [${CONFDIR}] missing" 1>&2
|
|
exit 1
|
|
fi
|
|
|
|
VERBOSE=0
|
|
NOOP=""
|
|
EXTRA_OPTS=""
|
|
while getopts "vhln" c ; do
|
|
case $c in
|
|
v)
|
|
VERBOSE=1
|
|
EXTRA_OPTS="${EXTRA_OPTS} -v --progress"
|
|
;;
|
|
h)
|
|
usage
|
|
exit 1
|
|
;;
|
|
l)
|
|
echo "Available mirrors:"
|
|
for name in ${MIRRORLIST} ; do
|
|
echo " ${name}"
|
|
done
|
|
exit 0
|
|
;;
|
|
n)
|
|
NOOP=" (DRY RUN)"
|
|
EXTRA_OPTS="${EXTRA_OPTS} -n"
|
|
;;
|
|
*)
|
|
usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
shift "$((OPTIND - 1))"
|
|
|
|
if [ $# -gt 0 ]; then
|
|
for mirror in "$@" ; do
|
|
if [ ! -f "${CONFDIR}/$1.conf" ]; then
|
|
echo "ERR: No mirror named [$1]" 1>&2
|
|
exit 1
|
|
fi
|
|
SYNC="${MIRRORS} $1"
|
|
shift
|
|
done
|
|
else
|
|
SYNC="${MIRRORLIST}"
|
|
fi
|
|
|
|
if [ "$(whoami)" != "mirror" ]; then
|
|
echo "ERR: Script needs to be run as mirror user" 1>&2
|
|
exit 1
|
|
fi
|
|
|
|
umask 022
|
|
|
|
if [ -f "${LOCKFILE}" ]; then
|
|
if kill -0 "$(cat ${LOCKFILE})" ; then
|
|
STARTED=" ($(stat --format='%y' ${LOCKFILE}))"
|
|
echo "ERR: Lockfile exists${STARTED}, exiting" 1>&2
|
|
exit 1
|
|
else
|
|
echo "WARN: Removing stale lock file..." 1>&2
|
|
rm -f "${LOCKFILE}"
|
|
fi
|
|
fi
|
|
trap 'rm -f ${LOCKFILE}' INT TERM EXIT
|
|
echo "$$" > "${LOCKFILE}"
|
|
|
|
for mirror in ${SYNC} ; do
|
|
POSTCMD=""
|
|
SRC=""
|
|
RSYNCOPTS=""
|
|
. "${CONFDIR}/${mirror}.conf"
|
|
if [ "${SRC}" = "" ]; then
|
|
echo "ERR: No SRC set for mirror ${mirror} ..." 1>&2
|
|
exit 1
|
|
fi
|
|
logmsg "Starting ${mirror} sync${NOOP}..."
|
|
rsync -aH -4 ${EXTRA_OPTS} --numeric-ids --delete --delete-delay \
|
|
--delay-updates --no-motd ${RSYNCOPTS} --log-file="${LOGFILE}" \
|
|
--exclude=.~tmp~/ "${SRC}" "/srv/mirrors/${mirror}/"
|
|
STATUS=$?
|
|
if [ ${STATUS} -ne 0 ]; then
|
|
echo "WARN: Encountered errors on ${mirror} sync, see ${LOGFILE} for details" 1>&2
|
|
fi
|
|
logmsg "Finished ${mirror} sync with exit status ${STATUS}${NOOP} ..."
|
|
if [ "${POSTCMD}" != "" ]; then
|
|
logmsg "Running post for ${mirror} ..."
|
|
if [ "${VERBOSE}" -eq 1 ]; then
|
|
${POSTCMD} 2>&1 | tee >( \
|
|
awk "{ print strftime(\"%Y/%m/%d %H:%M:%S\") \" [$$] \" \$0 }" \
|
|
>> "${LOGFILE}" )
|
|
else
|
|
${POSTCMD} 2>&1 | \
|
|
awk "{ print strftime(\"%Y/%m/%d %H:%M:%S\") \" [$$] \" \$0 }" \
|
|
>> "${LOGFILE}"
|
|
fi
|
|
logmsg "Finished post for ${mirror} ..."
|
|
fi
|
|
done
|
|
|
|
rm -f "${LOCKFILE}"
|