fix restic locking

This commit is contained in:
Ryan Cavicchioni 2020-08-22 10:01:56 -05:00
parent 8a5ddd5273
commit ba8f63cda4
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 32 additions and 3 deletions

View File

@ -76,12 +76,12 @@ if [ -f "$LOCK" ]; then
if ! [[ $cmdline =~ $(basename "$0") ]]; then
printf "removing orphaned lock, pid %d belongs to another process\n" "$pid"
rm -f "$LOCK"
fi
else
KEEP_LOCK=1
error_exit "another job is running, pid=${pid}"
fi
fi
fi
fi
echo $$ > "$LOCK"

View File

@ -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" \