From 7c34d0ab1a6cfb15ec77922153afff5e6d708718 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 00:18:40 -0600 Subject: [PATCH] feat(backup): archive pmp_links-only export on all three PM dashboard backup legs (#1744) pm-dashboard-backup.sh now resolves the actual configured projector DB (DATABASE_URL in the pm-projector env file, else the deployed ~/.local/state/pm-projector/projector.db), stages a pmp_links-only SQL export (schema + rows + index definitions) as the conditionally-required manifest member projector/pmp_links.sql, and fails the run before any upload when a present DB cannot be exported. The rest of projector.db is a rebuildable projection and is never archived. New pm-dashboard-backup-pmp-links.test.sh proves export, manifest inclusion, links-only content, env-configured source selection, and isolated restore integrity; manifest suite fixture repaired (events/coverage/aa-last-good) and its expected-member count corrected to 20. SHARED-SCHEMA-MAP updated. Verified live: real 2026-11-04 run staged the export (0 rows at run time) and verified the archive sha256 on local, R2, and Drive legs. --- .../home/pm-dashboard-backup-manifest.test.sh | 9 +- .../pm-dashboard-backup-pmp-links.test.sh | 282 ++++++++++++++++++ nixos/home/pm-dashboard-backup.sh | 62 +++- pm-stack/SHARED-SCHEMA-MAP.md | 26 +- 4 files changed, 362 insertions(+), 17 deletions(-) create mode 100755 nixos/home/pm-dashboard-backup-pmp-links.test.sh diff --git a/nixos/home/pm-dashboard-backup-manifest.test.sh b/nixos/home/pm-dashboard-backup-manifest.test.sh index 1484772bb9..8d9b4556e8 100755 --- a/nixos/home/pm-dashboard-backup-manifest.test.sh +++ b/nixos/home/pm-dashboard-backup-manifest.test.sh @@ -120,7 +120,8 @@ setup_home() { "$RUN_HOME/atlas/meta-quota-export" local f for f in actions.json comments.json fleet-feed.json quota-current.json \ - registry.json quota-gatherer-state.json quota-gatherer-heartbeat.json; do + registry.json quota-gatherer-state.json quota-gatherer-heartbeat.json \ + events.json coverage-snapshot.json aa-last-good.json; do printf '{}\n' >"$RUN_HOME/.local/state/pm-aggregator/$f" done printf '{}\n' >"$RUN_HOME/atlas/meta-quota-export/latest.json" @@ -217,11 +218,11 @@ assert_member missing-optional journals/pm-aggregator.log optional present if manifest_json missing-optional | python3 -c ' import json, sys m = json.load(sys.stdin) -sys.exit(0 if len(m.get("expected", [])) == 14 else 1) +sys.exit(0 if len(m.get("expected", [])) == 20 else 1) '; then - ok "missing-optional: manifest lists all 14 expected members" + ok "missing-optional: manifest lists all 20 expected members" else - fail "missing-optional: manifest expected list is not the full 14-member set" + fail "missing-optional: manifest expected list is not the full 20-member set" fi # --- dump step ran but produced no dir: conditionally required ---------------- diff --git a/nixos/home/pm-dashboard-backup-pmp-links.test.sh b/nixos/home/pm-dashboard-backup-pmp-links.test.sh new file mode 100755 index 0000000000..00c195510b --- /dev/null +++ b/nixos/home/pm-dashboard-backup-pmp-links.test.sh @@ -0,0 +1,282 @@ +#!/usr/bin/env bash +# Tests for pm-dashboard-backup.sh's pmp_links export (code#1744 P0): +# pmp_links is the only non-rebuildable table in the projector DB, so +# the dashboard archive must carry a pmp_links-only export on all +# three legs (local dated archive, R2, Drive — the archive tarball is +# the single artefact all three legs copy, so membership in the +# verified tarball + the legs' sha256 verification is the coverage). +# The script resolves the ACTUAL configured projector DB (the +# pm-projector env file's database URL, else the deployed default +# ~/.local/state/pm-projector/projector.db), stages ONLY the +# pmp_links schema+rows as projector/pmp_links.sql, and records it +# in the manifest as a conditionally-required member: required +# whenever a projector DB was present, optional when there is none. +# A present but unreadable/corrupt DB fails the run before any +# upload — a missing or corrupt export can never report success. +# This drives the REAL script with stubbed tools (the +# pm-dashboard-backup-manifest.test.sh harness shape): scratch HOME, +# stub bws, stub journalctl, stub rclone backed by a local store. +# Scenarios: +# * with-db — fixture DB (pmp_links rows + a pmp_items decoy table) +# at the default state path: run completes and uploads; the +# archive's projector/pmp_links.sql restores into a scratch DB +# with content identical to the source, contains NO pmp_items +# data, and the manifest records the member required + present. +# * env-url — the env file's database URL points at a fixture DB +# elsewhere: the export comes from THAT database. +# * no-db — no projector DB anywhere: run completes, manifest +# records the member optional + missing. +# * corrupt-db — the configured DB file is garbage: run fails, +# nothing is uploaded, the journal marker is unchanged, and the +# log names pmp_links. +set -uo pipefail + +SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +if [[ -f "$SELF_DIR/pm-dashboard-backup.sh" ]]; then + SCRIPT="$SELF_DIR/pm-dashboard-backup.sh" +else + SCRIPT="$SELF_DIR/modules/pm-dashboard-backup.sh" +fi + +command -v sqlite3 >/dev/null 2>&1 || { + echo "pm-dashboard-backup pmp_links tests: sqlite3 required" >&2 + exit 1 +} + +TMP_ROOT="$(mktemp -d)" +trap 'rm -rf "$TMP_ROOT"' EXIT + +failures=0 +fail() { + echo "FAIL: $1" >&2 + failures=$((failures + 1)) +} +ok() { echo "ok: $1"; } + +BIN="$TMP_ROOT/bin" +mkdir -p "$BIN" + +{ printf '#!%s\n' "$BASH"; cat <<'STUB' +if [[ "${1:-} ${2:-}" == "secret list" ]]; then + printf '%s\n' '[{"key":"cloudflare.r2-backups-access-key-id","value":"fake-access-key-id"},{"key":"cloudflare.r2-backups-secret-access-key","value":"fake-secret-access-key"},{"key":"cloudflare.r2-endpoint","value":"https://example.invalid"}]' + exit 0 +fi +echo "stub bws: unsupported args: $*" >&2 +exit 1 +STUB +} >"$BIN/bws" + +{ printf '#!%s\n' "$BASH"; cat <<'STUB' +echo "stub journal line" +exit 0 +STUB +} >"$BIN/journalctl" + +{ printf '#!%s\n' "$BASH"; cat <<'STUB' +store="${RCLONE_STUB_DIR:?RCLONE_STUB_DIR unset}" +calls="${RCLONE_CALLS_LOG:?RCLONE_CALLS_LOG unset}" +mkdir -p "$store" +echo "$*" >>"$calls" +key() { printf '%s' "$1" | tr '/:' '__'; } +cmd="${1:-}" +shift || true +case "$cmd" in + copyto) cp "$1" "$store/$(key "$2")" ;; + cat) + f="$store/$(key "$1")" + [[ -f "$f" ]] || exit 1 + cat "$f" + ;; + lsf) exit 0 ;; + purge) exit 0 ;; + *) + echo "stub rclone: unsupported command: $cmd" >&2 + exit 1 + ;; +esac +STUB +} >"$BIN/rclone" +chmod +x "$BIN/bws" "$BIN/journalctl" "$BIN/rclone" + +PRESET=1000000000 +RC=0 +MARKER_AFTER="" +RUN_HOME="" + +setup_home() { + local name="$1" + RUN_HOME="$TMP_ROOT/$name/home" + mkdir -p "$RUN_HOME/backups/dashboard" "$RUN_HOME/.local/state/pm-aggregator" \ + "$RUN_HOME/atlas/meta-quota-export" + local f + for f in actions.json comments.json fleet-feed.json quota-current.json \ + registry.json quota-gatherer-state.json quota-gatherer-heartbeat.json \ + events.json coverage-snapshot.json aa-last-good.json; do + printf '{}\n' >"$RUN_HOME/.local/state/pm-aggregator/$f" + done + printf '{}\n' >"$RUN_HOME/atlas/meta-quota-export/latest.json" + printf 'history\n' >"$RUN_HOME/atlas/meta-quota-export/history.jsonl" + echo "$PRESET" >"$RUN_HOME/backups/dashboard/.last-run-epoch" +} + +# make_fixture_db +make_fixture_db() { + mkdir -p "$(dirname "$1")" + sqlite3 "$1" <"$TMP_ROOT/$name.log" 2>&1 + RC=$? + MARKER_AFTER="$(cat "$RUN_HOME/backups/dashboard/.last-run-epoch" 2>/dev/null || echo missing)" +} + +archive_dir() { # $1 = scenario name; extracts the run archive to a dir, prints it + local arc="$RUN_HOME/backups/dashboard/2026-10-28/dashboard-2026-10-28.tar.zst" + local d="$TMP_ROOT/$1-extract" + mkdir -p "$d" + tar -I zstd -xf "$arc" -C "$d" 2>/dev/null + printf '%s' "$d" +} + +assert_member() { # $1 scenario, $2 class, $3 status + local d + d="$(archive_dir "$1")" + if python3 -c ' +import json, sys +m = json.load(open(sys.argv[1])) +for e in m.get("expected", []): + if e["path"] == "projector/pmp_links.sql": + sys.exit(0 if (e["class"] == sys.argv[2] and e["status"] == sys.argv[3]) else 1) +sys.exit(1) +' "$d/manifest.json" "$2" "$3"; then + ok "$1: manifest records projector/pmp_links.sql as $2 + $3" + else + fail "$1: manifest does not record projector/pmp_links.sql as $2 + $3" + fi +} + +no_upload() { # $1 scenario name + [[ ! -f "$TMP_ROOT/$1/rclone-calls.log" ]] \ + || ! grep -q '^copyto' "$TMP_ROOT/$1/rclone-calls.log" +} + +canonical_links() { # $1 = sqlite db path; canonical pmp_links content + sqlite3 "file:$1?mode=ro" '.dump pmp_links' +} + +# --- with-db: export staged, restorable, links-only --------------------------- +setup_home with-db +make_fixture_db "$RUN_HOME/.local/state/pm-projector/projector.db" 'first-link-note' +run_backup with-db +if [[ "$RC" -eq 0 ]]; then + ok "with-db: script exited 0" +else + fail "with-db: expected exit 0, got $RC; log: $(cat "$TMP_ROOT/with-db.log")" +fi +D="$(archive_dir with-db)" +if [[ -f "$D/projector/pmp_links.sql" ]]; then + ok "with-db: archive contains projector/pmp_links.sql" +else + fail "with-db: archive lacks projector/pmp_links.sql" +fi +if grep -q 'CREATE INDEX pmp_links_subject' "$D/projector/pmp_links.sql" 2>/dev/null; then + ok "with-db: export carries the pmp_links index definitions" +else + fail "with-db: export lacks the pmp_links index definitions" +fi +if grep -q 'DECOY-PROJECTION-ROW' "$D/projector/pmp_links.sql" 2>/dev/null \ + || grep -q 'pmp_items' "$D/projector/pmp_links.sql" 2>/dev/null; then + fail "with-db: export leaks non-pmp_links content (projection tables must not be archived)" +else + ok "with-db: export is pmp_links-only (no projection tables)" +fi +SCRATCH="$TMP_ROOT/with-db-scratch.db" +rm -f "$SCRATCH" +if sqlite3 "$SCRATCH" <"$D/projector/pmp_links.sql" 2>/dev/null \ + && [[ "$(canonical_links "$SCRATCH")" == "$(canonical_links "$RUN_HOME/.local/state/pm-projector/projector.db")" ]] \ + && [[ "$(canonical_links "$SCRATCH")" == *"first-link-note"* ]]; then + ok "with-db: isolated restore reproduces pmp_links content exactly" +else + fail "with-db: restored pmp_links content differs from source" +fi +assert_member with-db required present +if ! no_upload with-db; then + ok "with-db: archive uploaded to the offsite legs (stub rclone copyto recorded)" +else + fail "with-db: expected uploads to the offsite legs" +fi + +# --- env-url: the env file's database URL selects the source DB ---------------- +setup_home env-url +mkdir -p "$RUN_HOME/elsewhere" "$RUN_HOME/.local/state/pm-projector" +make_fixture_db "$RUN_HOME/elsewhere/custom.db" 'env-url-link-note' +printf '%s=file:%s\n' 'DATABASE_URL' "$RUN_HOME/elsewhere/custom.db" \ + >"$RUN_HOME/.local/state/pm-projector/.env" +run_backup env-url +D="$(archive_dir env-url)" +if [[ "$RC" -eq 0 ]] && grep -q 'env-url-link-note' "$D/projector/pmp_links.sql" 2>/dev/null; then + ok "env-url: export sourced from the env-configured database" +else + fail "env-url: export did not come from the env-configured database (rc=$RC); log: $(cat "$TMP_ROOT/env-url.log")" +fi +assert_member env-url required present + +# --- no-db: not applicable, recorded optional + missing ------------------------ +setup_home no-db +run_backup no-db +if [[ "$RC" -eq 0 ]]; then + ok "no-db: script exited 0" +else + fail "no-db: expected exit 0, got $RC; log: $(cat "$TMP_ROOT/no-db.log")" +fi +assert_member no-db optional missing + +# --- corrupt-db: fail before upload, marker unchanged -------------------------- +setup_home corrupt-db +mkdir -p "$RUN_HOME/.local/state/pm-projector" +printf 'this is not a sqlite database\n' >"$RUN_HOME/.local/state/pm-projector/projector.db" +run_backup corrupt-db +if [[ "$RC" -ne 0 ]]; then + ok "corrupt-db: script exited non-zero ($RC)" +else + fail "corrupt-db: expected non-zero exit, got 0; log: $(cat "$TMP_ROOT/corrupt-db.log")" +fi +if grep -q 'pmp_links' "$TMP_ROOT/corrupt-db.log"; then + ok "corrupt-db: failure output names pmp_links" +else + fail "corrupt-db: failure output does not name pmp_links: $(cat "$TMP_ROOT/corrupt-db.log")" +fi +if no_upload corrupt-db; then + ok "corrupt-db: nothing uploaded" +else + fail "corrupt-db: an upload was attempted" +fi +if [[ "$MARKER_AFTER" == "$PRESET" ]]; then + ok "corrupt-db: journal marker unchanged" +else + fail "corrupt-db: marker advanced — was $PRESET, now $MARKER_AFTER" +fi + +if (( failures > 0 )); then + echo "pm-dashboard-backup pmp_links tests: $failures failure(s)" >&2 + exit 1 +fi +echo "pm-dashboard-backup pmp_links tests: all passed" diff --git a/nixos/home/pm-dashboard-backup.sh b/nixos/home/pm-dashboard-backup.sh index 764ab34eec..6609adaf6e 100755 --- a/nixos/home/pm-dashboard-backup.sh +++ b/nixos/home/pm-dashboard-backup.sh @@ -20,6 +20,13 @@ # ~/backups/dashboard/.last-run.json (#1026), read by the # laptop-backups watchdog (modules/laptop-backups-watchdog.sh). # +# The archive also carries a pmp_links-only export of the projector DB +# (code#1744 P0): pmp_links is the only non-rebuildable table in that DB, +# so the export is staged as a conditionally-required manifest member — +# required whenever a projector DB is present at the configured path — +# while the rest of projector.db (a rebuildable projection) is never +# archived. +# # Credentials come from Bitwarden Secrets Manager at runtime, referenced # by key name only and never logged (the repo-archive-backup pattern): # cloudflare.r2-backups-access-key-id, cloudflare.r2-backups-secret- @@ -285,6 +292,7 @@ ledger_set credentials ok DUMP_SCRIPT="${PM_DB_DUMP_SCRIPT:-$HOME/tools/pm-stack-db-backup.sh}" DUMP_DIR="$HOME/backups/pm-stack/$DATE" DUMP_RAN=0 +PMP_RAN=0 if [ -x "$DUMP_SCRIPT" ]; then ledger_begin db-dump log "running DB dump step: $DUMP_SCRIPT" @@ -333,6 +341,7 @@ optional tree aggregator/charge-inputs required file meta-quota-export/latest.json required file meta-quota-export/history.jsonl conditional tree db-dumps +conditional file projector/pmp_links.sql optional tree aggregator/snapshots optional file journals/pm-aggregator.log optional file journals/quota-gatherer.log @@ -370,6 +379,53 @@ done if [ -d "$DUMP_DIR" ]; then cp -a "$DUMP_DIR" "$STAGE/db-dumps" || fail "dataset copy failed: $DUMP_DIR"; fi +# --- pmp_links export (code#1744 P0) ---------------------------------- +# Source: the ACTUAL configured projector database — DATABASE_URL in the +# pm-projector env file when it names a file: URL, else the deployed +# default ~/.local/state/pm-projector/projector.db (a relative file: +# value resolves against the projector unit's working directory). +# PM_PROJECTOR_DB / PM_PROJECTOR_ENV override both (tests, relocation). +# Only the pmp_links table is exported (schema + rows as SQL): the rest +# of projector.db is a rebuildable projection and credentials never +# enter the archive — the .env file itself is NOT read beyond the +# DATABASE_URL line and is never staged. +PMP_DB="${PM_PROJECTOR_DB:-}" +if [ -z "$PMP_DB" ]; then + PMP_ENV="${PM_PROJECTOR_ENV:-$HOME/.local/state/pm-projector/.env}" + if [ -f "$PMP_ENV" ]; then + _url="$(grep -m1 '^DATABASE_URL=' "$PMP_ENV" 2>/dev/null | cut -d= -f2- | tr -d '"'"'"' \r')" + case "$_url" in + file:*) PMP_DB="${_url#file:}"; PMP_DB="${PMP_DB%%\?*}" ;; + esac + fi + [ -n "$PMP_DB" ] || PMP_DB="$HOME/.local/state/pm-projector/projector.db" + case "$PMP_DB" in + /*) ;; + *) PMP_DB="${PM_PROJECTOR_DIR:-$HOME/git/code/pm-stack/projector}/$PMP_DB" ;; + esac +fi +if [ -f "$PMP_DB" ]; then + command -v sqlite3 >/dev/null 2>&1 || + fail "sqlite3 not found — cannot export pmp_links from $PMP_DB" + PMP_ROWS="$(sqlite3 "file:$PMP_DB?mode=ro" 'SELECT COUNT(*) FROM pmp_links;' 2>/dev/null)" || + fail "pmp_links export failed: cannot read pmp_links in $PMP_DB" + mkdir -p "$STAGE/projector" + sqlite3 "file:$PMP_DB?mode=ro" '.dump pmp_links' >"$STAGE/projector/pmp_links.sql" || + fail "pmp_links export failed: .dump of $PMP_DB" + # .dump omits the table's indexes on this sqlite build — append them + # from sqlite_master so a restore reproduces the full table shape. + sqlite3 "file:$PMP_DB?mode=ro" \ + "SELECT sql || ';' FROM sqlite_master WHERE type = 'index' AND tbl_name = 'pmp_links' AND sql IS NOT NULL;" \ + >>"$STAGE/projector/pmp_links.sql" || + fail "pmp_links export failed: index definitions of $PMP_DB" + grep -q 'CREATE TABLE pmp_links' "$STAGE/projector/pmp_links.sql" || + fail "pmp_links export failed: no pmp_links schema in dump of $PMP_DB" + PMP_RAN=1 + log "pmp_links export staged from $PMP_DB ($PMP_ROWS rows)" +else + log "no projector DB at $PMP_DB — pmp_links export not applicable" +fi + # --- journals: window start = min(last successful run, 24h ago) --- NOW_EPOCH="$(date +%s)" LAST=0 @@ -385,7 +441,7 @@ for svc in pm-aggregator quota-gatherer meta-quota-export; do done # --- manifest (files as staged + the expected set's per-member status) --- -EXPECTED_SET="$EXPECTED_SET" DUMP_RAN="$DUMP_RAN" \ +EXPECTED_SET="$EXPECTED_SET" DUMP_RAN="$DUMP_RAN" PMP_RAN="$PMP_RAN" \ python3 - "$STAGE" "$DATE" "$CREATED_AT" <<'PYEOF' import hashlib, json, os, sys stage, date, created = sys.argv[1], sys.argv[2], sys.argv[3] @@ -412,7 +468,9 @@ for line in os.environ.get("EXPECTED_SET", "").splitlines(): continue cls, kind, rel = parts if cls == "conditional": - cls = "required" if os.environ.get("DUMP_RAN") == "1" else "optional" + ran = os.environ.get("PMP_RAN") if rel.startswith("projector/") \ + else os.environ.get("DUMP_RAN") + cls = "required" if ran == "1" else "optional" member = {"path": rel, "class": cls, "kind": kind} if kind == "tree": full = os.path.join(stage, rel) diff --git a/pm-stack/SHARED-SCHEMA-MAP.md b/pm-stack/SHARED-SCHEMA-MAP.md index 1778949dee..43062eaf82 100644 --- a/pm-stack/SHARED-SCHEMA-MAP.md +++ b/pm-stack/SHARED-SCHEMA-MAP.md @@ -76,17 +76,21 @@ checked-in default is not proof the deployed instance is local SQLite. ## Registry backup legs and current coverage -For `pmp_links`, the intended recovery legs are the PM dashboard archive's -three destinations: local dated archive, Cloudflare R2, and Google Drive -([backup destinations](../nixos/home/pm-dashboard-backup.sh#L1-L11), -[R2/Drive uploads](../nixos/home/pm-dashboard-backup.sh#L522-L538)). But the -current archive manifest stages aggregator files and does **not** include -`projector.db` or a `pmp_links` export ([manifest](../nixos/home/pm-dashboard-backup.sh#L307-L334), -[staged files](../nixos/home/pm-dashboard-backup.sh#L346-L354)). Thus no -existing backup leg for the link registry is evidenced: adding it to and -verifying all three legs is a prerequisite for a later migration, not a -change made by this inventory slice. The aggregator's distinct `registry.json` -is already a required archive member ([manifest lines 321-327](../nixos/home/pm-dashboard-backup.sh#L321-L327)). +For `pmp_links`, the recovery legs are the PM dashboard archive's three +destinations: local dated archive, Cloudflare R2, and Google Drive +([backup destinations](../nixos/home/pm-dashboard-backup.sh#L1-L11)). As of +the #1744 P0 slice (task 9251), every archive run stages a `pmp_links`-only +export (`projector/pmp_links.sql`: schema + rows dumped from the ACTUAL +configured projector database) as a conditionally-required manifest member — +required whenever a projector DB is present at the configured path, so a +missing or corrupt export fails the run before any upload. The rest of +`projector.db` (a rebuildable projection) is deliberately NOT archived. +Restore is replaying the SQL file into a scratch SQLite database; +`nixos/home/pm-dashboard-backup-pmp-links.test.sh` proves export, +manifest inclusion, links-only content, and restore integrity against +fixture databases, and the three legs inherit coverage because all three +copy the same sha256-verified archive tarball. The aggregator's distinct +`registry.json` is already a required archive member. Kaneo/Grist/Pad dumps are only included when the optional external dump script exists ([conditional dump step](../nixos/home/pm-dashboard-backup.sh#L275-L299)); the #3142 design also calls for their own verified native restore paths.