LibrePortal/scripts/backup/engine/restic_forget.sh
librelad 038d1c0729 fix(backup): system config in scheduled backups + retention (review findings)
Final-review gaps in the system-config backup:

1. Scheduled (cron) backups skipped it — backupScheduleEnabledApps only queued
   per-app backups, so the daily schedule never refreshed the system config (and
   thus the backup-location creds could go stale). Now it queues a
   `libreportal backup system` task (or runs inline on terminal-only installs),
   and skips the reproducible libreportal app for consistency with backupAllApps.

2. No retention on system snapshots — they bypass backupAppStart's per-app forget,
   so they accumulated unbounded. Add resticForgetSystem (tag system=config,
   respects append-only + the same keep-* policy) + engineForgetSystem dispatcher;
   backupSystemConfig now applies retention across all locations after snapshotting.

Verified with stubs: backupSystemConfig snapshots AND prunes on every location;
engineForgetSystem pairs with resticForgetSystem; scheduled createTaskFile call
matches the existing 3-arg signature.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-26 00:48:18 +01:00

97 lines
3.1 KiB
Bash

#!/bin/bash
resticForgetApp()
{
local idx="$1"
local app_name="$2"
if resticLocationAppendOnly "$idx"; then
isNotice "$(resticLocationName "$idx") is append-only — skipping forget for $app_name"
return 0
fi
local keep_last keep_daily keep_weekly keep_monthly keep_yearly
keep_last=$(resticRetentionFor "$idx" KEEP_LAST)
keep_daily=$(resticRetentionFor "$idx" KEEP_DAILY)
keep_weekly=$(resticRetentionFor "$idx" KEEP_WEEKLY)
keep_monthly=$(resticRetentionFor "$idx" KEEP_MONTHLY)
keep_yearly=$(resticRetentionFor "$idx" KEEP_YEARLY)
resticEnvExport "$idx" || return 1
local args=(forget --tag "app=$app_name" --group-by tags,host)
[[ -n "$keep_last" ]] && args+=(--keep-last "$keep_last")
[[ -n "$keep_daily" ]] && args+=(--keep-daily "$keep_daily")
[[ -n "$keep_weekly" ]] && args+=(--keep-weekly "$keep_weekly")
[[ -n "$keep_monthly" ]] && args+=(--keep-monthly "$keep_monthly")
[[ -n "$keep_yearly" ]] && args+=(--keep-yearly "$keep_yearly")
[[ "$CFG_BACKUP_PRUNE_AFTER_FORGET" == "true" ]] && args+=(--prune)
isNotice "Applying retention for $app_name on $(resticLocationName "$idx")"
runBackupOp restic "${args[@]}"
local rc=$?
resticEnvUnset
return $rc
}
resticForgetSystem()
{
local idx="$1"
if resticLocationAppendOnly "$idx"; then
isNotice "$(resticLocationName "$idx") is append-only — skipping forget for system config"
return 0
fi
local keep_last keep_daily keep_weekly keep_monthly keep_yearly
keep_last=$(resticRetentionFor "$idx" KEEP_LAST)
keep_daily=$(resticRetentionFor "$idx" KEEP_DAILY)
keep_weekly=$(resticRetentionFor "$idx" KEEP_WEEKLY)
keep_monthly=$(resticRetentionFor "$idx" KEEP_MONTHLY)
keep_yearly=$(resticRetentionFor "$idx" KEEP_YEARLY)
resticEnvExport "$idx" || return 1
local args=(forget --tag "system=config" --group-by tags,host)
[[ -n "$keep_last" ]] && args+=(--keep-last "$keep_last")
[[ -n "$keep_daily" ]] && args+=(--keep-daily "$keep_daily")
[[ -n "$keep_weekly" ]] && args+=(--keep-weekly "$keep_weekly")
[[ -n "$keep_monthly" ]] && args+=(--keep-monthly "$keep_monthly")
[[ -n "$keep_yearly" ]] && args+=(--keep-yearly "$keep_yearly")
[[ "$CFG_BACKUP_PRUNE_AFTER_FORGET" == "true" ]] && args+=(--prune)
isNotice "Applying retention for system config on $(resticLocationName "$idx")"
runBackupOp restic "${args[@]}"
local rc=$?
resticEnvUnset
return $rc
}
resticRetentionFor()
{
local idx="$1"
local field="$2"
if [[ "$(resticLocationField "$idx" CUSTOM_RETENTION)" == "true" ]]; then
local override
override=$(resticLocationField "$idx" "$field")
if [[ -n "$override" ]]; then
echo "$override"
return
fi
fi
local global_var="CFG_BACKUP_${field}"
echo "${!global_var}"
}
resticForgetAppAllLocations()
{
local app_name="$1"
local idx
while IFS= read -r idx; do
[[ -z "$idx" ]] && continue
resticForgetApp "$idx" "$app_name"
done < <(resticEnabledLocations)
}