diff --git a/.env.example b/.env.example index 1ba6d42..23024a0 100644 --- a/.env.example +++ b/.env.example @@ -13,6 +13,11 @@ WAIT_INTERVAL=3 START_RETRIES=1 LOCK_FILE="/run/lock/zorruno-docker-backup.lock" ZSTD_EXTRA_OPTS="-T0 -12" +# Space-separated tar member paths relative to SOURCE_BASE_DIR. +# Example: TAR_EXCLUDES="nextcloud/data otherproject/cache" +TAR_EXCLUDES="" +# Final backup-run reports retained locally for this many days. +BACKUP_HISTORY_DAYS=30 # Optional retained MQTT status and command listener settings. MQTT_REPORT_ENABLED=0 diff --git a/README.md b/README.md index fd22602..4d25047 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,9 @@ Copy `.env.example` to `.env`; `.env` is ignored by Git. Important values: | `BACKUP_HOSTNAME` | Host folder/name embedded in archive filenames | | `BACKUP_METHODS` | Ordered method names, default `rclone smb` | | `ZSTD_EXTRA_OPTS` | Options passed to `zstd`, default `-T0 -12` | +| `TAR_EXCLUDES` | Space-separated archive paths to omit, relative to `SOURCE_BASE_DIR` | +| `BACKUP_HISTORY_FILE` | JSON Lines run-history file (default is shared with backup-status MCP) | +| `BACKUP_HISTORY_DAYS` | Days of completed run history to retain, default `30` | | `KEEP_COUNT` | Local archives retained per Compose project | | `PULL_IMAGES` | Pull images before restarting (`1` or `0`) | diff --git a/backup.sh b/backup.sh index cadfdff..f44e3b5 100755 --- a/backup.sh +++ b/backup.sh @@ -104,11 +104,11 @@ finish_run() { else if (( DRY_RUN )); then status="dry_run_failed"; else status="failed"; fi fi - if is_true "$MQTT_REPORT_ENABLED"; then - "$SCRIPT_DIR/stages/99-mqtt-report.sh" "$status" "$exit_code" \ - "$BACKUP_STARTED_EPOCH" "$finished" "$BACKUP_RUN_ID" "$BACKUP_RUN_LOG" \ - || log ERROR "Unable to publish final MQTT result" - fi + # Persist history even when MQTT reporting is disabled. The stage handles + # MQTT publication conditionally and keeps its own failures non-fatal here. + "$SCRIPT_DIR/stages/99-mqtt-report.sh" "$status" "$exit_code" \ + "$BACKUP_STARTED_EPOCH" "$finished" "$BACKUP_RUN_ID" "$BACKUP_RUN_LOG" \ + || log ERROR "Unable to record final backup report" rm -f -- "$BACKUP_RUN_LOG" exit "$exit_code" } diff --git a/lib/common.sh b/lib/common.sh index 503ca89..f12d1c6 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -22,6 +22,9 @@ WAIT_INTERVAL=3 START_RETRIES=1 LOCK_FILE="/run/lock/zorruno-docker-backup.lock" ZSTD_EXTRA_OPTS="-T0 -12" +TAR_EXCLUDES="" +BACKUP_HISTORY_FILE="/dockervolumes/openclaw/backup-status-mcp/history/backup-runs.jsonl" +BACKUP_HISTORY_DAYS=30 # Optional MQTT reporting and command-listener defaults. MQTT_REPORT_ENABLED=0 MQTT_HOST="" @@ -68,6 +71,18 @@ return 0 } die() { log ERROR "$1"; exit "${2:-1}"; } +# Never let local backups silently fall back to the unencrypted root filesystem. +if [[ "$BACKUP_ROOT" == "/dockerbackups" ]]; then + if ! mountpoint -q /srv/storage || ! mountpoint -q "$BACKUP_ROOT"; then + die "Backup root is not mounted on encrypted storage; refusing to run." + fi + storage_uuid=$(findmnt -n -o UUID -T /srv/storage 2>/dev/null || true) + backup_uuid=$(findmnt -n -o UUID -T "$BACKUP_ROOT" 2>/dev/null || true) + if [[ -z "$storage_uuid" || "$backup_uuid" != "$storage_uuid" ]]; then + die "Backup root is not on encrypted storage; refusing to run." + fi +fi + is_true() { case "${1,,}" in 1|true|yes|on|enabled) return 0 ;; diff --git a/stages/10-containers.sh b/stages/10-containers.sh index 7cac8a7..4758b4b 100755 --- a/stages/10-containers.sh +++ b/stages/10-containers.sh @@ -157,7 +157,16 @@ backup_project() { return 1 fi read -r -a zstd_args <<<"$ZSTD_EXTRA_OPTS" - if ! tar --one-file-system -cf - -C "$parent" "$base" | zstd "${zstd_args[@]}" -o "$output"; then + # Exclusion patterns are archive paths relative to SOURCE_BASE_DIR, allowing + # host-local .env settings to omit large data directories from any project. + tar_args=(--one-file-system) + if [[ -n "${TAR_EXCLUDES:-}" ]]; then + read -r -a tar_excludes <<<"$TAR_EXCLUDES" + for pattern in "${tar_excludes[@]}"; do + tar_args+=(--exclude="$pattern") + done + fi + if ! tar "${tar_args[@]}" -cf - -C "$parent" "$base" | zstd "${zstd_args[@]}" -o "$output"; then # Never leave a partial archive that a transfer stage could mistake for a # valid backup. Make a best-effort restart before recording the failure. rm -f -- "$output" diff --git a/stages/99-mqtt-report.sh b/stages/99-mqtt-report.sh index 2fb8146..884b1a8 100755 --- a/stages/99-mqtt-report.sh +++ b/stages/99-mqtt-report.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Stage 99: publish retained backup run state for Home Assistant. -# Called once at pipeline start and again from backup.sh's EXIT trap. +# Stage 99: persist a bounded run history and publish retained state for HA. +# The running report is MQTT-only; final reports are also kept locally. set -Eeuo pipefail SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" . "$SCRIPT_DIR/../lib/common.sh" @@ -52,6 +52,26 @@ payload="$(jq -cn \ duration_seconds:$duration,containers_total:$total,containers_backed_up:$archived, containers_dry_run:$dry_run,error_count:$error_count,errors:$errors}')" +if [[ "$STATUS" != "running" ]]; then + history_dir="$(dirname -- "$BACKUP_HISTORY_FILE")" + mkdir -p -- "$history_dir" + history_lock="${BACKUP_HISTORY_FILE}.lock" + exec 8>"$history_lock" + flock 8 + history_tmp="$(mktemp "${BACKUP_HISTORY_FILE}.XXXXXX")" + if [[ -f "$BACKUP_HISTORY_FILE" ]]; then + cutoff="$(date -u -d "$BACKUP_HISTORY_DAYS days ago" +%FT%TZ)" + jq -c --arg cutoff "$cutoff" 'select((.finished_at // "") >= $cutoff)' \ + "$BACKUP_HISTORY_FILE" >"$history_tmp" + fi + printf '%s\n' "$payload" >>"$history_tmp" + chmod 0644 "$history_tmp" + mv -f -- "$history_tmp" "$BACKUP_HISTORY_FILE" + log HISTORY "Recorded $STATUS run in $BACKUP_HISTORY_FILE" +fi + # Retention gives HA the latest known state after restarts or broker reconnects. -mqtt_publish "$MQTT_STATUS_TOPIC" "$payload" 1 -log MQTT "Published $STATUS result to $MQTT_STATUS_TOPIC" +if is_true "$MQTT_REPORT_ENABLED"; then + mqtt_publish "$MQTT_STATUS_TOPIC" "$payload" 1 + log MQTT "Published $STATUS result to $MQTT_STATUS_TOPIC" +fi