apps(backup): cover the remaining stateful gaps off-host

Four pieces of state were still only on the machine they run on. All four now
land on the Linode over the WireGuard mesh, in the same /root/cluster-backup
tree (or /root/pihost-backup for the Pi) with the same manifest/sha256 habit:

* monitoring/kube-prometheus-stack-grafana — new CronJob `monitoring/grafana-backup`.
  The image is -distroless (no shell), so this mounts the PVC read-write and
  takes a consistent copy with the sqlite backup API (PRAGMA integrity_check on
  the copy), then streams it: grafana/grafana-<stamp>.db, keep 14.
* osint/osint-db — plain Postgres Deployment, so neither the CNPG primary lookup
  nor CNPG_TARGETS reaches it. `forgejo/cluster-backup` gains a PG_TARGETS loop
  that pg_dump -Fc's inside the pod using its own POSTGRES_USER/POSTGRES_DB
  (exec-only RBAC in apps/base/osint/), sha256 checked on both sides.
* sirius-growth/sgs-leadgen-data — new CronJob `sirius-growth/leadgen-backup`,
  link-dest snapshot mirror, keep 7.
* Pi /etc/wireguard + wgportal sqlite — infrastructure/wireguard/backup/,
  nightly systemd timer -> [email protected]:/root/pihost-backup, keep 14.

Each tree carries its own retention, so adding artifacts cannot crowd the
Linode. Docs §8/§8.1/§8.2 record coverage, the accepted gaps (Prometheus TSDB,
camera frames, runner scratch) and the restore procedure — including the
TimescaleDB pre/post-restore calls osint needs.
This commit is contained in:
Lance Walters 2026-11-19 17:14:36 -05:00
commit c1fe485469
12 changed files with 713 additions and 18 deletions

View file

@ -0,0 +1,82 @@
# WireGuard hub state — backup of `/etc/wireguard` + the wgportal peer database
The Pi (`rpi`, `10.10.0.1`) is the WireGuard **hub** of the mesh *and* a spoke
of the Linode hub (`10.20.0.1`). Two files there are unrebuildable:
| Path | What it is |
|---|---|
| `/etc/wireguard/wg0.conf` | wgportal-autogenerated hub config: wg0 server key, every peer, every `AllowedIPs` |
| `/etc/wireguard/wg-remote.conf` | the Pi→Linode spoke (key + endpoint) |
| `/opt/wgportal/data/sqlite.db` | wgportal's peer database (identifiers, keys, allowed IPs, display names) |
A rebuilt Pi comes back from git + Garage's own backup — but the peer set and
the hub key would be gone, and with them every remote-access path. This
directory mirrors `infrastructure/garage/backup/`: a small script plus a
systemd timer that copies the state **off the Pi** to
`[email protected]:/root/pihost-backup/`.
## This directory is NOT applied by Flux
Flux only reconciles `./clusters/homek8s`. Install these by hand (they are host
units, not cluster objects):
```bash
# from the repo checkout on the Pi (or scp the three files over)
sudo install -m 0755 wireguard-backup.sh /usr/local/sbin/wireguard-backup.sh
sudo install -m 0644 wireguard-backup.service wireguard-backup.timer /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now wireguard-backup.timer
# one hand-fired run, and read the result (exit code is the truth)
sudo systemctl start wireguard-backup.service
systemctl status wireguard-backup.service --no-pager
journalctl -u wireguard-backup.service -n 40 --no-pager
```
Re-run the install after a Pi reinstall — the units do not survive a reimage.
## What lands on the Linode
```
/root/pihost-backup/wireguard/wireguard-<UTC stamp>.tar.gz mode 0600
/root/pihost-backup/manifests/<stamp>.txt sizes + sha256
```
- One tarball per run (`/etc/wireguard/` verbatim, including the `.bak` peer
history, plus `wgportal-sqlite.db`). Retention: **14** tarballs + 14
manifests, enforced by the script.
- The sqlite copy is taken with the **sqlite backup API** (python3 stdlib —
there is no `sqlite3` CLI on the Pi), not `cp`: a live `cp` can capture a
half-written page. The script runs `PRAGMA integrity_check` on the copy and
fails the unit if it is not `ok`.
- The tarball is checksummed on both sides before the `.part` file is renamed.
- Transport is the `wg-remote` tunnel using the existing `sirius` user key
(`/home/sirius/.ssh/linode_admin`). Never the Linode public IP.
## Restore
The tarball contains private keys — treat it as a secret, not an archive.
```bash
# pull the newest tarball and inspect it WITHOUT writing to /etc
ssh -i /home/sirius/.ssh/linode_admin root@10.20.0.1 \
'ls -1t /root/pihost-backup/wireguard/*.tar.gz | head -1' # pick a stamp
ssh -i /home/sirius/.ssh/linode_admin root@10.20.0.1 \
'cat /root/pihost-backup/wireguard/wireguard-<stamp>.tar.gz' \
| tar -tzf -
# restore onto a rebuilt Pi (stop the interfaces first so a running wg-quick
# does not rewrite wg0.conf with SaveConfig = true)
sudo systemctl stop wg-quick@wg-remote || true
sudo wg-quick down wg0 || true
ssh ... 'cat /root/pihost-backup/wireguard/wireguard-<stamp>.tar.gz' | sudo tar -xzf - -C /
sudo install -m 0600 -o root -g root /dev/null /dev/null # no-op, permissions come from the archive
sudo systemctl start wg-quick@wg0 && sudo systemctl start wg-quick@wg-remote
sudo wg show
```
If only wgportal lost its database: extract `wgportal-sqlite.db` from the
tarball, run `PRAGMA integrity_check` on it, and copy it over
`/opt/wgportal/data/sqlite.db` while the wgportal container is stopped — a
portal restart rewrites peers from live `wg set` state, so stop/start it
deliberately rather than leaving it running mid-restore.

