mirror: Refactoring sync-mirrors script
This commit is contained in:
parent
205b82f1d8
commit
770d6a74d3
2 changed files with 46 additions and 49 deletions
|
@ -1,4 +1,7 @@
|
|||
#!/bin/bash
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
umask 022
|
||||
|
||||
LOCKFILE="/var/run/sync-mirrors/lockfile"
|
||||
LOGFILE="/var/log/sync-mirrors/sync-mirrors-$(date +%Y%m%d%H%M%S).log"
|
||||
|
@ -9,30 +12,35 @@ usage() {
|
|||
echo " $(basename "$0") -l" 1>&2
|
||||
}
|
||||
|
||||
logmsg() {
|
||||
[ "${VERBOSE}" -eq 1 ] && echo "$1"
|
||||
echo "$(date '+%Y/%m/%d %H:%M:%S') [$$] $1" >> "${LOGFILE}"
|
||||
list_mirrors() {
|
||||
for f in "$CONFDIR"/*.conf ; do
|
||||
basename "$f" ".conf"
|
||||
done
|
||||
}
|
||||
|
||||
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
|
||||
logmsg() {
|
||||
"$VERBOSE" && echo "$1"
|
||||
echo "$(date '+%Y/%m/%d %H:%M:%S') [$$] $1" >> "$LOGFILE"
|
||||
}
|
||||
|
||||
logstream() {
|
||||
while read -r line; do
|
||||
logmsg "$line"
|
||||
done
|
||||
}
|
||||
|
||||
if [ ! -d "$CONFDIR" ]; then
|
||||
echo "ERR: Config directory [${CONFDIR}] missing" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VERBOSE=0
|
||||
VERBOSE=false
|
||||
NOOP=""
|
||||
EXTRA_OPTS=""
|
||||
while getopts "vhln" c ; do
|
||||
case $c in
|
||||
v)
|
||||
VERBOSE=1
|
||||
VERBOSE=true
|
||||
EXTRA_OPTS="${EXTRA_OPTS} -v --progress"
|
||||
;;
|
||||
h)
|
||||
|
@ -41,9 +49,7 @@ while getopts "vhln" c ; do
|
|||
;;
|
||||
l)
|
||||
echo "Available mirrors:"
|
||||
for name in ${MIRRORLIST} ; do
|
||||
echo " ${name}"
|
||||
done
|
||||
list_mirrors | sed -e 's/^/ /'
|
||||
exit 0
|
||||
;;
|
||||
n)
|
||||
|
@ -59,17 +65,19 @@ done
|
|||
|
||||
shift "$((OPTIND - 1))"
|
||||
|
||||
if [ $# -gt 0 ]; then
|
||||
if [ $# -eq 0 ]; then
|
||||
set -- $(list_mirrors)
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "ERR: No configured mirrors found" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
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
|
||||
|
@ -77,52 +85,41 @@ if [ "$(whoami)" != "mirror" ]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
umask 022
|
||||
|
||||
if [ -f "${LOCKFILE}" ]; then
|
||||
if kill -0 "$(cat ${LOCKFILE})" ; then
|
||||
STARTED=" ($(stat --format='%y' ${LOCKFILE}))"
|
||||
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}"
|
||||
rm -f "$LOCKFILE"
|
||||
fi
|
||||
fi
|
||||
trap 'rm -f ${LOCKFILE}' INT TERM EXIT
|
||||
echo "$$" > "${LOCKFILE}"
|
||||
trap 'rm -f $LOCKFILE' INT TERM EXIT
|
||||
echo "$$" > "$LOCKFILE"
|
||||
|
||||
for mirror in ${SYNC} ; do
|
||||
for mirror in "$@" ; do
|
||||
POSTCMD=""
|
||||
SRC=""
|
||||
RSYNCOPTS=""
|
||||
# shellcheck source=/dev/null
|
||||
. "${CONFDIR}/${mirror}.conf"
|
||||
if [ "${SRC}" = "" ]; then
|
||||
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}/"
|
||||
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
|
||||
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
|
||||
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
|
||||
$POSTCMD 2>&1 | logstream
|
||||
logmsg "Finished post for ${mirror} ..."
|
||||
fi
|
||||
done
|
||||
|
||||
rm -f "${LOCKFILE}"
|
|
@ -70,7 +70,7 @@
|
|||
- name: Copy mirroring script
|
||||
ansible.builtin.copy:
|
||||
dest: /usr/local/bin/sync-mirrors
|
||||
src: sync-mirrors
|
||||
src: sync-mirrors.sh
|
||||
mode: "0755"
|
||||
owner: root
|
||||
group: root
|
||||
|
|
Loading…
Add table
Reference in a new issue