@@ -369,13 +369,27 @@ restic() {
369
369
# shellcheck disable=SC2086
370
370
command restic forget --tag " ${restic_tags_filter} " ${PRUNE_RESTIC_RETENTION} " ${@ } "
371
371
}
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
+
372
385
_check () {
373
386
if ! output=" $( command restic check 2>&1 ) " ; then
374
387
log ERROR " Repository contains error! Aborting"
375
388
<<< " ${output}" log ERROR
376
389
return 1
377
390
fi
378
391
}
392
+
379
393
init () {
380
394
if [ -z " ${RESTIC_PASSWORD:- } " ] \
381
395
&& [ -z " ${RESTIC_PASSWORD_FILE:- } " ] \
@@ -389,6 +403,9 @@ restic() {
389
403
fi
390
404
if output=" $( command restic snapshots 2>&1 > /dev/null) " ; then
391
405
log INFO " Repository already initialized"
406
+ log INFO " Checking for stale locks"
407
+ _unlock
408
+ log INFO " Checking repo integrity"
392
409
_check
393
410
elif <<< " ${output}" grep -q ' ^Is there a repository at the following location?$' ; then
394
411
log INFO " Initializing new restic repository..."
@@ -397,6 +414,12 @@ restic() {
397
414
<<< " ${output}" log ERROR
398
415
log ERROR " Wrong password provided to an existing repository?"
399
416
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
400
423
else
401
424
<<< " ${output}" log ERROR
402
425
log INTERNALERROR " Unhandled restic repository state."
@@ -442,6 +465,7 @@ restic() {
442
465
call_if_function_exists " ${@ } "
443
466
}
444
467
468
+
445
469
rclone () {
446
470
readarray -td, includes_patterns < <( printf ' %s' " ${INCLUDES:- .} " )
447
471
0 commit comments