From f80797de04edf5a185a166869f8ca1493794db4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20K=C3=B6rner?= <79668910+netzwergehh@users.noreply.github.com> Date: Thu, 3 Jun 2021 12:25:32 +0200 Subject: [PATCH] Update mirrorsync.sh Added dead process check to file locking --- mirrorsync.sh | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/mirrorsync.sh b/mirrorsync.sh index 51da284..65262f0 100644 --- a/mirrorsync.sh +++ b/mirrorsync.sh @@ -53,6 +53,8 @@ filelistfile="fullfiletimelist-rocky" lockfile="$0.lockfile" logfile="$0.log" +# Check if the filelistfile has changed on upstream mirror +# and exit if it is still the same checkresult=$(${rsync} --no-motd --dry-run --out-format="%n" "${src}/${filelistfile}" "${dst}/${filelistfile}") if [[ -z "$checkresult" ]]; then printf "%s unchanged. Not updating at %(%c)T\n" "$filelistfile" -1 >> "$logfile" 2>&1 @@ -60,17 +62,23 @@ if [[ -z "$checkresult" ]]; then exit 1 fi - -if [[ -e "$lockfile" ]]; then +# Check for existing lockfile to avoid multiple simultaneously running syncs +# If lockfile exists but process is dead continue anyway +if [[ -e "$lockfile" ]] && ! kill -0 "$(< "$lockfile")"; then + printf "Warning: lockfile exists but process dead, continuing.\n" >> "$logfile" 2>&1 + logger -t rsync "Warning: lockfile exists but process dead, continuing with updating ${mirrormodule}." + rm -f "$lockfile" +elif [[ -e "$lockfile" ]]; then printf "Update already in progress at %(%c)T\n" -1 >> "$logfile" 2>&1 logger -t rsync "Not updating ${mirrormodule}: already in progress." -else - touch $lockfile - printf "Started update at %(%c)T\n" -1 >> "$logfile" 2>&1 - logger -t rsync "Updating ${mirrormodule}" - - ${rsync} "${opts[@]}" "${src}/" "${dst}/" >> "$logfile" 2>&1 - logger -t rsync "Finished updating ${mirrormodule}" - printf "End: %(%c)T\n" -1 >> "$logfile" 2>&1 - rm -f $lockfile + exit 1 fi + +# The actual syncing part +printf '%s\n' "$$" > "$lockfile" +printf "Started update at %(%c)T\n" -1 >> "$logfile" 2>&1 +logger -t rsync "Updating ${mirrormodule}" +${rsync} "${opts[@]}" "${src}/" "${dst}/" >> "$logfile" 2>&1 +logger -t rsync "Finished updating ${mirrormodule}" +printf "End: %(%c)T\n" -1 >> "$logfile" 2>&1 +rm -f $lockfile