Files
aevolution/docs/adr/0074-world-backup-and-restore.md
T
dingoandClaude Opus 5.5 3a34ae39bc fix: M8 review findings (restore ownership, barrier tool, World0 claimRef, CI selection, drill verdicts)
P1: restore-world.sh claims its objects path (mkdir) and the database (a held
session with a restore try-lock and the exclusive upgrade lock), and removes only
what it created, so a losing restore never drops a winner's schema.
P2: barrier Ready-pod selection via shared jsonpath helper with fixtures; barrier
pod fsGroup; script-generated pods inspected by the policy check; World0 PV claim
references protected; ci-paths.sh uses the event's real base and runs on
uncertainty; fault-drill asserts RTO and needs positive quarantine evidence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 22:53:11 -04:00

194 lines
10 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ADR-0074: World backup bundles and verified restore
## Status
Accepted for M8.7, 2026-09-30. It builds on:
- ADR-0061 (immutable, content-addressed objects; upload verified before commit);
- ADR-0067 (clones and history audit);
- ADR-0070 (dev/stage stores);
- ADR-0073 (synthetic load).
The permanent backup destination, encryption, scheduling and WAL archiving wait
on the maintainer's choice of destination (docs/BACKLOG.md). Until then, this ADR
covers everything up to that destination.
## Context
M8.7 requires:
- a coordinated manifest covering the database, the objects, the versions, the
binaries and any in-flight state;
- a restore onto independent storage;
- retention and GC that respect clone and backup pins.
The evidence asked for is:
- rebuilding an isolated world from a backup;
- corruption and missing-object failures;
- credential and key recovery.
The named risk is a backup that exists but cannot reconstruct causal state.
docs/infrastructure/OPERATIONS.md proposes a coordinated admission barrier. Two
facts make a barrier unnecessary for a single database:
- a region's durable state is one PostgreSQL database plus immutable objects;
- every object a committed row references was verified durable before that row
committed.
## Decision
**Bundle.** `tools/backup/backup-world.sh --objects DIR --out DIR` writes:
| File | Content |
|---|---|
| `MANIFEST` | Format, time and label. The server version. Every schema migration and its checksum. Every retained build (image digest) and whether it is retired. Every region head (world, region, frame, sequence, tick, commit, digest, build). Row counts, and the object count and bytes |
| `database.dump` | `pg_dump` custom format of schema `evolution`, with no owners. Grants are kept for an in-place restore (ADR-0075 amendment); every other restore skips them |
| `objects/w<world>/r<region>/<sha256>` | Every object in the store, each checked against its name while it is copied |
| `SHA256SUMS` | Checksums of every other file |
**The cut.** One psql session opens a `REPEATABLE READ, READ ONLY` transaction and
exports its snapshot. The session reads the heads for the MANIFEST, then runs
`pg_dump --snapshot` on that same snapshot while the session holds it. Objects are
copied afterwards.
- Objects are never overwritten or deleted, and no GC exists. So every object the
snapshot references is present and final when it is copied.
- Objects written after the snapshot are copied as harmless orphans.
- Nothing is written to the source, and admission never stops. The backup runs
online while the host commits.
**In-flight state.**
- Every acknowledgement made before the snapshot is in the bundle, because an
acknowledgement follows its commit.
- A commit whose transaction completes after the snapshot is not in the bundle.
It was not acknowledged at the cut.
- Unacknowledged inputs are never durable, by design (ADR-0064).
- Lease rows are copied, but a restored copy is never served under them (see
below).
- There are no cross-region transfers yet. When they exist, they live in the same
database and are covered by the same snapshot.
**Restore.** `tools/backup/restore-world.sh --bundle B --objects DIR` targets an
empty database owned by the restoring login, and fails closed in this order:
1. Every file matches `SHA256SUMS`, no file is unlisted, and every object's name
is its digest. Nothing is touched before this passes.
2. The target has no `evolution` schema and runs the manifest's server version.
3. `pg_restore` runs in one transaction, then the objects are copied.
4. The restored heads equal the MANIFEST heads exactly.
5. Every region's full history is audited with `evo-world-clone audit`. The audit
checks every link's manifest, payload, trace replay and outcomes, from genesis
to the head.
A failure after step 2 removes the restored schema and objects, so an unverified
restore cannot be mistaken for a world. The result is a verbatim, owner-only copy
that keeps the source's world identities. It has no runtime grants and is never
served. (ADR-0075 adds `--in-place` for dev/stage host-loss recovery.)
**Isolated rebuild.** `tools/backup/verify-backup.sh BUNDLE --isolate WORLD`:
1. Starts a private, throwaway PostgreSQL cluster on local disk.
2. Restores the bundle as a fresh login that owns only the restore database.
3. Creates a separate database with its own owner, runtime group and login, and
migrates it with `migrate-postgres.sh`.
4. Clones every region of the source world into `WORLD` with
`evo-world-clone clone`, which audits the source history first. Provenance is
kept in `region_origins`.
5. Runs `evo-server` on each clone under synthetic load until it acknowledges 20
ticks past the backup head, then drains it.
This is OPERATIONS.md steps 3–5 for a clone. Restoring in place under the source's
own identity, with the old writers fenced, is host-loss recovery and belongs to
M8.8.
**Credentials and keys.**
- A bundle carries no roles, passwords or connection details. Since ADR-0075 its
dump keeps grants (role names only), for an in-place restore; the MANIFEST says
`dump privileges`.
- Restoring needs only a login that owns an empty database, and no source
credential. `backup-check.sh` and `verify-backup.sh` prove this with fresh,
unrelated roles.
- Bundles are not encrypted yet, so there is no key to lose. Encryption and its key
custody are chosen together with the destination. They are required before any
World0 data leaves the host.
**Retention and GC.**
- Each bundle is a full, self-contained copy, and no bundle depends on another.
Deleting one never breaks another.
- Clones copy their payloads into the target namespace (ADR-0067), so a clone does
not depend on its source either.
- The object store has no delete, and history never expires.
- Any future GC must prove reachability from every live head, clone origin and
retained build. Bundles hold their own copies, so GC cannot damage them.
- Retention of bundles at the destination (the OPERATIONS proposal: 30 daily, 12
weekly, 12 monthly) is applied when the destination exists.
**Binaries.** The MANIFEST names every build identity that wrote history. These
are the region image digests that recovery and replay need. Harbor must keep those
images; its retention policy is not yet verified (backlog).
**In the cluster.** `tools/backup/cluster-backup.sh <dev|stage> --to DIR [--verify WORLD]`
is an operator action, like `fault-drill.sh`, and refuses World0 before kubectl
runs. It works as follows:
- It runs one short-lived Pod (component `backup`) with:
- the environment's pinned postgres-tools image, which now includes `pg_dump`,
`pg_restore` and `backup-world.sh`;
- the migrator login;
- the `aevolution-objects` volume mounted read-only on the store node;
- a 12 GiB emptyDir for the bundle.
- The database egress policy admits the `backup` component.
- The bundle is streamed out with `kubectl exec tar` and its checksums are checked
locally. Then the Pod is deleted, including on failure.
A scheduled CronJob, synced by Argo, follows once the destination exists.
**Qualification.** `tools/ci/backup-check.sh` runs inside `postgres-check.sh` for
every PostgreSQL build:
- It backs up a real `evo-server` while the server commits under synthetic load.
The backup head covers every acknowledgement made before the backup, and the
source then moves past it.
- It runs `verify-backup.sh --isolate` on a second cluster.
- It restores damaged bundles, each of which must be refused, leaving nothing
behind: a corrupt object, an unlisted file, a damaged dump, a missing payload
that has consistent checksums, and a MANIFEST head that disagrees with the
database.
- It restores a second time into a database that already holds a world, which
must be refused.
## Consequences
- **RPO.** The recovery point is the last verified bundle. Without WAL archiving,
this is a logical backup and cannot recover to an arbitrary point in time.
Continuous archiving on pg-forge-01 needs the destination (backlog). M8.8 decides
the host-loss RPO for World0.
- **Full copies.** Every bundle copies the whole object store. That is fine at
dev/stage sizes (the objects volume is 10 GiB). Incremental object copies, which
are safe because objects are content-addressed, come with the destination.
- **Off host.** A bundle is off the host only if `--to` is on another machine. The
maintainer's workstation qualifies only if it is not the hypervisor host.
- **Restore test.** A scheduled job or a successful upload is not a restore test.
Only `verify-backup.sh` output counts as restore evidence.
## Alternatives rejected
- **Physical base backups (`pg_basebackup`) as the only copy.** They restore the
whole server, with its roles, and cannot be audited per region or restored as an
isolated world without a second step. They remain the likely basis for PITR once
WAL archiving exists.
- **An admission barrier before the cut.** It is unnecessary given
upload-before-commit and immutable objects, and it would pause the world.
- **Restoring directly as a runnable world with fresh grants.** It would serve the
source's world identity from a second place, which could become competing
authority. Isolation goes through the clone path, which re-identifies the world.
- **A new object-storage dependency (MinIO, S3 SDK).** The destination is not
chosen; a directory and `tar` need no new dependency.
## Amendment: restore ownership (M8 review, 2026-09-30)
The review reproduced a race. A restore checked that the target was empty, a
competing actor then created the schema, the restore's `pg_restore` failed, and
its cleanup dropped the other actor's schema. Restores now own what they touch:
- `--objects` is claimed by creating it (`mkdir`, never `-p`). An existing path is
refused and never touched.
- One psql session, held open for the whole restore, takes the restore claim
(advisory 71420004, try-lock, so a concurrent restore refuses at once) and the
upgrade lock (71420002, exclusive). Provisioning, acquisition and lease checks in
that database wait until the restore is verified or removed.
- Cleanup removes only what the invocation created: the schema only if its own
one-transaction `pg_restore` committed, and the directory only if its own `mkdir`
created it.
`backup-check.sh` covers three cases:
- a competing schema created just before `pg_restore` survives;
- a concurrent restore refuses, and the first completes;
- a lease acquisition times out while the restore holds the database.