LibrePortal/scripts/backup/engine/kopia_forget.sh
librelad 8b5e02c760 refactor(storage): resolve every app directory through appDir
The main sweep — ~260 call sites across ~100 files move from string
concatenation on a single root to appDir/storageAppDirs/storageAppConfigs.
On a single-root install the resolved paths are identical, so this is a
no-op until a location is registered.

Enumerators were the interesting half. `for d in "$containers_dir"/*/`
appears in the menus, the registry/artifact scanners and the DNS setup —
and a shell glob cannot list a rootless 751 tree at all, which is the
same bug config_find_file.sh already documents in a comment. Routing them
through storageAppDirs (which enumerates as the owning user) fixes that
alongside the multi-root work.

Three places needed judgement rather than substitution:

db_app_scan.sh deletes database rows and port allocations for apps whose
folder is missing, and reaps "empty" app dirs. With a storage location
unmounted, every app on it looks exactly like that. Each of those
branches now gates on appStorageAvailable first — an app on an unplugged
drive is skipped with a notice, never deleted.

instance_create.sh rewrites cloned hooks so an instance touches its own
directory instead of the base app's. Its sed matched ${containers_dir}<type>,
which this sweep just replaced with $(appDir <type>) — so it would have
silently stopped redirecting, and an instance would have written to the
original's files (the adguard auth adapter case its own comment warns
about). Now matches both appDir forms, verified against bare, quoted,
unrelated-app, legacy and prose cases.

peer_shell/peer_pull streamed and extracted relative to the primary root.
Both now use the app's own root, and peer_shell keeps a single-root
fallback since it runs as a restricted SSH shell with no LibrePortal env.

Also fixes a pre-existing bug found on the way: webui_app_config.sh
tested "$containers_dir/frontend/data/last_update", one level short of the
real tree under the libreportal app dir, so the WebUI refresh trigger
after a config update has never once fired.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-24 04:09:51 +01:00

80 lines
2.8 KiB
Bash

#!/bin/bash
# Kopia retention is expressed as a per-source policy plus a maintenance pass.
# We set the policy on the path we backed up, then run maintenance which
# forgets and reclaims space in one go.
kopiaForgetApp()
{
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)
kopiaEnvExport "$idx" || return 1
local src="$(appDir "$app_name")"
local host_tag="${CFG_INSTALL_NAME:-libreportal}"
local policy_args=(policy set --global=false "$src")
[[ -n "$keep_last" ]] && policy_args+=(--keep-latest "$keep_last")
[[ -n "$keep_daily" ]] && policy_args+=(--keep-daily "$keep_daily")
[[ -n "$keep_weekly" ]] && policy_args+=(--keep-weekly "$keep_weekly")
[[ -n "$keep_monthly" ]] && policy_args+=(--keep-monthly "$keep_monthly")
[[ -n "$keep_yearly" ]] && policy_args+=(--keep-annual "$keep_yearly")
runBackupOp kopia "${policy_args[@]}" >/dev/null 2>&1
isNotice "Running Kopia maintenance for $app_name on $(resticLocationName "$idx")"
runBackupOp kopia maintenance run --full
local rc=$?
kopiaEnvUnset
return $rc
}
kopiaForgetSystem()
{
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)
kopiaEnvExport "$idx" || return 1
local src="${configs_dir%/}"
local policy_args=(policy set --global=false "$src")
[[ -n "$keep_last" ]] && policy_args+=(--keep-latest "$keep_last")
[[ -n "$keep_daily" ]] && policy_args+=(--keep-daily "$keep_daily")
[[ -n "$keep_weekly" ]] && policy_args+=(--keep-weekly "$keep_weekly")
[[ -n "$keep_monthly" ]] && policy_args+=(--keep-monthly "$keep_monthly")
[[ -n "$keep_yearly" ]] && policy_args+=(--keep-annual "$keep_yearly")
runBackupOp kopia "${policy_args[@]}" >/dev/null 2>&1
isNotice "Running Kopia maintenance for system config on $(resticLocationName "$idx")"
runBackupOp kopia maintenance run --full
local rc=$?
kopiaEnvUnset
return $rc
}