View file

@ -0,0 +1,18 @@
# /etc/systemd/system/wireguard-backup.service (rpi)
#
# Hub state -> Linode VPS. The peer set and the wg0 server key are the one
# thing on the Pi that neither Flux nor a git repo can rebuild; a Pi disk loss
# would otherwise take remote access with it.
[Unit]
Description=WireGuard hub state (etc/wireguard + wgportal sqlite) -> Linode VPS
After=network-online.target docker.service
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/wireguard-backup.sh
# A slow tunnel is not a reason to retry a small job forever.
TimeoutStartSec=10min
Nice=10
# The tarball is an archive of private keys.
UMask=0077

View file

@ -0,0 +1,110 @@
#!/usr/bin/env bash
# WireGuard hub-state backup — /etc/wireguard + the wgportal peer database,
# copied OFF the Pi to the Linode VPS over the wg-remote tunnel.
#
# Why this exists: the Pi is the WireGuard *hub*. Its /etc/wireguard carries the
# wg0 server key and every peer's public key/allowed-ips, and wgportal's sqlite
# is the peer database the UI renders. Losing the Pi's disk would take the whole
# remote-access path with it — including the tunnel you would need to fix it.
#
# Destination is 10.20.0.1 (the hub's VPN address), never the public IP, and
# never the Pi itself: a backup on the machine it protects is not a backup.
# Mirrors infrastructure/garage/backup/ in shape (same key, same host, same
# timer/oneshot style). Not managed by Flux — install it with the steps in
# ./README.md.
#
# Run as root (systemd does): /etc/wireguard is 0600 root:root and
# /opt/wgportal/data is 0700 root:root.
#
# Exit code is the truth: the unit is oneshot, so
# `systemctl status wireguard-backup.service` shows the last result.
set -euo pipefail
KEY=/home/sirius/.ssh/linode_admin
REMOTE=[email protected]
DEST=/root/pihost-backup
KEEP=14
STAGE=/var/lib/pihost-backup
STAMP=${STAMP:-$(date -u +%Y%m%dT%H%M%SZ)}
# Host key: root on the Pi has no known_hosts entry for the VPN peer and a
# rebuilt Pi would not have one either. accept-new pins it on first contact and
# still fails loudly if it later changes; the transport is already an
# authenticated WireGuard tunnel.
SSH="ssh -i $KEY -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ConnectTimeout=20"
log() { printf '%s %s\n' "$(date -u +%FT%TZ)" "$*"; }
log "target check: $($SSH "$REMOTE" 'hostname; df -h / | tail -1')"
$SSH "$REMOTE" "install -d -m 0700 '$DEST' '$DEST/wireguard' '$DEST/manifests'"
install -d -m 0700 "$STAGE"
work="$STAGE/$STAMP"
rm -rf "$work"; install -d -m 0700 "$work"
trap 'rm -rf "$work"' EXIT
log "collecting /etc/wireguard"
# Whole directory including the wgportal-autogenerated wg0.conf, wg-remote.conf
# and their .bak history (the .bak files are how old peer sets survive a bad
# SaveConfig rewrite).
cp -a /etc/wireguard "$work/wireguard"
if [ -f /opt/wgportal/data/sqlite.db ]; then
log "consistent copy of wgportal sqlite"
# A plain cp of a live sqlite file can capture a half-written page; the
# backup API takes a read transaction instead. python3 is on the Pi, there is
# no sqlite3 CLI.
python3 - "$work/wgportal-sqlite.db" <<'PY'
import sqlite3, sys
src = sqlite3.connect("/opt/wgportal/data/sqlite.db", timeout=30)
src.execute("PRAGMA busy_timeout=30000")
dst = sqlite3.connect(sys.argv[1])
src.backup(dst)
dst.close()
src.close()
chk = sqlite3.connect(sys.argv[1])
verdict = chk.execute("PRAGMA integrity_check").fetchone()[0]
peers = chk.execute("SELECT count(*) FROM peers").fetchone()[0]
chk.close()
print(" integrity_check=%s peers=%s" % (verdict, peers))
if verdict != "ok":
sys.exit("FAIL: wgportal sqlite integrity_check returned %r" % verdict)
PY
else
log "WARN: /opt/wgportal/data/sqlite.db missing — backing up /etc/wireguard only"
fi
log "manifest"
( cd "$work" && find . -type f -printf '%s %p\n' | sort -k2 | sed 's| \./| |' > MANIFEST.sizes
find . -type f ! -name MANIFEST.sha256 -print0 | sort -z | xargs -0 -r sha256sum > MANIFEST.sha256 )
manifest_text=$(cat "$work/MANIFEST.sizes")
tarfile="$STAGE/wireguard-$STAMP.tar.gz"
tar -C "$work" -czf "$tarfile" .
local_sha=$(sha256sum "$tarfile" | awk '{print $1}')
local_size=$(wc -c < "$tarfile")
log "shipping $local_size bytes sha256=$local_sha"
$SSH "$REMOTE" "cat > '$DEST/wireguard/wireguard-$STAMP.tar.gz.part'" < "$tarfile"
remote_sha=$($SSH "$REMOTE" "sha256sum '$DEST/wireguard/wireguard-$STAMP.tar.gz.part' | awk '{print \$1}'")
if [ "$local_sha" != "${remote_sha:-none}" ]; then
echo "FAIL: sha256 mismatch (local=$local_sha remote=${remote_sha:-none})"
$SSH "$REMOTE" "rm -f '$DEST/wireguard/wireguard-$STAMP.tar.gz.part'"
exit 1
fi
$SSH "$REMOTE" "mv '$DEST/wireguard/wireguard-$STAMP.tar.gz.part' '$DEST/wireguard/wireguard-$STAMP.tar.gz' && \
chmod 600 '$DEST/wireguard/wireguard-$STAMP.tar.gz'"
# Remote manifest: sizes + checksums, the same evidence habit as the cluster
# backup job (/root/cluster-backup/manifests/<stamp>.txt).
{
printf 'stamp %s\nhost %s\n' "$STAMP" "$(hostname -s)"
printf -- '--- tarball\n%s %s bytes wireguard-%s.tar.gz\n' "$local_sha" "$local_size" "$STAMP"
printf -- '--- contents\n%s\n' "$manifest_text"
} | $SSH "$REMOTE" "cat > '$DEST/manifests/$STAMP.txt'"
log "retention: last $KEEP tarballs"
$SSH "$REMOTE" "cd '$DEST/wireguard' && ls -1t *.tar.gz 2>/dev/null | tail -n +$((KEEP + 1)) | xargs -r rm -f"
$SSH "$REMOTE" "cd '$DEST/manifests' && ls -1t *.txt 2>/dev/null | tail -n +$((KEEP + 1)) | xargs -r rm -f"
$SSH "$REMOTE" "ls -l '$DEST/wireguard/wireguard-$STAMP.tar.gz'; df -h '$DEST' | tail -1"
log "RESULT=success stamp=$STAMP"

View file

@ -0,0 +1,13 @@
# /etc/systemd/system/wireguard-backup.timer (rpi)
[Unit]
Description=Nightly WireGuard hub-state backup
[Timer]
# 04:13 local time, jittered so it does not race the 03:36 garage-data run.
OnCalendar=*-*-* 04:13:00
RandomizedDelaySec=600
Persistent=true
Unit=wireguard-backup.service
[Install]
WantedBy=timers.target