From ba8f63cda4c6de958da8b7d8214c2bfde2d9b900 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 22 Aug 2020 10:01:56 -0500 Subject: [PATCH] fix restic locking --- roles/restic/files/restic-job.sh | 6 +++--- roles/restic/files/restic-tidy.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/roles/restic/files/restic-job.sh b/roles/restic/files/restic-job.sh index c757150..7fa6d15 100755 --- a/roles/restic/files/restic-job.sh +++ b/roles/restic/files/restic-job.sh @@ -76,10 +76,10 @@ if [ -f "$LOCK" ]; then if ! [[ $cmdline =~ $(basename "$0") ]]; then printf "removing orphaned lock, pid %d belongs to another process\n" "$pid" rm -f "$LOCK" + else + KEEP_LOCK=1 + error_exit "another job is running, pid=${pid}" fi - else - KEEP_LOCK=1 - error_exit "another job is running, pid=${pid}" fi fi fi diff --git a/roles/restic/files/restic-tidy.sh b/roles/restic/files/restic-tidy.sh index ab7054a..be4d9c9 100755 --- a/roles/restic/files/restic-tidy.sh +++ b/roles/restic/files/restic-tidy.sh @@ -8,6 +8,16 @@ error_exit() { } RESTIC_ETC_PATH=${RESTIC_ETC_PATH:-/etc/restic} +LOCK_PATH=/run/restic +LOCK="${LOCK_PATH}/tidy.lock" +KEEP_LOCK= + +function finish { + if [ -z $KEEP_LOCK ]; then + rm -f "$LOCK" + fi +} +trap finish EXIT # shellcheck source=/dev/null source "${RESTIC_ETC_PATH}/env.sh" @@ -43,6 +53,25 @@ KEEP_WEEKLY=${KEEP_WEEKLY:-5} KEEP_MONTHLY=${KEEP_MONTHLY:-12} KEEP_YEARLY=${KEEP_YEARLY:-10} +if [ -f "$LOCK" ]; then + pid=$(cat "$LOCK") + if ! kill -0 "$pid" 2> /dev/null; then + printf "removing orphaned lock, pid %d does not exist\n" "$pid" + rm -f "$LOCK" + else + if [[ -f "/proc/${pid}/cmdline" ]]; then + cmdline=$(tr "\0" " " <"/proc/${pid}/cmdline") + if ! [[ $cmdline =~ $(basename "$0") ]]; then + printf "removing orphaned lock, pid %d belongs to another process\n" "$pid" + rm -f "$LOCK" + else + KEEP_LOCK=1 + error_exit "another job is running, pid=${pid}" + fi + fi + fi +fi + printf "started, keep hourly:%d daily:%d weekly:%d monthly:%d year:%d\n" \ "$KEEP_HOURLY" \ "$KEEP_DAILY" \