add mirror role and enable syncs on mirror group
This commit is contained in:
parent
b2c634425b
commit
278f2deb92
7 changed files with 277 additions and 0 deletions
128
roles/mirror/base/files/sync-mirrors
Executable file
128
roles/mirror/base/files/sync-mirrors
Executable file
|
@ -0,0 +1,128 @@
|
|||
#!/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 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
|
||||
kill -0 "$(cat ${LOCKFILE})"
|
||||
if [ $? -ne 1 ]; then
|
||||
which stat > /dev/null 2>&1
|
||||
if [ $? -eq 0 ]; then
|
||||
STARTED=" ($(stat --format='%y' ${LOCKFILE}))"
|
||||
else
|
||||
STARTED=""
|
||||
fi
|
||||
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} ..."
|
||||
${POSTCMD} 2>&1 | awk \
|
||||
"{ print strftime(\"%Y/%m/%d %H:%M:%S\") \" [$$] \" \$0 }" \
|
||||
>> "${LOGFILE}"
|
||||
logmsg "Finished post for ${mirror} ..."
|
||||
fi
|
||||
done
|
||||
|
||||
rm -f "${LOCKFILE}"
|
Loading…
Add table
Add a link
Reference in a new issue