perf(backup): throttle + dedupe the WebUI backup dashboard refresh

The "Refreshing backup data..." step on every WebUI update fired one
`restic stats` (restore-size mode — the slowest restic op) plus ~4
identical unfiltered `restic snapshots --json` pulls per enabled
location (dashboard, snapshots, app-status, migrate each pulled their
own), all over a fresh SSH connection for remote repos, on every pass
with no throttle — the slow, "frozen"-looking line users hit on poor
links.

Two fixes:

1. Dedupe. engineSnapshotsJson transparently memoises the first
   unfiltered whole-repo pull per location to a shared cache dir
   (LP_SNAP_CACHE_DIR, set by webui_updater around the chain), so the
   four generators reuse one restic call instead of four. Filtered and
   failed/empty pulls always fall through to a live call.

2. Throttle. Backups and location changes already regenerate this data
   live when they happen, so the routine pass is only a drift catch-up.
   Split the cheap local-only generators (engines/schema/passwords —
   no remote I/O) out to always run, and gate the remote pull behind
   CFG_BACKUP_DASHBOARD_REFRESH_INTERVAL (minutes, default 30, 0 =
   every update). A completed backup touches a dirty marker that forces
   the next pass to pull; WEBUI_UPDATER_FORCE still forces a full
   refresh.

Net: N locations x 5 remote restic calls every update -> 1 call per
location, only when something actually changed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
This commit is contained in:
librelad 2026-07-17 21:35:24 +01:00
parent f6a7415a52
commit a5cd8d625b
5 changed files with 76 additions and 10 deletions

View File

@ -3,3 +3,4 @@
# @icon 💾 # @icon 💾
# ================================================================================ # ================================================================================
CFG_BACKUP_CRONTAB_APP="0 5 * * *" # App Backup Schedule - Crontab schedule for application backups CFG_BACKUP_CRONTAB_APP="0 5 * * *" # App Backup Schedule - Crontab schedule for application backups
CFG_BACKUP_DASHBOARD_REFRESH_INTERVAL=30 # Dashboard Refresh Interval - Minutes between routine restic pulls that refresh the Backups dashboard (a completed backup always refreshes immediately). 0 refreshes on every WebUI update.

View File

@ -133,6 +133,11 @@ backupAppStart()
webuiGenerateBackupDashboard webuiGenerateBackupDashboard
webuiGenerateBackupSnapshots all webuiGenerateBackupSnapshots all
webuiGenerateBackupAppStatus "$stored_app_name" webuiGenerateBackupAppStatus "$stored_app_name"
# Signal the throttled routine refresh (webuiLibrePortalUpdate) that a
# new snapshot exists, so its next pass does a full remote pull to
# reconcile the bits this targeted regen skips (migrate + other apps'
# status) even while the refresh window is otherwise closed.
touch "/tmp/libreportal_webui_backup_dirty" 2>/dev/null || true
fi fi
echo "" echo ""

View File

