Add configurable exclusions and backup run history
This commit is contained in:
1 parent
64cfbeb90c
commit
2351cb6384
6 files changed
+58
-6
No files matched your search
@@ -13,6 +13,11 @@ WAIT_INTERVAL=3
|
|||||||
START_RETRIES=1
|
START_RETRIES=1
|
||||||
LOCK_FILE="/run/lock/zorruno-docker-backup.lock"
|
LOCK_FILE="/run/lock/zorruno-docker-backup.lock"
|
||||||
ZSTD_EXTRA_OPTS="-T0 -12"
|
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.
|
# Optional retained MQTT status and command listener settings.
|
||||||
MQTT_REPORT_ENABLED=0
|
MQTT_REPORT_ENABLED=0
|
||||||
|
|||||||
@@ -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_HOSTNAME` | Host folder/name embedded in archive filenames |
|
||||||
| `BACKUP_METHODS` | Ordered method names, default `rclone smb` |
|
| `BACKUP_METHODS` | Ordered method names, default `rclone smb` |
|
||||||
| `ZSTD_EXTRA_OPTS` | Options passed to `zstd`, default `-T0 -12` |
|
| `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 |
|
| `KEEP_COUNT` | Local archives retained per Compose project |
|
||||||
| `PULL_IMAGES` | Pull images before restarting (`1` or `0`) |
|
| `PULL_IMAGES` | Pull images before restarting (`1` or `0`) |
|
||||||
|
|
||||||
|
|||||||
@@ -104,11 +104,11 @@ finish_run() {
|
|||||||
else
|
else
|
||||||
if (( DRY_RUN )); then status="dry_run_failed"; else status="failed"; fi
|
if (( DRY_RUN )); then status="dry_run_failed"; else status="failed"; fi
|
||||||
fi
|
fi
|
||||||
if is_true "$MQTT_REPORT_ENABLED"; then
|
# 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" \
|
"$SCRIPT_DIR/stages/99-mqtt-report.sh" "$status" "$exit_code" \
|
||||||
"$BACKUP_STARTED_EPOCH" "$finished" "$BACKUP_RUN_ID" "$BACKUP_RUN_LOG" \
|
"$BACKUP_STARTED_EPOCH" "$finished" "$BACKUP_RUN_ID" "$BACKUP_RUN_LOG" \
|
||||||
|| log ERROR "Unable to publish final MQTT result"
|
|| log ERROR "Unable to record final backup report"
|
||||||
fi
|
|
||||||
rm -f -- "$BACKUP_RUN_LOG"
|
rm -f -- "$BACKUP_RUN_LOG"
|
||||||
exit "$exit_code"
|
exit "$exit_code"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ WAIT_INTERVAL=3
|
|||||||
START_RETRIES=1
|
START_RETRIES=1
|
||||||
LOCK_FILE="/run/lock/zorruno-docker-backup.lock"
|
LOCK_FILE="/run/lock/zorruno-docker-backup.lock"
|
||||||
ZSTD_EXTRA_OPTS="-T0 -12"
|
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.
|
# Optional MQTT reporting and command-listener defaults.
|
||||||
MQTT_REPORT_ENABLED=0
|
MQTT_REPORT_ENABLED=0
|
||||||
MQTT_HOST=""
|
MQTT_HOST=""
|
||||||
@@ -68,6 +71,18 @@ return 0
|
|||||||
}
|
}
|
||||||
die() { log ERROR "$1"; exit "${2:-1}"; }
|
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() {
|
is_true() {
|
||||||
case "${1,,}" in
|
case "${1,,}" in
|
||||||
1|true|yes|on|enabled) return 0 ;;
|
1|true|yes|on|enabled) return 0 ;;
|
||||||
|
|||||||
+10
-1
@@ -157,7 +157,16 @@ backup_project() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
read -r -a zstd_args <<<"$ZSTD_EXTRA_OPTS"
|
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
|
# Never leave a partial archive that a transfer stage could mistake for a
|
||||||
# valid backup. Make a best-effort restart before recording the failure.
|
# valid backup. Make a best-effort restart before recording the failure.
|
||||||
rm -f -- "$output"
|
rm -f -- "$output"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Stage 99: publish retained backup run state for Home Assistant.
|
# Stage 99: persist a bounded run history and publish retained state for HA.
|
||||||
# Called once at pipeline start and again from backup.sh's EXIT trap.
|
# The running report is MQTT-only; final reports are also kept locally.
|
||||||
set -Eeuo pipefail
|
set -Eeuo pipefail
|
||||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
|
||||||
. "$SCRIPT_DIR/../lib/common.sh"
|
. "$SCRIPT_DIR/../lib/common.sh"
|
||||||
@@ -52,6 +52,26 @@ payload="$(jq -cn \
|
|||||||
duration_seconds:$duration,containers_total:$total,containers_backed_up:$archived,
|
duration_seconds:$duration,containers_total:$total,containers_backed_up:$archived,
|
||||||
containers_dry_run:$dry_run,error_count:$error_count,errors:$errors}')"
|
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.
|
# Retention gives HA the latest known state after restarts or broker reconnects.
|
||||||
|
if is_true "$MQTT_REPORT_ENABLED"; then
|
||||||
mqtt_publish "$MQTT_STATUS_TOPIC" "$payload" 1
|
mqtt_publish "$MQTT_STATUS_TOPIC" "$payload" 1
|
||||||
log MQTT "Published $STATUS result to $MQTT_STATUS_TOPIC"
|
log MQTT "Published $STATUS result to $MQTT_STATUS_TOPIC"
|
||||||
|
fi
|
||||||
Reference in new issue
Block a user