Skip to content

Commit 2abae26

Browse files
authored
(fix) Added the ability to check for stale locks #201 (#205)
1 parent 18cc11b commit 2abae26

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

scripts/opt/backup-loop.sh

+24
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,27 @@ restic() {
369369
# shellcheck disable=SC2086
370370
command restic forget --tag "${restic_tags_filter}" ${PRUNE_RESTIC_RETENTION} "${@}"
371371
}
372+
373+
_unlock() {
374+
if ! [ -z "${output=$(command restic list locks 2>&1)}" ];then
375+
log WARN "Confirmed stale lock on repo, unlocking..."
376+
if [[ unlock=$(command restic unlock 2>&1) == *"success"* ]]; then
377+
log INFO "Successfully unlocked the repo"
378+
else
379+
log ERROR "Unable to unlock the repo. Is there another process running?"
380+
return 1
381+
fi
382+
fi
383+
}
384+
372385
_check() {
373386
if ! output="$(command restic check 2>&1)"; then
374387
log ERROR "Repository contains error! Aborting"
375388
<<<"${output}" log ERROR
376389
return 1
377390
fi
378391
}
392+
379393
init() {
380394
if [ -z "${RESTIC_PASSWORD:-}" ] \
381395
&& [ -z "${RESTIC_PASSWORD_FILE:-}" ] \
@@ -389,6 +403,9 @@ restic() {
389403
fi
390404
if output="$(command restic snapshots 2>&1 >/dev/null)"; then
391405
log INFO "Repository already initialized"
406+
log INFO "Checking for stale locks"
407+
_unlock
408+
log INFO "Checking repo integrity"
392409
_check
393410
elif <<<"${output}" grep -q '^Is there a repository at the following location?$'; then
394411
log INFO "Initializing new restic repository..."
@@ -397,6 +414,12 @@ restic() {
397414
<<<"${output}" log ERROR
398415
log ERROR "Wrong password provided to an existing repository?"
399416
return 1
417+
elif <<<"${output}" grep -q 'repository is already locked exclusively'; then
418+
<<<"${output}" log ERROR
419+
log INFO "Detected stale lock, confirming..."
420+
_unlock
421+
log INFO "Checking repo integrity"
422+
_check
400423
else
401424
<<<"${output}" log ERROR
402425
log INTERNALERROR "Unhandled restic repository state."
@@ -442,6 +465,7 @@ restic() {
442465
call_if_function_exists "${@}"
443466
}
444467

468+
445469
rclone() {
446470
readarray -td, includes_patterns < <(printf '%s' "${INCLUDES:-.}")
447471

0 commit comments

Comments
 (0)