homek8s/infrastructure/wireguard/backup/wireguard-backup.sh

110 lines
4.8 KiB
Shell
Raw Permalink Normal View History

#!/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"