backup-script/backup_script.sh

92 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
### CONFIG
HC_HOST=https://health.jfreudenberger.de
###
PING_ROUTE=$HC_HOST/ping/$HC_UUID
HAS_HOOKS=""
checkInput() {
for variable in HOOKS BACKUP_PATH HC_UUID RESTIC_REPOSITORY RESTIC_PASSWORD; do
if [[ -z ${!variable} ]]; then
echo "Variable $variable not given"
exit 1
fi
done
}
checkForHooks() {
test -f "$HOOKS.sh" && HAS_HOOKS=1
}
loadServiceHooks() {
if [ "$HAS_HOOKS" == "1" ]; then
echo "Loading hooks $HOOKS"
source "$HOOKS.sh"
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
healthStart
checkForHooks
if [ "$HAS_HOOKS" == "1" ]; then
loadServiceHooks
pre
fi
restic backup $BACKUP_PATH
checkResticError "$?"
if [ "$HAS_HOOKS" == "1" ]; then
post
fi
healthFinish
}
backup