@ -52,7 +52,26 @@ engineBackupSystem() { local i="$1"; shift; backupLocationLocalGuard "$i
engineRestoreSystemLatest() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")RestoreSystemLatest" "$i" "$@"; } engineRestoreSystemLatest() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")RestoreSystemLatest" "$i" "$@"; }
engineRestoreSnapshot() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")RestoreSnapshot" "$i" "$@"; } engineRestoreSnapshot() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")RestoreSnapshot" "$i" "$@"; }
engineSnapshotLatestId() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")SnapshotLatestId" "$i" "$@"; } engineSnapshotLatestId() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")SnapshotLatestId" "$i" "$@"; }
engineSnapshotsJson() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")SnapshotsJson" "$i" "$@"; } # Whole-repo snapshot list. The WebUI backup refresh pulls this the same way
# (no filters) from four generators per location — dashboard, snapshots,
# app-status and migrate — which on a remote (SSH) repo is four identical restic
# round-trips. When LP_SNAP_CACHE_DIR is set (webui_updater wraps the refresh
# chain with it), memoise the first unfiltered pull per location to a file the
# sibling generators reuse; filtered/parameterised calls (extra args) and any
# failed/empty pull always fall through to a live restic call.
engineSnapshotsJson() {
local i="$1"; shift
if [[ $# -eq 0 && -n "${LP_SNAP_CACHE_DIR:-}" ]]; then
local _cf="${LP_SNAP_CACHE_DIR}/snapshots_${i}.json"
[[ -s "$_cf" ]] && { cat "$_cf"; return 0; }
local _out _rc
_out=$(engineDispatch "$(engineForLocation "$i")SnapshotsJson" "$i"); _rc=$?
[[ $_rc -eq 0 && -n "$_out" ]] && printf '%s' "$_out" > "$_cf" 2>/dev/null
printf '%s' "$_out"
return $_rc
fi
engineDispatch "$(engineForLocation "$i")SnapshotsJson" "$i" "$@"
}
engineSystemSnapshotsJson() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")SystemSnapshotsJson" "$i" "$@"; } engineSystemSnapshotsJson() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")SystemSnapshotsJson" "$i" "$@"; }
engineSnapshotListFiles() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")SnapshotListFiles" "$i" "$@"; } engineSnapshotListFiles() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")SnapshotListFiles" "$i" "$@"; }
engineForgetApp() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")ForgetApp" "$i" "$@"; } engineForgetApp() { local i="$1"; shift; engineDispatch "$(engineForLocation "$i")ForgetApp" "$i" "$@"; }

View File

@ -72,7 +72,7 @@ webuiValidateConfigValue() {
isError " Invalid crontab format for $var_name" isError " Invalid crontab format for $var_name"
fi fi
;; ;;
CFG_BACKUP_KEEP_LAST|CFG_BACKUP_KEEP_DAILY|CFG_BACKUP_KEEP_WEEKLY|CFG_BACKUP_KEEP_MONTHLY|CFG_BACKUP_KEEP_YEARLY|CFG_BACKUP_VERIFY_DATA_PERCENT|CFG_UPDATER_CHECK|CFG_UPDATER_SCAN_INTERVAL|CFG_SWAPFILE_SIZE|CFG_GENERATED_PASS_LENGTH|CFG_WEBUI_LOG_STREAM_IDLE_TIMEOUT_MINUTES|CFG_WEBUI_LOG_STREAM_MAX_DURATION_MINUTES|CFG_WEBUI_LOG_STREAM_MAX_LINES_PER_SEC) CFG_BACKUP_KEEP_LAST|CFG_BACKUP_KEEP_DAILY|CFG_BACKUP_KEEP_WEEKLY|CFG_BACKUP_KEEP_MONTHLY|CFG_BACKUP_KEEP_YEARLY|CFG_BACKUP_VERIFY_DATA_PERCENT|CFG_BACKUP_DASHBOARD_REFRESH_INTERVAL|CFG_UPDATER_CHECK|CFG_UPDATER_SCAN_INTERVAL|CFG_SWAPFILE_SIZE|CFG_GENERATED_PASS_LENGTH|CFG_WEBUI_LOG_STREAM_IDLE_TIMEOUT_MINUTES|CFG_WEBUI_LOG_STREAM_MAX_DURATION_MINUTES|CFG_WEBUI_LOG_STREAM_MAX_LINES_PER_SEC)
# Validate numeric values # Validate numeric values
if ! echo "$var_value" | grep -qE '^[0-9]+$'; then if ! echo "$var_value" | grep -qE '^[0-9]+$'; then
isError " $var_name must be a positive integer" isError " $var_name must be a positive integer"

View File

