Better Healthchecks integration for restic-backup.sh
This commit is contained in:
parent
9d6c642019
commit
fbddd00da3
10
lib.sh
10
lib.sh
|
@ -132,3 +132,13 @@ function lib_missing_commands() {
|
|||
function lib_gen_string() {
|
||||
LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c ${1:-16}; echo
|
||||
}
|
||||
|
||||
# Contact Healthchecks
|
||||
# The first argument($1) must be the URL everything after that gets transmitted as log message
|
||||
function lib_healthchecks() {
|
||||
if [[ "$2" ]]; then
|
||||
curl -fsS -m 10 --retry 5 --data-raw "${*:2}" -o /dev/null "$1"
|
||||
elif [[ "$1" ]]; then
|
||||
curl -fsS -m 10 --retry 5 -o /dev/null "$1"
|
||||
fi
|
||||
}
|
||||
|
|
|
@ -16,12 +16,13 @@
|
|||
##
|
||||
|
||||
NAME="restic-backup.sh"
|
||||
VERSION="0.2.0"
|
||||
VERSION="0.2.1"
|
||||
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-26][v0.2.0] Complete rewrite"
|
||||
CHANGELOG=("[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")
|
||||
|
||||
##
|
||||
|
@ -182,35 +183,37 @@ else
|
|||
fi
|
||||
|
||||
if [[ "$1" =~ ^/ && -r "$1" ]]; then
|
||||
# Signal Healthchecks start if enabled
|
||||
# Signal backup start to Healthchecks if enabled
|
||||
if [[ "$C" -eq 1 ]]; then
|
||||
HEALTHCHECKS_URL=$(cat $HEALTHCHECKS)
|
||||
curl -fsS -m 10 --retry 5 -o /dev/null $HEALTHCHECKS_URL/start
|
||||
lib_healthchecks "$HEALTHCHECKS_URL/start" "Commencing backup [$1]"
|
||||
fi
|
||||
# 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 Healthchecks fail if enabled
|
||||
# Signal fail to Healthchecks if enabled
|
||||
if [[ "$C" -eq 1 ]]; then
|
||||
curl -fsS -m 10 --retry 5 -o /dev/null $HEALTHCHECKS_URL/fail >/dev/null 2>&1
|
||||
lib_healthchecks "$HEALTHCHECKS_URL/fail" "Something went wrong while running backup [$1]"
|
||||
fi
|
||||
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 Healthchecks fail if enabled
|
||||
# Signal fail to Healthchecks if enabled
|
||||
if [[ "$C" -eq 1 ]]; then
|
||||
curl -fsS -m 10 --retry 5 -o /dev/null $HEALTHCHECKS_URL/fail >/dev/null 2>&1
|
||||
lib_healthchecks "$HEALTHCHECKS_URL/fail" "Something went wrong while deleting old snapshots [$1]"
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
# Signal Healthchecks success if enabled
|
||||
# Signal success to Healthchecks if enabled
|
||||
if [[ "$C" -eq 1 ]]; then
|
||||
curl -fsS -m 10 --retry 5 -o /dev/null $HEALTHCHECKS_URL >/dev/null 2>&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
|
||||
# Wrapper mode
|
||||
RESTIC_COMMAND="$RESTIC_BINARY --password-file $PASSWORD --repository-file $REPOSITORY"
|
||||
$RESTIC_COMMAND $@
|
||||
else
|
||||
lib_print "!The given path must be absolute and readable [$1]"
|
||||
|
|
Loading…
Reference in New Issue