LibrePortal/scripts/backup/engine/kopia_snapshots.sh
librelad 647b19cf4a restore: ask the repository about a snapshot by id, not by app tag
storageSnapshotSourcePath resolved a snapshot's source path with

    engineSnapshotsJson "$idx" "$snapshot_id"

but that function's second parameter is an app TAG filter. So it ran
`restic snapshots --tag app=<snapshot-id>`, matched nothing, and returned 1 —
every time, for every snapshot, since the file was written.

Nothing broke loudly, because both callers have a fallback:

  * storageRestoreAppTo fell through to "restoring in place", reinstating the
    exact cross-root bug the file exists to fix — restoring onto a host whose
    containers root differs from the source's matched no include path and
    restored nothing, silently
  * the first-run preflight never read a manifest, so every app reported size
    "?" and its fit and location checks passed unconditionally. Thirteen green
    ticks that had checked nothing.

Add engineSnapshotPaths: restic answers it with a positional snapshot id, kopia
by filtering its list. borg has no adapter on purpose — it rebuilds its listing
from archive metadata that carries no paths — so a missing adapter is a quiet
"no" and those callers keep their in-place fallback.

Add scripts/dev/lp-preflight-test, which pins the cases that must say NO: an
app too big for the disk, one this version no longer ships, one whose storage
location is gone, and a resolver that reaches for the app-tag filter again.
Verified against both historical bugs — reintroducing either fails the test.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-27 12:15:15 +01:00

145 lines
3.9 KiB
Bash

#!/bin/bash
# Reshape `kopia snapshot list --json` to match the restic-style {short_id,
# time, hostname, tags, paths} schema the frontend consumes.
kopiaSnapshotsJson()
{
local idx="$1"
local app_filter="$2"
local host_filter="$3"
kopiaEnvExport "$idx" || return 1
local args=(snapshot list --all --json)
local raw
raw=$(runBackupOp kopia "${args[@]}" 2>/dev/null)
local rc=$?
kopiaEnvUnset
[[ $rc -ne 0 || -z "$raw" ]] && { echo "[]"; return $rc; }
local jq_filter='[.[] | {
id: .id,
short_id: (.id[0:8]),
time: .startTime,
hostname: .source.host,
tags: ((.tags // []) | map(sub(":"; "="))),
paths: [.source.path]
}]'
if [[ -n "$app_filter" ]]; then
jq_filter='[.[] | select(any(.tags[]?; . == "app:'"$app_filter"'"))] | '"$jq_filter"
fi
if [[ -n "$host_filter" ]]; then
jq_filter='[.[] | select(.source.host == "'"$host_filter"'")] | '"$jq_filter"
fi
if command -v jq >/dev/null 2>&1; then
echo "$raw" | jq -c "$jq_filter"
else
echo "$raw"
fi
}
kopiaSystemSnapshotsJson()
{
local idx="$1"
local host_filter="$2"
kopiaEnvExport "$idx" || return 1
local raw
raw=$(runBackupOp kopia snapshot list --all --json 2>/dev/null)
local rc=$?
kopiaEnvUnset
[[ $rc -ne 0 || -z "$raw" ]] && { echo "[]"; return $rc; }
local jq_filter='[.[] | {
id: .id,
short_id: (.id[0:8]),
time: .startTime,
hostname: .source.host,
tags: ((.tags // []) | map(sub(":"; "="))),
paths: [.source.path]
}]'
jq_filter='[.[] | select(any(.tags[]?; . == "system:config"))] | '"$jq_filter"
if [[ -n "$host_filter" ]]; then
jq_filter='[.[] | select(.source.host == "'"$host_filter"'")] | '"$jq_filter"
fi
if command -v jq >/dev/null 2>&1; then
echo "$raw" | jq -c "$jq_filter"
else
echo "$raw"
fi
}
kopiaRestoreSystemLatest()
{
local idx="$1"
local target_dir="$2"
local host="${3:-$CFG_INSTALL_NAME}"
local json snapshot_id
json=$(kopiaSystemSnapshotsJson "$idx" "$host")
if command -v jq >/dev/null 2>&1; then
snapshot_id=$(echo "$json" | jq -r 'sort_by(.time) | last | .id // empty')
else
snapshot_id=$(echo "$json" | grep -oE '"id":\s*"[^"]+"' | tail -1 | cut -d'"' -f4)
fi
if [[ -z "$snapshot_id" ]]; then
isError "No system-config snapshot found in $(resticLocationName "$idx") for host=$host"
return 1
fi
# Whole-snapshot restore into staging (no include subpath).
kopiaRestoreSnapshot "$idx" "$snapshot_id" "$target_dir"
}
kopiaSnapshotLatestId()
{
local idx="$1"
local app_name="$2"
local host="${3:-$CFG_INSTALL_NAME}"
local json
json=$(kopiaSnapshotsJson "$idx" "$app_name" "$host")
if command -v jq >/dev/null 2>&1; then
echo "$json" | jq -r 'sort_by(.time) | last | .id // empty'
else
echo "$json" | grep -oE '"id":\s*"[^"]+"' | tail -1 | cut -d'"' -f4
fi
}
kopiaSnapshotListFiles()
{
local idx="$1"
local snapshot_id="$2"
kopiaEnvExport "$idx" || return 1
runBackupOp kopia snapshot list "$snapshot_id" --json 2>/dev/null
local rc=$?
kopiaEnvUnset
return $rc
}
# The path recorded on one snapshot. kopia has no "describe this id" listing, so
# filter the full list; ids are matched whole or by the 8-char short form that
# kopiaSnapshotsJson publishes as short_id.
kopiaSnapshotPaths()
{
local idx="$1"
local snapshot_id="$2"
command -v jq >/dev/null 2>&1 || return 1
kopiaEnvExport "$idx" || return 1
local raw
raw=$(runBackupOp kopia snapshot list --all --json 2>/dev/null)
local rc=$?
kopiaEnvUnset
[[ $rc -eq 0 && -n "$raw" ]] || return 1
printf '%s' "$raw" | jq -r --arg id "$snapshot_id" \
'.[] | select(.id == $id or .id[0:8] == $id) | .source.path' 2>/dev/null
}