@ -92,14 +92,55 @@ webuiLibrePortalUpdate() {
checkSuccess "Refreshed ${_app} WebUI data..." checkSuccess "Refreshed ${_app} WebUI data..."
done done
# Generate Backup locations / snapshots / engines / dashboards. # Backup dashboard data, split by cost:
# Announce before running: snapshot generation reaches remote backup # * local-only (engines/schema/passwords) — cheap file + config
# locations (restic over SSH), and the whole chain's output is # reads, no remote I/O; always run so engine/password/config
# captured below, so without this the update looks frozen mid-fetch # edits surface on the next pass.
# — noticeable on slow connections. # * remote (locations stats, dashboard, snapshots, app-status,
isNotice "Refreshing backup data (may be slow on poor connections)..." # migrate) — each spawns restic against every enabled location
local result; result=$(webuiGenerateBackupLocations && webuiGenerateBackupDashboard && webuiGenerateBackupSnapshots all && webuiGenerateBackupAppStatus && webuiGenerateBackupEngines && webuiGenerateBackupSchema && webuiGenerateBackupPasswords && webuiGenerateBackupMigrate) # (over SSH for remote repos). Backups and location changes
checkSuccess "Refreshed backup dashboard data..." # already regen this live at the moment they happen
# (backup_app_start.sh / location_*.sh), so this routine pass is
# only a drift catch-up. Throttle it: pull at most once per
# CFG_BACKUP_DASHBOARD_REFRESH_INTERVAL minutes unless a backup
# marked the data dirty since, or the caller forced a full
# refresh (WEBUI_UPDATER_FORCE). 0 disables the throttle.
local result
result=$(webuiGenerateBackupEngines && webuiGenerateBackupSchema && webuiGenerateBackupPasswords)
checkSuccess "Refreshed backup engine/schema/password data..."
local backup_refresh_min="${CFG_BACKUP_DASHBOARD_REFRESH_INTERVAL:-30}"
local backup_stamp="/tmp/libreportal_webui_backup_refreshed"
# Touched by backup_app_start.sh when a backup completes; a value
# newer than the stamp forces the next routine pass to pull.
local backup_dirty="/tmp/libreportal_webui_backup_dirty"
local do_remote=1
if [[ -z "$WEBUI_UPDATER_FORCE" && "$backup_refresh_min" != "0" && -f "$backup_stamp" ]]; then
local _bnow _blast _bdirty
_bnow=$(date +%s)
_blast=$(stat -c '%Y' "$backup_stamp" 2>/dev/null || echo 0)
_bdirty=$(stat -c '%Y' "$backup_dirty" 2>/dev/null || echo 0)
if (( _bnow - _blast < backup_refresh_min * 60 )) && (( _bdirty <= _blast )); then
do_remote=0
fi
fi
if (( do_remote )); then
# Announce before running: the chain reaches remote backup
# locations (restic over SSH) and its output is captured below,
# so without this the update looks frozen mid-fetch on slow links.
isNotice "Refreshing backup data (may be slow on poor connections)..."
# One unfiltered restic snapshots pull per location, shared across
# the generators below instead of one each — see engineSnapshotsJson.
export LP_SNAP_CACHE_DIR; LP_SNAP_CACHE_DIR="$(mktemp -d 2>/dev/null)"
result=$(webuiGenerateBackupLocations && webuiGenerateBackupDashboard && webuiGenerateBackupSnapshots all && webuiGenerateBackupAppStatus && webuiGenerateBackupMigrate)
checkSuccess "Refreshed backup dashboard data..."
[[ -n "$LP_SNAP_CACHE_DIR" ]] && rm -rf "$LP_SNAP_CACHE_DIR"
unset LP_SNAP_CACHE_DIR
touch "$backup_stamp" 2>/dev/null || true
else
isNotice "Backup data current — remote refresh throttled (last pull <${backup_refresh_min}m ago)."
fi
# Peers (named other LibrePortal instances) — small, cheap; lives # Peers (named other LibrePortal instances) — small, cheap; lives
# in its own data/peers/generated/peers.json file consumed by # in its own data/peers/generated/peers.json file consumed by