Fixed a bug when repo is locked and simplified healthchecks code in restic-backup.sh
This commit is contained in:
parent
d268b96a4c
commit
811f791ad9
|
@ -16,12 +16,13 @@
|
||||||
##
|
##
|
||||||
|
|
||||||
NAME="restic-backup.sh"
|
NAME="restic-backup.sh"
|
||||||
VERSION="0.2.2"
|
VERSION="0.2.3"
|
||||||
AUTHOR="david@socialnerds.org"
|
AUTHOR="david@socialnerds.org"
|
||||||
LICENSE="MIT"
|
LICENSE="MIT"
|
||||||
DESCRIPTION="A simple script to help setup and run periodic Restic backup jobs."
|
DESCRIPTION="A simple script to help setup and run periodic Restic backup jobs."
|
||||||
WEBSITE="https://git.socialnerds.org/david/scripts"
|
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-27][v0.2.1] Better Healthchecks integration"
|
||||||
"[2023-10-26][v0.2.0] Complete rewrite"
|
"[2023-10-26][v0.2.0] Complete rewrite"
|
||||||
"[2022-01-01][v0.1.0] Initial version")
|
"[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)"
|
EXECUTABLE="$(basename $0)"
|
||||||
LIBRARIES="lib.sh"
|
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
|
REQUIRE_ROOT=0
|
||||||
|
|
||||||
# Files
|
# Files
|
||||||
|
@ -184,50 +185,38 @@ else
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Initialize a new repo if the URL has no config file
|
if [[ "$1" =~ ^/ && -r "$1" ]]; then
|
||||||
if ! $($RESTIC_COMMAND snapshots >/dev/null 2>&1); then
|
# Initialize a new repo if the URL has no config file
|
||||||
if ! $($RESTIC_COMMAND init >/dev/null 2>&1); then
|
if ! $($RESTIC_COMMAND snapshots >/dev/null 2>&1); then
|
||||||
lib_print "!A problem occured while initializing a new repository"
|
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
|
exit 1
|
||||||
fi
|
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
|
# Signal backup start to Healthchecks if enabled
|
||||||
if [[ "$C" -eq 1 ]]; then
|
if [[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)/start" "Commencing backup [$1]"
|
||||||
HEALTHCHECKS_URL=$(cat $HEALTHCHECKS)
|
|
||||||
lib_healthchecks "$HEALTHCHECKS_URL/start" "Commencing backup [$1]"
|
|
||||||
fi
|
|
||||||
# Run the actual backup
|
# Run the actual backup
|
||||||
if ! $($RESTIC_COMMAND backup --one-file-system ${@:2} "$1" >/dev/null 2>&1); then
|
if ! $($RESTIC_COMMAND backup --one-file-system ${@:2} "$1" >/dev/null 2>&1); then
|
||||||
lib_print "!Something went wrong while running backup [$1]"
|
lib_print "!Something went wrong while running backup [$1]"
|
||||||
# Signal fail to Healthchecks if enabled
|
# Signal fail to Healthchecks if enabled
|
||||||
if [[ "$C" -eq 1 ]]; then
|
[[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)/fail" "Something went wrong while running backup [$1]"
|
||||||
lib_healthchecks "$HEALTHCHECKS_URL/fail" "Something went wrong while running backup [$1]"
|
|
||||||
fi
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
# Delete old snapshots
|
# Delete old snapshots
|
||||||
if ! $($RESTIC_COMMAND forget --keep-last $SNAPSHOTS --path "$1" --prune >/dev/null 2>&1); then
|
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]"
|
lib_print "!Something went wrong while deleting old snapshots [$1]"
|
||||||
# Signal fail to Healthchecks if enabled
|
# Signal fail to Healthchecks if enabled
|
||||||
if [[ "$C" -eq 1 ]]; then
|
[[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)/fail" "Something went wrong while deleting old snapshots [$1]"
|
||||||
lib_healthchecks "$HEALTHCHECKS_URL/fail" "Something went wrong while deleting old snapshots [$1]"
|
|
||||||
fi
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
# Signal success to Healthchecks if enabled
|
# Signal success to Healthchecks if enabled
|
||||||
if [[ "$C" -eq 1 ]]; then
|
[[ "$C" -eq 1 ]] && lib_healthchecks "$(cat $HEALTHCHECKS)" "Backup successfully done [$1]"
|
||||||
lib_healthchecks "$HEALTHCHECKS_URL/log" "$($RESTIC_COMMAND stats)"
|
|
||||||
lib_healthchecks "$HEALTHCHECKS_URL" "Backup successfully done [$1]"
|
|
||||||
fi
|
|
||||||
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
|
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
|
# Wrapper mode
|
||||||
RESTIC_COMMAND="$RESTIC_BINARY --password-file $PASSWORD --repository-file $REPOSITORY"
|
RESTIC_COMMAND="$RESTIC_BINARY --password-file $PASSWORD --repository-file $REPOSITORY"
|
||||||
|
|
Loading…
Reference in New Issue