From 811f791ad93dcacfbc42a975abd449385ddd8837 Mon Sep 17 00:00:00 2001 From: david Date: Sat, 28 Oct 2023 21:20:53 +0200 Subject: [PATCH] Fixed a bug when repo is locked and simplified healthchecks code in restic-backup.sh --- restic-backup.sh | 51 +++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/restic-backup.sh b/restic-backup.sh index 181ae11..6cc0921 100755 --- a/restic-backup.sh +++ b/restic-backup.sh @@ -16,12 +16,13 @@ ## NAME="restic-backup.sh" -VERSION="0.2.2" +VERSION="0.2.3" AUTHOR="david@socialnerds.org" LICENSE="MIT" DESCRIPTION="A simple script to help setup and run periodic Restic backup jobs." WEBSITE="https://git.socialnerds.org/david/scripts" -CHANGELOG=("[2023-10-27][v0.2.2] Added secret export option" +CHANGELOG=("[2023-10-28][v0.2.3] Fixed a bug when repo is locked, simplified healthchecks code" + "[2023-10-27][v0.2.2] Added secret export option" "[2023-10-27][v0.2.1] Better Healthchecks integration" "[2023-10-26][v0.2.0] Complete rewrite" "[2022-01-01][v0.1.0] Initial version") @@ -32,7 +33,7 @@ CHANGELOG=("[2023-10-27][v0.2.2] Added secret export option" EXECUTABLE="$(basename $0)" LIBRARIES="lib.sh" -DEPENDENCIES="basename dirname readlink tr head chmod curl restic" +DEPENDENCIES="basename dirname readlink tr head chmod cat curl restic" REQUIRE_ROOT=0 # Files @@ -184,50 +185,38 @@ else fi fi - # Initialize a new repo if the URL has no config file - if ! $($RESTIC_COMMAND snapshots >/dev/null 2>&1); then - if ! $($RESTIC_COMMAND init >/dev/null 2>&1); then - lib_print "!A problem occured while initializing a new repository" + if [[ "$1" =~ ^/ && -r "$1" ]]; then + # Initialize a new repo if the URL has no config file + if ! $($RESTIC_COMMAND snapshots >/dev/null 2>&1); then + if ! $($RESTIC_COMMAND init >/dev/null 2>&1); then + lib_print "!A problem occured while initializing a new repository" + exit 1 + fi + lib_print "New repository initialized" + fi + # Run the Restic repository check + if ! $($RESTIC_COMMAND check >/dev/null 2>&1); then + lib_print "!Repository check failed" exit 1 fi - lib_print "New repository initialized" - fi - - # Run the Restic repository check - if ! $($RESTIC_COMMAND check >/dev/null 2>&1); then - lib_print "!Repository check failed" - exit 1 - fi - - if [[ "$1" =~ ^/ && -r "$1" ]]; then # Signal backup start to Healthchecks if enabled - if [[ "$C" -eq 1 ]]; then - HEALTHCHECKS_URL=$(cat $HEALTHCHECKS) - lib_healthchecks "$HEALTHCHECKS_URL/start" "Commencing backup [$1]" - fi + if [[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)/start" "Commencing backup [$1]" # Run the actual backup if ! $($RESTIC_COMMAND backup --one-file-system ${@:2} "$1" >/dev/null 2>&1); then lib_print "!Something went wrong while running backup [$1]" # Signal fail to Healthchecks if enabled - if [[ "$C" -eq 1 ]]; then - lib_healthchecks "$HEALTHCHECKS_URL/fail" "Something went wrong while running backup [$1]" - fi + [[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)/fail" "Something went wrong while running backup [$1]" exit 1 fi # Delete old snapshots if ! $($RESTIC_COMMAND forget --keep-last $SNAPSHOTS --path "$1" --prune >/dev/null 2>&1); then lib_print "!Something went wrong while deleting old snapshots [$1]" # Signal fail to Healthchecks if enabled - if [[ "$C" -eq 1 ]]; then - lib_healthchecks "$HEALTHCHECKS_URL/fail" "Something went wrong while deleting old snapshots [$1]" - fi + [[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)/fail" "Something went wrong while deleting old snapshots [$1]" exit 1 fi # Signal success to Healthchecks if enabled - if [[ "$C" -eq 1 ]]; then - lib_healthchecks "$HEALTHCHECKS_URL/log" "$($RESTIC_COMMAND stats)" - lib_healthchecks "$HEALTHCHECKS_URL" "Backup successfully done [$1]" - fi + [[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)" "Backup successfully done [$1]" elif [[ "$1" =~ ^(backup|cache|cat|check|copy|diff|dump|find|forget|generate|help|init|key|list|ls|migrate|mount|prune|recover|repair|restore|rewrite|snapshots|stats|tag|unlock|version)$ ]]; then # Wrapper mode RESTIC_COMMAND="$RESTIC_BINARY --password-file $PASSWORD --repository-file $REPOSITORY"