Fix healthcheck
All checks were successful
ShellCheck / shellcheck (push) Successful in 9s
Docker / build (push) Successful in 24s

This commit is contained in:
Andras Schmelczer 2026-09-21 22:03:44 +01:00
commit aa46e5ed2f
4 changed files with 80 additions and 52 deletions

View file

@ -14,63 +14,70 @@ execute_script() {
}
configure_environment() {
local index=$1
local all_vars_set=true
# required variables
for var in BORG_PASSPHRASE BORG_REPO; do
local indexed_var_name="${var}_${index}"
if [[ -n "${!indexed_var_name}" ]]; then
export "$var"="${!indexed_var_name}"
else
all_vars_set=false
break
fi
done
local repo_var="BORG_REPO_$1" passphrase_var="BORG_PASSPHRASE_$1" remote_var="BORG_REMOTE_PATH_$1"
[[ -n ${!repo_var} && -n ${!passphrase_var} ]] || return 1
export BORG_REPO="${!repo_var}" BORG_PASSPHRASE="${!passphrase_var}"
# optional variables
local indexed_var_name="BORG_REMOTE_PATH_${index}"
if [[ -n "${!indexed_var_name}" ]]; then
export BORG_REMOTE_PATH="${!indexed_var_name}"
if [[ -n ${!remote_var} ]]; then
export BORG_REMOTE_PATH="${!remote_var}"
else
unset BORG_REMOTE_PATH
fi
[[ $all_vars_set == true ]]
}
main() {
if [ -n "$BORG_REPO" ]; then
# fallback case if multi-target backup isn't needed
if execute_script; then
date > /health/backup_completion_time.log
else
if ! execute_script; then
echo "Skipping completion log due to backup failure"
return 1
fi
else
local index=0
local configurations_found=false
local any_failed=false
local completed_indices=" "
while configure_environment $index; do
while configure_environment "$index"; do
execute_script || any_failed=true
configurations_found=true
completed_indices+="$index "
unset BORG_PASSPHRASE BORG_REMOTE_PATH BORG_REPO
((index++))
done
echo "Finished backup script at $(date)"
if [[ $any_failed == false ]]; then
date > /health/backup_completion_time.log # this used by the healtcheck
else
echo "Skipping completion log due to backup failure(s)"
if (( index == 0 )); then
echo "No valid configuration found. Please ensure environment variables are set properly."
return 1
fi
if [[ $configurations_found == false ]]; then
echo "No valid configuration found. Please ensure environment variables are set properly."
# A missing or incomplete target must not silently hide later targets.
local indexed_var_name
for indexed_var_name in ${!BORG_REPO_@} ${!BORG_PASSPHRASE_@} ${!BORG_REMOTE_PATH_@}; do
[[ -n ${!indexed_var_name} ]] || continue
case "$completed_indices" in
*" ${indexed_var_name##*_} "*) ;;
*)
echo "Invalid or non-contiguous backup configuration: $indexed_var_name. Skipping completion log."
return 1
;;
esac
done
if [[ $any_failed == true ]]; then
echo "Skipping completion log due to backup failure(s)"
return 1
fi
fi
# Rename only a complete timestamp, leaving the previous success intact on failure.
mkdir -p /health || return 1
trap 'rm -f /health/backup_completion_time.log.$$' EXIT
date -u '+%Y-%m-%dT%H:%M:%SZ' > /health/backup_completion_time.log.$$ &&
mv -fT /health/backup_completion_time.log.$$ /health/backup_completion_time.log
}
main
backup_status=$?
echo "Finished backup wrapper script at $(date)"
exit "$backup_status"