backup-script/backup_script.sh

98 lines
1.7 KiB
Bash
Raw Normal View History

2022-02-20 19:00:21 +01:00
#!/usr/bin/env bash
### CONFIG
HC_HOST=https://health.jfreudenberger.de
###
PING_ROUTE=$HC_HOST/ping/$HC_UUID
HAS_HOOKS=""
checkInput() {
2022-02-20 19:07:06 +01:00
for variable in HOOKS BACKUP_PATH HC_UUID RESTIC_REPOSITORY RESTIC_PASSWORD; do
2022-02-20 19:00:21 +01:00
if [[ -z ${!variable} ]]; then
echo "Variable $variable not given"
exit 1
fi
done
}
goIntoScriptDirectory() {
cd $(dirname "${BASH_SOURCE[0]}")
}
2022-02-20 19:00:21 +01:00
checkForHooks() {
2022-02-20 19:07:06 +01:00
test -f "$HOOKS.sh" && HAS_HOOKS=1
2022-02-20 19:00:21 +01:00
}
loadServiceHooks() {
if [ "$HAS_HOOKS" == "1" ]; then
2022-02-20 19:07:06 +01:00
echo "Loading hooks $HOOKS"
source "$HOOKS.sh"
2022-02-20 19:00:21 +01:00
fi
}
# Send a curl to start the backup
healthStart() {
curl -fsS -m 10 --retry 5 -o /dev/null "$PING_ROUTE"/start
}
# Send a curl to finish the backup
healthFinish() {
curl -fsS -m 10 --retry 5 -o /dev/null "$PING_ROUTE"
}
# Check for General Error
checkNoError() {
if [ "$1" -ne 0 ]; then
curl -fsS -m 10 --retry 5 --data-raw "$2 error" "$PING_ROUTE"/fail
printf "\n%s ERROR !!!\n" "$3"
else
printf "\n%s SUCESSFULL\n" "$3"
fi
}
# Check for Error in Restic backup
checkResticError() {
if [ "$1" -eq 1 ]; then
exitPrematurely "restic fatal error"
elif [ "$1" -eq 2 ]; then
exitPrematurely "restic remaining files"
else
printf "\nRESTIC SUCCESSFULL\n"
fi
}
exitPrematurely() {
if [ "$HAS_HOOKS" == "1" ]; then
post
fi
curl -fsS -m 10 --retry 5 --data-raw "$1" "$PING_ROUTE"/fail
printf "\n$1 ERROR !!!\n"
exit -1
}
# Backup the service and call healthchecks
backup() {
checkInput
goIntoScriptDirectory
2022-02-20 19:00:21 +01:00
healthStart
checkForHooks
if [ "$HAS_HOOKS" == "1" ]; then
loadServiceHooks
pre
fi
restic backup $BACKUP_PATH
checkResticError "$?"
if [ "$HAS_HOOKS" == "1" ]; then
post
fi
healthFinish
}
backup