btrbk-cron/btrbk-cron
2024-09-18 18:39:31 +02:00

54 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
CONF=/etc/btrbk/btrbk-cron.conf
LOGFILE=$(mktemp)
trap 'rm -f -- "${LOGFILE}"' EXIT
if [[ ! -f "${CONF}" ]] ; then
echo "Warning: Can't read config file (${CONF}), healthchecks disabled."
else
# shellcheck disable=SC1090,SC1091
source "${CONF}"
fi
HEALTHCHECK_URL=${HEALTHCHECK_URL:-http://127.0.0.1}
HEALTHCHECK_ENABLE=${HEALTHCHECK_ENABLE:-false}
CURLOPTS=(-fsS -m 10 --retry 5)
if [[ ! -d /run/btrbk ]] ; then
mkdir -p /run/btrbk
fi
echo "---" | tee -a "${LOGFILE}"
echo "Start backup: $(date||true)" | tee -a "${LOGFILE}"
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
curl "${CURLOPTS[@]}" "${HEALTHCHECK_URL}/start"
fi
echo
echo "healthcheck notified! (start)" | tee -a "${LOGFILE}"
(nice -n 19 /usr/bin/ionice -c idle /usr/bin/btrbk -v --progress run >> "${LOGFILE}" ; ) 2>&1
ret=$?
if [[ "${ret}" -eq "0" ]]; then
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
curl "${CURLOPTS[@]}" "${HEALTHCHECK_URL}"
fi
echo
echo "healthcheck notified! (ok)" | tee -a "${LOGFILE}"
else
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
curl "${CURLOPTS[@]}" "${HEALTHCHECK_URL}/fail"
fi
echo
echo "healthcheck notified! (fail)" | tee -a "${LOGFILE}"
fi
echo "starting cleanup" | tee -a "${LOGFILE}"
(nice -n 19 /usr/bin/ionice -c idle /usr/bin/btrbk clean >> "${LOGFILE}" ; ) 2>&1
echo "backup finished, sending logs" | tee -a "${LOGFILE}"
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
curl "${CURLOPTS[@]}" --data-binary "@${LOGFILE}" "${HEALTHCHECK_URL}/log"
fi
echo
rm -f -- "${LOGFILE}"
echo "backup finished"