fix(backup): make backup_scheduler actually run; add freshness alerting
busybox crond reads <dir>/<user>, but the crontab was mounted at /etc/crontab, so no cron job had ever run: SQL dumps were last taken 2026-06-17 and the ntfy failure alerts never fired. Kopia's own server schedule kept file snapshots going until its cache broke on 2026-08-28, and nothing noticed for seven weeks. - mount crontab at /etc/crontabs/root and run crond -c /etc/crontabs - add hourly backup-check-freshness.sh: every Kopia source and the newest SQL dump must be <26h old and dumps must not be empty stubs; pushes UP/DOWN to the Uptime Kuma push monitor "Backups fresh" (#73), which also alerts via ntfy if no push arrives for 2h - scheduler joins edge to reach uptime_kuma; jq added; KOPIA password and KUMA_PUSH_URL exported to /etc/backup.env for cron's bare env - drop backup-run-snapshots.sh from cron (would duplicate the kopia server's 03:00 policy snapshots); keep it for manual runs - bookstack dump used an unset MYSQL_PASSWORD (empty 20-byte gzip); dump_mariadb now takes the password env var name - add immich-postgres and nextcloud-postgres dumps Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
72688f48ac
commit
e0240d8e10
6 files changed
+96
-10
No files matched your search
+60
@@ -0,0 +1,60 @@
|
||||
#!/bin/sh
|
||||
# Hourly dead-man check: every Kopia source in snapshot-paths.txt and the newest
|
||||
# SQL dump must be younger than MAX_AGE_HOURS. Pushes UP/DOWN to the Uptime Kuma
|
||||
# push monitor "Backups fresh"; Kuma also alerts (via ntfy) if no push arrives
|
||||
# for 2h, so a dead scheduler or crash-looping kopia is caught too.
|
||||
# Runs inside backup_scheduler; needs docker.sock + jq.
|
||||
set -u
|
||||
|
||||
[ -f /etc/backup.env ] && . /etc/backup.env
|
||||
|
||||
MAX_AGE_HOURS="${MAX_AGE_HOURS:-26}"
|
||||
KOPIA_PASSWORD="${KOPIA_REPOSITORY_PASSWORD:-}"
|
||||
PROBLEMS=""
|
||||
|
||||
problem() { PROBLEMS="${PROBLEMS:+${PROBLEMS}; }$*"; }
|
||||
|
||||
push() {
|
||||
status="$1"
|
||||
msg=$(printf '%s' "$2" | jq -sRr @uri)
|
||||
curl -sf -m 20 "${KUMA_PUSH_URL}?status=${status}&msg=${msg}" >/dev/null 2>&1 || echo "kuma push failed"
|
||||
}
|
||||
|
||||
# --- Kopia snapshots ---------------------------------------------------------
|
||||
if ! SNAPS=$(docker exec -e KOPIA_PASSWORD="${KOPIA_PASSWORD}" -e KOPIA_CONFIG_PATH=/app/config/repository.config \
|
||||
kopia kopia snapshot list --all --json 2>/dev/null); then
|
||||
problem "kopia: cannot list snapshots (container down or repo unreadable)"
|
||||
else
|
||||
while IFS= read -r path || [ -n "${path}" ]; do
|
||||
case "${path}" in ''|\#*) continue ;; esac
|
||||
age=$(printf '%s' "${SNAPS}" | jq -r --arg p "${path}" '
|
||||
[.[] | select(.source.path == $p) | .startTime | sub("\\.[0-9]+"; "") | fromdateiso8601]
|
||||
| if length == 0 then "none" else ((now - max) / 3600 | floor) end')
|
||||
if [ "${age}" = "none" ]; then
|
||||
problem "kopia ${path}: no snapshots"
|
||||
elif [ "${age}" -ge "${MAX_AGE_HOURS}" ]; then
|
||||
problem "kopia ${path}: ${age}h old"
|
||||
fi
|
||||
done < /snapshot-paths.txt
|
||||
fi
|
||||
|
||||
# --- SQL dumps ---------------------------------------------------------------
|
||||
NEWEST=$(find /dumps -mindepth 1 -maxdepth 1 -type d -name '2*' | sort | tail -1)
|
||||
if [ -z "${NEWEST}" ]; then
|
||||
problem "dumps: none found"
|
||||
else
|
||||
age=$(( ($(date +%s) - $(stat -c %Y "${NEWEST}")) / 3600 ))
|
||||
[ "${age}" -ge "${MAX_AGE_HOURS}" ] && problem "dumps: newest is ${age}h old"
|
||||
# pg_dump/mariadb-dump failures still leave a gzip stub (no pipefail in sh)
|
||||
tiny=$(find "${NEWEST}" -name '*.sql.gz' -size -1k -exec basename {} \; | tr '\n' ' ')
|
||||
[ -n "${tiny}" ] && problem "dumps: suspiciously small: ${tiny}"
|
||||
fi
|
||||
|
||||
if [ -z "${PROBLEMS}" ]; then
|
||||
echo "backup-check: OK"
|
||||
push up "OK"
|
||||
else
|
||||
echo "backup-check: ${PROBLEMS}"
|
||||
push down "${PROBLEMS}"
|
||||
exit 1
|
||||
fi
|
||||
@@ -23,16 +23,21 @@ dump_postgres() {
|
||||
}
|
||||
|
||||
# user: root (default) or app account (e.g. bookstack — root password may not match env)
|
||||
# pwvar: optional name of the container env var holding that user's password
|
||||
dump_mariadb() {
|
||||
name="$1"
|
||||
db="$2"
|
||||
user="${3:-root}"
|
||||
pwvar="${4:-}"
|
||||
if ! docker ps --format '{{.Names}}' | grep -qx "${name}"; then
|
||||
log "skip ${name} (not running)"
|
||||
return 0
|
||||
fi
|
||||
log "dump ${name} → ${db} (as ${user})"
|
||||
if [ "${user}" = "root" ]; then
|
||||
if [ -n "${pwvar}" ]; then
|
||||
docker exec "${name}" sh -c "mariadb-dump -u \"${user}\" -p\"\${${pwvar}}\" \"${db}\"" \
|
||||
| gzip -c > "${DIR}/${name}.sql.gz"
|
||||
elif [ "${user}" = "root" ]; then
|
||||
docker exec "${name}" sh -c "mariadb-dump -u root -p\"\${MARIADB_ROOT_PASSWORD:-\$MYSQL_ROOT_PASSWORD}\" \"${db}\"" \
|
||||
| gzip -c > "${DIR}/${name}.sql.gz"
|
||||
else
|
||||
@@ -48,8 +53,10 @@ dump_postgres postgres_plane plane plane
|
||||
dump_postgres postgres_authentik authentik authentik
|
||||
dump_postgres postgres_famapp famapp famapp
|
||||
dump_postgres postgres_resume postgres postgres
|
||||
dump_postgres immich-postgres immich immich
|
||||
dump_postgres nextcloud-postgres nextcloud nextcloud
|
||||
|
||||
dump_mariadb mariadb_bookstack bookstack bookstack
|
||||
dump_mariadb mariadb_bookstack bookstack bookstack BOOKSTACK_DB_PASSWORD
|
||||
dump_mariadb owncloud_mariadb owncloud
|
||||
dump_mariadb romm-db romm
|
||||
|
||||
|
||||
Reference in new issue
Block a user