diff --git a/stacks/backup/README.md b/stacks/backup/README.md index 2a81a51..095b3bc 100644 --- a/stacks/backup/README.md +++ b/stacks/backup/README.md @@ -32,8 +32,19 @@ Kopia → Backblaze B2. Backs up `/config`, **selected** `labdata` paths (see `S | Time | What | |------|------| -| 02:50 | `backup-dump-databases.sh` — SQL dumps | -| 03:00 | Kopia snapshots (paths in `snapshot-paths.txt`) | +| 02:50 | `backup-dump-databases.sh` — SQL dumps (`backup_scheduler` cron) | +| 03:00 | Kopia snapshots — kopia server's own policy schedule (paths in `snapshot-paths.txt`) | +| hh:15 | `backup-check-freshness.sh` — pushes to Uptime Kuma monitor **Backups fresh** | + +## Alerting + +`backup-check-freshness.sh` runs hourly and pushes UP/DOWN to the Uptime Kuma push monitor +"Backups fresh (kopia + SQL dumps)" (`KUMA_PUSH_URL` in `stack.env`). DOWN when any Kopia source +or the newest SQL dump is older than 26h, or a dump file is a near-empty stub. Kuma also goes +DOWN if no push arrives for 2h (scheduler dead, kopia crash-looping), and notifies via ntfy. + +`backup-run-snapshots.sh` is for manual runs only — cron'ing it would duplicate the server's +scheduled uploads. ## If B2 creds are empty diff --git a/stacks/backup/crontab b/stacks/backup/crontab index b39aee2..d692295 100644 --- a/stacks/backup/crontab +++ b/stacks/backup/crontab @@ -1,3 +1,5 @@ # min hour day month weekday command +# Kopia snapshots run on the kopia server's own policy schedule (03:00, see init-kopia.sh); +# backup-run-snapshots.sh is kept for manual runs only — cron'ing it duplicates those uploads. 50 2 * * * /bin/sh /scripts/backup-dump-databases.sh -0 3 * * * /bin/sh /scripts/backup-run-snapshots.sh +15 * * * * /bin/sh /scripts/backup-check-freshness.sh diff --git a/stacks/backup/docker-compose.yml b/stacks/backup/docker-compose.yml index 7172930..588c264 100644 --- a/stacks/backup/docker-compose.yml +++ b/stacks/backup/docker-compose.yml @@ -48,7 +48,7 @@ services: container_name: backup_scheduler image: alpine:3.20 restart: unless-stopped - networks: [backup] + networks: [backup, edge] # edge: push heartbeats to uptime_kuma:3001 env_file: - stack.env volumes: @@ -56,14 +56,17 @@ services: - /config/backup/dumps:/dumps - /config/backup/scripts/backup-dump-databases.sh:/scripts/backup-dump-databases.sh:ro - /config/backup/scripts/backup-run-snapshots.sh:/scripts/backup-run-snapshots.sh:ro + - /config/backup/scripts/backup-check-freshness.sh:/scripts/backup-check-freshness.sh:ro - /config/backup/snapshot-paths.txt:/snapshot-paths.txt:ro - - /config/backup/crontab:/etc/crontab:ro + # busybox crond reads /; mounting at /etc/crontab meant no job ever ran + - /config/backup/crontab:/etc/crontabs/root:ro entrypoint: ["/bin/sh", "-c"] command: - | - apk add --no-cache docker-cli curl tzdata >/dev/null - printenv | grep -E '^(NTFY_|TZ=)' > /etc/backup.env || true - crond -f -l 8 -c /etc + apk add --no-cache docker-cli curl jq tzdata >/dev/null + # crond jobs get a bare env; scripts source this instead + printenv | grep -E '^(NTFY_|KUMA_|KOPIA_REPOSITORY_PASSWORD=|TZ=)' | sed "s/=\(.*\)/='\1'/" > /etc/backup.env || true + crond -f -l 8 -c /etc/crontabs depends_on: - kopia diff --git a/stacks/backup/scripts/backup-check-freshness.sh b/stacks/backup/scripts/backup-check-freshness.sh new file mode 100755 index 0000000..a3d9dbb --- /dev/null +++ b/stacks/backup/scripts/backup-check-freshness.sh @@ -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 diff --git a/stacks/backup/scripts/backup-dump-databases.sh b/stacks/backup/scripts/backup-dump-databases.sh index 74d2e87..4760bad 100644 --- a/stacks/backup/scripts/backup-dump-databases.sh +++ b/stacks/backup/scripts/backup-dump-databases.sh @@ -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 diff --git a/stacks/backup/stack.env b/stacks/backup/stack.env index 2dbdc4f..c9fa1d2 100644 --- a/stacks/backup/stack.env +++ b/stacks/backup/stack.env @@ -13,3 +13,6 @@ B2_ENDPOINT=s3.us-west-004.backblazeb2.com # ntfy alerts (same pattern as watchtower) NTFY_URL=https://ntfy.ginnoir.com/backup NTFY_TITLE=ValhallaBackup + +# Uptime Kuma push monitor "Backups fresh" (id 73) — backup-check-freshness.sh +KUMA_PUSH_URL=http://uptime_kuma:3001/api/push/GTUcqAMJs5k5AfPq940NkEdM