homek8s/infrastructure/garage/backup/garage-backup.sh

88 lines
4.1 KiB
Shell
Raw Normal View History

infrastructure(garage): commit the config, prove WG-only, split keys, back it up, scrape it Garage was the only piece of the platform in no git repository and reachable from more than WireGuard. This is a hardening/ownership pass, not a migration — the container stays on the Pi. - infrastructure/garage/: docker-compose.yml (pinned dxflrs/garage:v1.1.0, every publish on an explicit 10.10.0.1 / 127.0.0.1 address, no 0.0.0.0), garage.toml (rpc_secret moved to /etc/garage/garage.env, [s3_web]/3902 removed, admin API on 3903 for metrics), README (apply, state, restore, keys, backup, metrics) and an explicit note that Flux does not apply it. - host/garage-firewall.nft: PREROUTING priority -140 drop of 3900-3903 from anything but lo/wg0/wg-remote. Docker's DNAT bypasses ufw's INPUT chain, so ufw alone cannot stop a compose typo from publishing S3 on the LAN. Measured: a 0.0.0.0:3902 publish stayed unreachable from the LAN and the drop counter incremented. - backup/: garage-backup.sh meta|data + systemd timers (meta hourly, data nightly) rsyncing to [email protected]:/root/garage-backup over wg-remote. Nothing else backs up Forgejo or the PG PVCs; that stays uncovered. - monitoring/controllers/home/garage.yaml: Service + EndpointSlice + ServiceMonitor for 10.10.0.1:3903 and five alerts. Target is UP. - apps/base/siriusdevops/networkpolicy-site-egress.yaml: first policy in the namespace. CiliumNetworkPolicy, not a k8s NetworkPolicy: 10.10.0.1 is a cluster node (reserved:remote-node), so ipBlock 10.10.0.1/32 is inert. Egress whitelist = TCP 3900 to a node + cluster DNS; measured 403 on 3900, timeouts on 3900-adjacent 5000/3903, on 10.10.0.2:8080 and on the internet. Keys: ci-push (W only, siriusdevops-media) and site-fetch (R only, same bucket) replace the shared ci-key, which is deleted. The k8s Secret and the two Forgejo repo secrets were repointed. No key material is in this commit.
2026-11-19 16:32:58 -05:00
#!/usr/bin/env bash
# Garage backup — meta (frequent) and data (nightly), copied OFF the Pi.
#
# garage-backup.sh meta # consistent metadata snapshot + layout + rpc env
# garage-backup.sh data # object data tree
#
# Destination is the Linode VPS (10.20.0.1) over the wg-remote tunnel, NOT the
# Pi and NOT a bucket in Garage. A bucket that backs up into itself is not a
# backup: losing meta/ or the disk loses the "backup" with it.
#
# Run as root (systemd unit does): node_key is 0600 root:root, and rsync needs
# to read the whole meta tree. The SSH key is the sirius user's linode_admin,
# readable by root.
#
# Restore procedure: see ../README.md. Exit code is the truth — the units are
# oneshot and `systemctl status garage-*-backup.service` shows the last result.
set -euo pipefail
mode=${1:-}
case "$mode" in
meta|data) ;;
*) echo "usage: $0 meta|data" >&2; exit 2 ;;
esac
GARAGE_DIR=/home/sirius/websites/garage
KEY=/home/sirius/.ssh/linode_admin
REMOTE=[email protected]
DEST=/root/garage-backup
KEEP_LOCAL_SNAPSHOTS=14
# Host key: the Pi's root 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 the key later changes. The transport is the WireGuard
# tunnel, so first contact is already authenticated at the network layer.
export RSYNC_RSH="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)" "$*"; }
# Fail loudly and early if the tunnel/target is not there. A silent "backup ok"
# with the VPS unreachable is the failure mode this whole file exists to avoid.
log "target check: $(ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ConnectTimeout=20 "$REMOTE" 'hostname; df -h / | tail -1')"
# rsync will not create the parent of a --delete target, so make the tree first.
ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new "$REMOTE" \
"install -d -m 0700 $DEST $DEST/meta $DEST/meta/snapshots $DEST/data"
case "$mode" in
meta)
# `meta snapshot` is Garage's supported consistent copy of db.sqlite while
# the server keeps running. Never rsync a live db.sqlite+wal by hand.
log "taking metadata snapshot"
docker exec garage /garage -c /etc/garage.toml meta snapshot --all
# Prune local snapshots so /home does not fill with dailies.
mapfile -t snaps < <(find "$GARAGE_DIR/meta/snapshots" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p\n' \
| sort -rn | awk '{print $2}')
if (( ${#snaps[@]} > KEEP_LOCAL_SNAPSHOTS )); then
log "pruning $(( ${#snaps[@]} - KEEP_LOCAL_SNAPSHOTS )) old local snapshot(s)"
printf '%s\0' "${snaps[@]:$KEEP_LOCAL_SNAPSHOTS}" | xargs -0 -r rm -rf
fi
log "syncing metadata to $REMOTE:$DEST/meta"
rsync -a --delete "$GARAGE_DIR/meta/snapshots/" "$REMOTE:$DEST/meta/snapshots/"
# Layout + node identity + the RPC secret. Without node_key the restored
# node gets a new identity and meta/data no longer agree.
rsync -a "$GARAGE_DIR/meta/cluster_layout" \
"$GARAGE_DIR/meta/data_layout" \
"$GARAGE_DIR/meta/node_key" \
"$REMOTE:$DEST/meta/"
install -d -m 0700 /etc/garage
rsync -a /etc/garage/garage.toml /etc/garage/garage.env "$REMOTE:$DEST/meta/"
ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new "$REMOTE" \
"chmod 600 $DEST/meta/garage.env $DEST/meta/node_key"
;;
data)
log "syncing object data to $REMOTE:$DEST/data"
# --delete keeps the remote an exact mirror (deleted objects stay deleted).
# multipart/ is in-flight upload scratch and is deliberately not mirrored.
rsync -a --delete --exclude '/multipart/' \
"$GARAGE_DIR/data/" "$REMOTE:$DEST/data/"
;;
esac
log "remote state after $mode backup:"
ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new "$REMOTE" \
"du -sh $DEST/* 2>/dev/null; echo '--- newest meta snapshot ---'; ls -1dt $DEST/meta/snapshots/*/ 2>/dev/null | head -3"
log "OK"