305 lines
12 KiB
YAML
305 lines
12 KiB
YAML
|
|
---
|
||
|
|
# Nightly backup of every piece of state on this cluster that is not
|
||
|
|
# 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
|
||
|
|
# cannot be mounted from another namespace. The pod therefore runs beside the
|
||
|
|
# data it copies; the portal database is reached over the API (see RBAC).
|
||
|
|
#
|
||
|
|
# Design notes (see docs/cluster-overview.md §8.1):
|
||
|
|
# * Destination is the Linode VPS (`[email protected]:/root/cluster-backup`),
|
||
|
|
# reached over the WireGuard mesh — off the T480 AND off the Pi. The same
|
||
|
|
# host already carries `/root/garage-backup` from the Garage pass.
|
||
|
|
# * Forgejo data is a *link-dest snapshot mirror*: rsync only ships changed
|
||
|
|
# blocks but every night leaves a full, restorable hardlinked snapshot.
|
||
|
|
# Never staged on the T480's root fs (it runs close to DiskPressure) —
|
||
|
|
# data flows PVC -> rsync -> ssh, no local temp file.
|
||
|
|
# * Databases are `pg_dump -Fc` streams taken from inside the CNPG primary
|
||
|
|
# (local socket, `-U postgres`, no password in the manifest and no
|
||
|
|
# cross-namespace Secret copy) piped straight to the Linode.
|
||
|
|
# * Credential-free on the DB side is deliberate: nothing in git, nothing to
|
||
|
|
# rotate. The only secret is the backup SSH key (`cluster-backup-ssh`,
|
||
|
|
# created out of band, `from=`-restricted on the Linode).
|
||
|
|
apiVersion: v1
|
||
|
|
kind: ServiceAccount
|
||
|
|
metadata:
|
||
|
|
name: cluster-backup
|
||
|
|
namespace: forgejo
|
||
|
|
labels:
|
||
|
|
app: cluster-backup
|
||
|
|
---
|
||
|
|
# Dump source in this namespace: find the CNPG primary and run pg_dump in it.
|
||
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
||
|
|
kind: Role
|
||
|
|
metadata:
|
||
|
|
name: cluster-backup
|
||
|
|
namespace: forgejo
|
||
|
|
labels:
|
||
|
|
app: cluster-backup
|
||
|
|
rules:
|
||
|
|
- apiGroups: [""]
|
||
|
|
resources: ["pods"]
|
||
|
|
verbs: ["get", "list"]
|
||
|
|
- apiGroups: [""]
|
||
|
|
resources: ["pods/exec"]
|
||
|
|
verbs: ["create"]
|
||
|
|
- apiGroups: ["postgresql.cnpg.io"]
|
||
|
|
resources: ["clusters"]
|
||
|
|
verbs: ["get"]
|
||
|
|
---
|
||
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
||
|
|
kind: RoleBinding
|
||
|
|
metadata:
|
||
|
|
name: cluster-backup
|
||
|
|
namespace: forgejo
|
||
|
|
labels:
|
||
|
|
app: cluster-backup
|
||
|
|
roleRef:
|
||
|
|
apiGroup: rbac.authorization.k8s.io
|
||
|
|
kind: Role
|
||
|
|
name: cluster-backup
|
||
|
|
subjects:
|
||
|
|
- kind: ServiceAccount
|
||
|
|
name: cluster-backup
|
||
|
|
namespace: forgejo
|
||
|
|
---
|
||
|
|
apiVersion: v1
|
||
|
|
kind: ConfigMap
|
||
|
|
metadata:
|
||
|
|
name: cluster-backup-scripts
|
||
|
|
namespace: forgejo
|
||
|
|
labels:
|
||
|
|
app: cluster-backup
|
||
|
|
data:
|
||
|
|
backup.sh: |
|
||
|
|
#!/bin/bash
|
||
|
|
# Nightly state backup: Forgejo data PVC + every CloudNativePG database.
|
||
|
|
#
|
||
|
|
# Layout on the Linode:
|
||
|
|
# /root/cluster-backup/forgejo-data/<UTC stamp>/ hardlinked snapshot
|
||
|
|
# /root/cluster-backup/forgejo-data/latest -> newest snapshot
|
||
|
|
# /root/cluster-backup/db/<ns>-<cluster>-<stamp>.dump pg_dump -Fc
|
||
|
|
# /root/cluster-backup/manifests/<stamp>.txt sizes + sha256
|
||
|
|
#
|
||
|
|
# Everything is streamed: no byte of this backup is staged on siriussec
|
||
|
|
# (the node runs close to DiskPressure and `local-path` lives on it).
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
# alpine/k8s ships kubectl/bash but not rsync/ssh; install them per run
|
||
|
|
# (~2 s). Nothing else is needed: the DB dumps run inside the CNPG pod.
|
||
|
|
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}
|
||
|
|
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}
|
||
|
|
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/forgejo-data' '$BACKUP_DIR/db' '$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
|
||
|
|
|
||
|
|
# First run: --link-dest needs an existing directory to compare against.
|
||
|
|
ssh_r "cd '$BACKUP_DIR/forgejo-data' && [ -e latest ] || { mkdir -p .empty && ln -sfn .empty latest; }"
|
||
|
|
|
||
|
|
echo "== forgejo-data: /src -> forgejo-data/$STAMP (incremental, link-dest)"
|
||
|
|
# Forgejo keeps writing while this runs, so a repack/upload can vanish
|
||
|
|
# mid-copy: rsync reports that as exit 24. That is expected on a live
|
||
|
|
# instance; anything else (23 = partial transfer, 11/12 = protocol) is a
|
||
|
|
# real failure. The two transient upload spools are excluded for the same
|
||
|
|
# reason — they hold half-written packages, never restorable state.
|
||
|
|
rc=0
|
||
|
|
rsync -a --numeric-ids --delete --stats --link-dest=../latest \
|
||
|
|
--exclude=/gitea/data/tmp/ \
|
||
|
|
--exclude=/gitea/data/repo-archive/tmp/ \
|
||
|
|
-e "ssh ${SSH_OPTS[*]}" \
|
||
|
|
"$DATA_SRC/" "$BACKUP_HOST:$BACKUP_DIR/forgejo-data/$STAMP/" || rc=$?
|
||
|
|
case "$rc" in
|
||
|
|
0) ;;
|
||
|
|
24) echo "== rsync exit 24: files changed during the copy (expected on a live Forgejo)" ;;
|
||
|
|
*) echo "FAIL: rsync exit $rc"; exit "$rc" ;;
|
||
|
|
esac
|
||
|
|
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##*/}
|
||
|
|
pod=$(kubectl -n "$ns" get cluster "$cluster" -o jsonpath='{.status.currentPrimary}')
|
||
|
|
db=$(kubectl -n "$ns" get cluster "$cluster" -o jsonpath='{.spec.bootstrap.initdb.database}')
|
||
|
|
if [ -z "$pod" ] || [ -z "$db" ]; then
|
||
|
|
echo "FAIL: $ns/$cluster has no currentPrimary/initdb database"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
out="$BACKUP_DIR/db/$ns-$cluster-$STAMP.dump"
|
||
|
|
echo "-- $ns/$cluster primary=$pod db=$db -> $out"
|
||
|
|
sha_file=$(mktemp)
|
||
|
|
kubectl -n "$ns" exec -c postgres "$pod" -- \
|
||
|
|
pg_dump -U postgres -d "$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})"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
ssh_r "mv '$out.part' '$out'"
|
||
|
|
echo " sha256=$local_sha"
|
||
|
|
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; \
|
||
|
|
echo '--- db'; sha256sum db/*-$STAMP.dump; \
|
||
|
|
echo '--- db listing'; ls -l db; } > manifests/$STAMP.txt && cat manifests/$STAMP.txt"
|
||
|
|
|
||
|
|
echo "== retention: last $DATA_KEEP data snapshots, last $DB_KEEP dump files"
|
||
|
|
ssh_r "cd '$BACKUP_DIR/forgejo-data' && ls -1dt 20* 2>/dev/null | tail -n +$((DATA_KEEP + 1)) | xargs -r rm -rf"
|
||
|
|
ssh_r "cd '$BACKUP_DIR/db' && ls -1t *.dump 2>/dev/null | tail -n +$((DB_KEEP + 1)) | xargs -r rm -f"
|
||
|
|
ssh_r "cd '$BACKUP_DIR/manifests' && ls -1t *.txt 2>/dev/null | tail -n +$((DB_KEEP + 1)) | xargs -r rm -f"
|
||
|
|
ssh_r "df -h '$BACKUP_DIR' | tail -1"
|
||
|
|
|
||
|
|
echo "RESULT=success stamp=$STAMP"
|
||
|
|
---
|
||
|
|
apiVersion: batch/v1
|
||
|
|
kind: CronJob
|
||
|
|
metadata:
|
||
|
|
name: cluster-backup
|
||
|
|
namespace: forgejo
|
||
|
|
labels:
|
||
|
|
app: cluster-backup
|
||
|
|
spec:
|
||
|
|
# UTC everywhere, same convention as every other CronJob in this repo.
|
||
|
|
schedule: "37 2 * * *"
|
||
|
|
timeZone: "Etc/UTC"
|
||
|
|
concurrencyPolicy: Forbid
|
||
|
|
# Missed by more than 30 min (cluster down / node rebooting): skip the night
|
||
|
|
# instead of starting a mirror at a random time.
|
||
|
|
startingDeadlineSeconds: 1800
|
||
|
|
successfulJobsHistoryLimit: 3
|
||
|
|
failedJobsHistoryLimit: 3
|
||
|
|
jobTemplate:
|
||
|
|
spec:
|
||
|
|
# First full mirror of an 18 GiB PVC measured ~15 MB/s over WireGuard
|
||
|
|
# (~20 min); two hours leaves room for a slow night without being killed
|
||
|
|
# half-way through a snapshot.
|
||
|
|
activeDeadlineSeconds: 7200
|
||
|
|
backoffLimit: 1
|
||
|
|
template:
|
||
|
|
metadata:
|
||
|
|
labels:
|
||
|
|
app: cluster-backup
|
||
|
|
spec:
|
||
|
|
restartPolicy: Never
|
||
|
|
serviceAccountName: cluster-backup
|
||
|
|
# The forgejo-data PVC is local-path on siriussec; the pod must run
|
||
|
|
# where the volume is.
|
||
|
|
nodeSelector:
|
||
|
|
kubernetes.io/hostname: siriussec
|
||
|
|
containers:
|
||
|
|
- name: backup
|
||
|
|
# alpine/k8s carries kubectl 1.36.4 (== cluster version) so the
|
||
|
|
# CNPG dumps can be taken through the exec API; rsync + openssh
|
||
|
|
# are added at start (the pod has normal egress).
|
||
|
|
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: CNPG_TARGETS
|
||
|
|
value: "forgejo/pg sirius-growth/portal"
|
||
|
|
- name: PG_TARGETS
|
||
|
|
value: "osint/osint-db"
|
||
|
|
- name: DATA_KEEP
|
||
|
|
value: "7"
|
||
|
|
- name: DB_KEEP
|
||
|
|
value: "14"
|
||
|
|
volumeMounts:
|
||
|
|
- name: forgejo-data
|
||
|
|
mountPath: /src
|
||
|
|
readOnly: true
|
||
|
|
- name: keys
|
||
|
|
mountPath: /keys
|
||
|
|
readOnly: true
|
||
|
|
- name: scripts
|
||
|
|
mountPath: /scripts
|
||
|
|
readOnly: true
|
||
|
|
resources:
|
||
|
|
requests:
|
||
|
|
cpu: 100m
|
||
|
|
memory: 128Mi
|
||
|
|
limits:
|
||
|
|
# Streaming only — this is a hard cap so a regression can
|
||
|
|
# never stage the 18 GiB PVC on the T480's root filesystem.
|
||
|
|
ephemeral-storage: 1Gi
|
||
|
|
memory: 512Mi
|
||
|
|
volumes:
|
||
|
|
- name: forgejo-data
|
||
|
|
persistentVolumeClaim:
|
||
|
|
claimName: forgejo-data
|
||
|
|
readOnly: true
|
||
|
|
- name: keys
|
||
|
|
secret:
|
||
|
|
secretName: cluster-backup-ssh
|
||
|
|
defaultMode: 0400
|
||
|
|
- name: scripts
|
||
|
|
configMap:
|
||
|
|
name: cluster-backup-scripts
|
||
|
|
defaultMode: 0755
|