homek8s/infrastructure/garage-linode/pitr-restore.md

177 lines
7.3 KiB
Markdown
Raw Normal View History

# Restore notes — point-in-time recovery for `sirius-growth/portal`
Rehearsed end to end on **2026-11-19** (kanban task `t_ce7d6772`) against the live
archive. This file records the commands and the actual output of that rehearsal,
so the next person restores from a transcript instead of a hope.
The archive: Garage on the Linode, bucket `pg-wal-archive`, `http://10.10.0.5:3900`
over the WireGuard mesh, credentials in Secret `sirius-growth/portal-wal-s3`.
Producer: `spec.backup.barmanObjectStore` on `Cluster sirius-growth/portal`
(WAL archiving continuous, `ScheduledBackup portal-daily` at 03:20 UTC,
`retentionPolicy: 14d`).
## 0. Is the archive actually healthy?
```bash
kubectl -n sirius-growth exec portal-1 -c postgres -- \
psql -U postgres -tAc "select last_archived_wal, last_archived_time, failed_count from pg_stat_archiver"
# 00000001000000000000005B|2026-11-19 22:40:30.1+00|0
```
`failed_count` must be 0 and `last_archived_time` must be minutes old. A
`targetTime` after the last archived WAL cannot be reached — recovery stops at
the last segment it can fetch.
```bash
kubectl -n sirius-growth get backup -o wide # newest barmanObjectStore Backup: completed
kubectl -n sirius-growth get scheduledbackup # portal-daily, LAST BACKUP set
```
## 1. Prove the marker, then make the mistake
The rehearsal wrote a marker, forced the WAL out, waited, then wrote a second
row that must *not* survive recovery:
```bash
q() { kubectl -n sirius-growth exec portal-1 -c postgres -- psql -U postgres -d portal -tAc "$1"; }
q "insert into pitr_probe(note) values ('MARKER-before-target') returning id, at"
# 3|2026-11-19 22:35:06.127778+00
q "select pg_switch_wal()" ; sleep 12
q "select last_archived_wal, last_archived_time from pg_stat_archiver"
# 000000010000000000000059|2026-11-19 22:35:07.089783+00
sleep 10
q "insert into pitr_probe(note) values ('POISON-after-target') returning id, at"
# 4|2026-11-19 22:35:28.959782+00
q "select pg_switch_wal()" ; sleep 12
q "select last_archived_wal, last_archived_time from pg_stat_archiver"
# 00000001000000000000005A|2026-11-19 22:35:29.906592+00
```
The target is therefore **`2026-11-19T22:35:15Z`** — 9 s after the marker, 14 s
before the row that must be lost. Note that WAL `…05A` (which contains the poison
row) *is* in the archive: the restore has to choose to stop, it is not starved of
data.
## 2. Restore into a throwaway cluster
Never restore over the live `portal` cluster while it is serving.
```yaml
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: portal-pitr-test
namespace: sirius-growth
labels: {purpose: pitr-acceptance-test}
spec:
instances: 1
imageName: ghcr.io/cloudnative-pg/postgresql:16
storage: {size: 5Gi, storageClass: local-path}
affinity: {nodeSelector: {kubernetes.io/hostname: siriussec}}
resources:
requests: {cpu: 100m, memory: 256Mi}
limits: {memory: 1Gi}
bootstrap:
recovery:
source: portal-wal
recoveryTarget:
targetTime: "2026-11-19T22:35:15Z"
externalClusters:
- name: portal-wal
barmanObjectStore:
destinationPath: s3://pg-wal-archive
endpointURL: http://10.10.0.5:3900
serverName: portal # MUST match the source Cluster name
s3Credentials:
accessKeyId: {name: portal-wal-s3, key: ACCESS_KEY_ID}
secretAccessKey: {name: portal-wal-s3, key: ACCESS_SECRET_KEY}
region: {name: portal-wal-s3, key: REGION}
```
`serverName` is not optional: it is the barman prefix (`portal/base`,
`portal/wals`). Omit it and the restore cluster looks for its own name and finds
an empty history. `targetTime` is UTC RFC3339 — a wrong timezone silently
restores the wrong hour.
The operator restores the base backup, replays WAL, stops at the target and
promotes:
```
ready=False phase=Setting up primary
ready=False phase=Waiting for the instances to become active
ready=True phase=Cluster in healthy state
portal-pitr-test 1 1 Cluster in healthy state portal-pitr-test-1
```
## 3. The acceptance evidence
```
$ kubectl -n sirius-growth exec portal-pitr-test-1 -c postgres -- \
psql -U postgres -d portal -c "select id, note, at from pitr_probe order by id"
id | note | at
----+----------------------+-------------------------------
3 | MARKER-before-target | 2026-11-19 22:35:06.127778+00
(1 row)
$ ... -tAc "select count(*) from pitr_probe where note like 'POISON%'"
0
```
Marker present, post-target row gone. The rest of the database came back with it
(physical restore, all databases, no `pg_restore` ordering games), e.g.
`clients 4 = 4`, `calendar_tokens 3 = 3`, `applications 1 = 1` against the
primary at that moment.
```
$ ... -tAc "select pg_is_in_recovery()"
f # replay finished, instance promoted
```
`pg_is_in_recovery() = f` is expected here and is **not** a failure: a CNPG
recovery cluster with `instances: 1` promotes itself once it hits the target. It
is writable from that moment — which is why the test cluster must be deleted, and
why nothing may point application traffic at it before you decide to.
```bash
kubectl -n sirius-growth delete cluster portal-pitr-test
```
## 4. Making the restored data the real data
Two shapes, both legitimate:
1. **Cut over (preferred, reversible).** Point the app at
`portal-pitr-test-rw` (`POSTGRES_HOST` in the API/portal Deployments — through
git, not `kubectl set env`), verify, then drop the old cluster only after the
app has been confirmed on the restored copy.
2. **In place.** Keep `portal`, stop its instances, and swap the PVC content from
the recovered data directory. Slower, destructive, and easy to get wrong —
only with the app scaled to zero.
Either way, take a fresh base backup once the decision is made
(`kubectl apply` a `Backup` CR against the surviving cluster) so the archive has
a post-recovery base rather than one from before the incident.
## Gotchas that cost time
- **Flux owns `apps/base/sirius-growth/cluster.yaml`.** A hand `kubectl apply` of
a new `spec.backup` is reverted by the `apps` Kustomization within its 1 min
interval. Config changes belong in the branch/PR. (During the rehearsal the
`kustomize-controller` Deployment was scaled to 0 for the test window and
scaled back afterwards; `Backup`/`Cluster` objects created by hand are not in
the Kustomization inventory, so they are not pruned.)
- **`ScheduledBackup.spec.schedule` is six-field cron, seconds first.** `0 20 3
* * *` = 03:20:00 UTC. Copying a five-field crontab string silently means
"every minute" in the first field.
- **The bucket prefix is `portal/portal/…` only if you get `destinationPath`
wrong.** Use `s3://pg-wal-archive` for the bucket root; barman appends the
server name.
- **CNPG 1.30 warns that in-tree barman-cloud is removed in 1.31.** If that
upgrade happens, migrate to the Barman Cloud Plugin first; until then the
archives remain readable by `barman-cloud-restore` in the same container image.
- **If archiving has failed** (`pg_stat_archiver.failed_count > 0`), fix it and
let a fresh base backup complete before trusting any recent `targetTime`.
- **Fallback when the archive is unusable:** the nightly `pg_dump` in
`forgejo/cluster-backup` (`db/sirius-growth-portal-<stamp>.dump`, 14 retained,
§8.2 step 2 of `docs/cluster-overview.md`). 24 h RPO, logical, independent of
barman and of the Linode Garage node.