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:
parent
094ee2c704
commit
c1fe485469
12 changed files with 713 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
|||
---
|
||||
# Nightly backup of every piece of state on this cluster that is not
|
||||
# rebuildable from git: Forgejo's data PVC and the CloudNativePG databases.
|
||||
# rebuildable from git: Forgejo's data PVC, the CloudNativePG databases and the
|
||||
# plain Postgres Deployment in `osint`.
|
||||
#
|
||||
# Why this lives in namespace `forgejo` (and not a `backup` namespace):
|
||||
# the biggest artifact is the `forgejo-data` PVC, and a Kubernetes volume
|
||||
|
|
@ -96,6 +97,11 @@ data:
|
|||
BACKUP_HOST=${BACKUP_HOST:[email protected]}
|
||||
BACKUP_DIR=${BACKUP_DIR:-/root/cluster-backup}
|
||||
CNPG_TARGETS=${CNPG_TARGETS:-"forgejo/pg sirius-growth/portal"}
|
||||
# Plain (non-CNPG) Postgres Deployments: <namespace>/<deployment>. There is
|
||||
# no Cluster CR to ask for a primary, and no superuser path from outside, so
|
||||
# the dump runs inside the pod and reads its own POSTGRES_USER/POSTGRES_DB
|
||||
# (local socket, trust auth) — no password in git or in this Job.
|
||||
PG_TARGETS=${PG_TARGETS:-"osint/osint-db"}
|
||||
DATA_SRC=${DATA_SRC:-/src}
|
||||
DATA_KEEP=${DATA_KEEP:-7}
|
||||
DB_KEEP=${DB_KEEP:-14}
|
||||
|
|
@ -143,6 +149,8 @@ data:
|
|||
ssh_r "cd '$BACKUP_DIR/forgejo-data' && ln -sfn '$STAMP' latest && du -sh '$STAMP'"
|
||||
|
||||
echo "== databases"
|
||||
# A previous run killed mid-stream can leave a zero-byte `.part` behind.
|
||||
ssh_r "find '$BACKUP_DIR/db' -name '*.part' -mmin +60 -delete 2>/dev/null || true"
|
||||
for target in $CNPG_TARGETS; do
|
||||
ns=${target%%/*}
|
||||
cluster=${target##*/}
|
||||
|
|
@ -170,6 +178,29 @@ data:
|
|||
ssh_r "ls -l '$out'"
|
||||
done
|
||||
|
||||
echo "== plain Postgres deployments"
|
||||
for target in $PG_TARGETS; do
|
||||
ns=${target%%/*}
|
||||
deploy=${target##*/}
|
||||
out="$BACKUP_DIR/db/$ns-$deploy-$STAMP.dump"
|
||||
echo "-- $ns/$deploy -> $out"
|
||||
sha_file=$(mktemp)
|
||||
kubectl -n "$ns" exec "deploy/$deploy" -- \
|
||||
sh -c 'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fc -Z6' \
|
||||
| tee >(sha256sum | awk '{print $1}' > "$sha_file") \
|
||||
| ssh_r "cat > '$out.part'"
|
||||
local_sha=$(cat "$sha_file"); rm -f "$sha_file"
|
||||
remote_sha=$(ssh_r "sha256sum '$out.part' | awk '{print \$1}'")
|
||||
if [ -z "$local_sha" ] || [ "$local_sha" != "$remote_sha" ]; then
|
||||
echo "FAIL: sha256 mismatch for $out (local=${local_sha:-none} remote=${remote_sha:-none})"
|
||||
ssh_r "rm -f '$out.part'"
|
||||
exit 1
|
||||
fi
|
||||
ssh_r "mv '$out.part' '$out'"
|
||||
echo " sha256=$local_sha"
|
||||
ssh_r "ls -l '$out'"
|
||||
done
|
||||
|
||||
echo "== manifest"
|
||||
ssh_r "cd '$BACKUP_DIR' && { echo \"stamp $STAMP\"; echo \"host \$(hostname -s)\"; \
|
||||
echo '--- forgejo-data/latest'; du -shL forgejo-data/latest; \
|
||||
|
|
@ -234,6 +265,8 @@ spec:
|
|||
value: /root/cluster-backup
|
||||
- name: CNPG_TARGETS
|
||||
value: "forgejo/pg sirius-growth/portal"
|
||||
- name: PG_TARGETS
|
||||
value: "osint/osint-db"
|
||||
- name: DATA_KEEP
|
||||
value: "7"
|
||||
- name: DB_KEEP
|
||||
|
|
|
|||
46
apps/base/osint/backup-exec-rbac.yaml
Normal file
46
apps/base/osint/backup-exec-rbac.yaml
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
---
|
||||
# Lets the nightly `forgejo/cluster-backup` CronJob take a `pg_dump` of the
|
||||
# plain (non-CNPG) Postgres Deployment `osint/osint-db` through the exec API.
|
||||
#
|
||||
# `osint-db` is a stock `postgres` Deployment, so there is no Cluster object and
|
||||
# no `status.currentPrimary` to look up; the dump is taken inside the pod over
|
||||
# the local socket and uses the pod's own `POSTGRES_USER`/`POSTGRES_DB`, so no
|
||||
# password is copied into `forgejo`, git or the Job.
|
||||
#
|
||||
# Least privilege: the ServiceAccount may run one command inside this
|
||||
# namespace's pods — nothing else.
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: Role
|
||||
metadata:
|
||||
name: cluster-backup
|
||||
namespace: osint
|
||||
labels:
|
||||
app: cluster-backup
|
||||
rules:
|
||||
- apiGroups: [""]
|
||||
resources: ["pods"]
|
||||
verbs: ["get", "list"]
|
||||
- apiGroups: [""]
|
||||
resources: ["pods/exec"]
|
||||
verbs: ["create"]
|
||||
# `kubectl exec deploy/osint-db` resolves the Deployment's ReplicaSet/pod, so
|
||||
# it needs read access to the deployment itself.
|
||||
- apiGroups: ["apps"]
|
||||
resources: ["deployments"]
|
||||
verbs: ["get"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: cluster-backup
|
||||
namespace: osint
|
||||
labels:
|
||||
app: cluster-backup
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: Role
|
||||
name: cluster-backup
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: cluster-backup
|
||||
namespace: forgejo
|
||||
|
|
@ -18,3 +18,6 @@ resources:
|
|||
- news-scraper-deployment.yaml
|
||||
- news-summarizer-deployment.yaml
|
||||
- httproute.yaml
|
||||
# exec-only RBAC so forgejo/cluster-backup can pg_dump this namespace's plain
|
||||
# Postgres Deployment (osint-db) — not a CNPG cluster, so it needs its own path.
|
||||
- backup-exec-rbac.yaml
|
||||
|
|
|
|||
|
|
@ -22,3 +22,5 @@ resources:
|
|||
# exec-only RBAC so forgejo/cluster-backup can pg_dump this namespace's CNPG
|
||||
# cluster (portal) without copying a database password across namespaces.
|
||||
- backup-exec-rbac.yaml
|
||||
# Snapshot mirror of the lead-gen capture PVC to the Linode.
|
||||
- leadgen-backup.yaml
|
||||
|
|
|
|||
150
apps/base/sirius-growth/leadgen-backup.yaml
Normal file
150
apps/base/sirius-growth/leadgen-backup.yaml
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
---
|
||||
# backup: `sgs-leadgen-data` PVC (4.9 MiB) -> Linode VPS over the WireGuard mesh.
|
||||
#
|
||||
# The lead-gen CronJobs (`cronjob-leadgen.yaml`, `cronjob-leadgen-ncda.yaml`)
|
||||
# read and write this claim, so a pod of this namespace must mount it — a PVC
|
||||
# cannot be mounted from `forgejo` where the main cluster-backup job lives.
|
||||
# The mirror is a hardlink `--link-dest` snapshot chain (same shape as the
|
||||
# forgejo-data mirror) so each night is a full restorable directory and only
|
||||
# changed blocks cross the tunnel.
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: leadgen-backup-scripts
|
||||
namespace: sirius-growth
|
||||
labels:
|
||||
app: leadgen-backup
|
||||
data:
|
||||
backup.sh: |
|
||||
#!/bin/bash
|
||||
# Snapshot mirror of /src -> $BACKUP_DIR/leadgen-data/<stamp>/ + `latest`.
|
||||
set -euo pipefail
|
||||
|
||||
if ! command -v rsync >/dev/null || ! command -v ssh >/dev/null; then
|
||||
echo "== installing rsync + openssh-client"
|
||||
apk add --no-cache rsync openssh-client >/dev/null
|
||||
fi
|
||||
|
||||
BACKUP_HOST=${BACKUP_HOST:[email protected]}
|
||||
BACKUP_DIR=${BACKUP_DIR:-/root/cluster-backup}
|
||||
DATA_SRC=${DATA_SRC:-/src}
|
||||
DATA_KEEP=${DATA_KEEP:-7}
|
||||
MIN_FREE_GB=${MIN_FREE_GB:-8}
|
||||
STAMP=${STAMP:-$(date -u +%Y%m%dT%H%M%SZ)}
|
||||
|
||||
SSH_OPTS=(-i /keys/id_ed25519
|
||||
-o UserKnownHostsFile=/keys/known_hosts
|
||||
-o StrictHostKeyChecking=yes
|
||||
-o BatchMode=yes
|
||||
-o ConnectTimeout=20
|
||||
-o ServerAliveInterval=30)
|
||||
ssh_r() { ssh "${SSH_OPTS[@]}" "$BACKUP_HOST" "$@"; }
|
||||
|
||||
echo "== target: $BACKUP_HOST:$BACKUP_DIR stamp=$STAMP"
|
||||
ssh_r "mkdir -p '$BACKUP_DIR/leadgen-data' '$BACKUP_DIR/manifests'"
|
||||
avail_gb=$(ssh_r "df -Pk '$BACKUP_DIR' | awk 'NR==2{printf \"%d\", \$4/1048576}'")
|
||||
avail_gb=${avail_gb:-0}
|
||||
echo "== remote free space: ${avail_gb} GiB (minimum ${MIN_FREE_GB} GiB)"
|
||||
if [ "$avail_gb" -lt "$MIN_FREE_GB" ]; then
|
||||
echo "FAIL: not enough free space on $BACKUP_HOST"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ssh_r "cd '$BACKUP_DIR/leadgen-data' && [ -e latest ] || { mkdir -p .empty && ln -sfn .empty latest; }"
|
||||
|
||||
echo "== leadgen-data: $DATA_SRC -> leadgen-data/$STAMP (link-dest)"
|
||||
rc=0
|
||||
rsync -a --numeric-ids --delete --stats --link-dest=../latest \
|
||||
-e "ssh ${SSH_OPTS[*]}" \
|
||||
"$DATA_SRC/" "$BACKUP_HOST:$BACKUP_DIR/leadgen-data/$STAMP/" || rc=$?
|
||||
case "$rc" in
|
||||
0) ;;
|
||||
24) echo "== rsync exit 24: files changed during the copy (expected)" ;;
|
||||
*) echo "FAIL: rsync exit $rc"; exit "$rc" ;;
|
||||
esac
|
||||
ssh_r "cd '$BACKUP_DIR/leadgen-data' && ln -sfn '$STAMP' latest && du -sh '$STAMP'"
|
||||
|
||||
echo "== manifest"
|
||||
ssh_r "cd '$BACKUP_DIR' && { echo 'stamp $STAMP'; echo \"host \$(hostname -s)\"; \
|
||||
echo '--- leadgen-data/latest'; du -shL leadgen-data/latest; \
|
||||
find leadgen-data/latest -type f -exec sha256sum {} + | sed 's| leadgen-data/latest/| |'; } \
|
||||
>> manifests/$STAMP.txt && tail -8 manifests/$STAMP.txt"
|
||||
|
||||
echo "== retention: last $DATA_KEEP leadgen snapshots"
|
||||
ssh_r "cd '$BACKUP_DIR/leadgen-data' && ls -1dt 20* 2>/dev/null | tail -n +$((DATA_KEEP + 1)) | xargs -r rm -rf"
|
||||
ssh_r "df -h '$BACKUP_DIR' | tail -1"
|
||||
|
||||
echo "RESULT=success stamp=$STAMP"
|
||||
---
|
||||
apiVersion: batch/v1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: leadgen-backup
|
||||
namespace: sirius-growth
|
||||
labels:
|
||||
app: leadgen-backup
|
||||
spec:
|
||||
# 07 minutes after the grafana job; the dataset is tiny, this is only so the
|
||||
# three nightly jobs never overlap on the mesh.
|
||||
schedule: "7 3 * * *"
|
||||
timeZone: "Etc/UTC"
|
||||
concurrencyPolicy: Forbid
|
||||
startingDeadlineSeconds: 1800
|
||||
successfulJobsHistoryLimit: 3
|
||||
failedJobsHistoryLimit: 3
|
||||
jobTemplate:
|
||||
spec:
|
||||
activeDeadlineSeconds: 900
|
||||
backoffLimit: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: leadgen-backup
|
||||
spec:
|
||||
restartPolicy: Never
|
||||
# The PVC is local-path on siriussec.
|
||||
nodeSelector:
|
||||
kubernetes.io/hostname: siriussec
|
||||
containers:
|
||||
- name: backup
|
||||
image: alpine/k8s:1.36.4
|
||||
imagePullPolicy: IfNotPresent
|
||||
command: ["/bin/bash", "/scripts/backup.sh"]
|
||||
env:
|
||||
- name: BACKUP_HOST
|
||||
value: [email protected]
|
||||
- name: BACKUP_DIR
|
||||
value: /root/cluster-backup
|
||||
- name: DATA_KEEP
|
||||
value: "7"
|
||||
volumeMounts:
|
||||
- name: leadgen-data
|
||||
mountPath: /src
|
||||
readOnly: true
|
||||
- name: keys
|
||||
mountPath: /keys
|
||||
readOnly: true
|
||||
- name: scripts
|
||||
mountPath: /scripts
|
||||
readOnly: true
|
||||
resources:
|
||||
requests:
|
||||
cpu: 50m
|
||||
memory: 64Mi
|
||||
limits:
|
||||
ephemeral-storage: 512Mi
|
||||
memory: 256Mi
|
||||
volumes:
|
||||
- name: leadgen-data
|
||||
persistentVolumeClaim:
|
||||
claimName: sgs-leadgen-data
|
||||
readOnly: true
|
||||
- name: keys
|
||||
secret:
|
||||
# Copy of forgejo/cluster-backup-ssh, created out of band.
|
||||
secretName: cluster-backup-ssh
|
||||
defaultMode: 0400
|
||||
- name: scripts
|
||||
configMap:
|
||||
name: leadgen-backup-scripts
|
||||
defaultMode: 0755
|
||||
Loading…
Reference in a new issue