#!/usr/bin/env bash # M8.8 supersession fence (ADR-0075): makes a database whose world was restored # elsewhere permanently unable to grant authority again. # # tools/backup/fence-database.sh --superseded-by LABEL --confirm # # Connection: PG* environment naming the OLD database, as its owner (migrator). # Run it as soon as the old database is reachable, before any of its hosts can # return (or immediately, when it never went away). # # It uses the upgrade barrier (ADR-0068), which serializes with every acquisition, # lease check and provisioning. It first retains a supersession identity, # SHA-256("AEVOLUTION-SUPERSEDED-v1"), which no image digest can equal, and then # retires every other build. Each retirement fences every lease. So: # - a running host's next lease check or commit fails, and it re-acquires; # - no retained build remains current, so every acquisition is refused and the # host quarantines (evo-server exit 4); # - a commit that names a retired build is refused by the trigger. # History, objects and heads are untouched. The database stays readable for audits # and for comparing what was lost. The heads at the moment of fencing are printed: # they are the old environment's final state, and record what the restore rewound. # Undoing this means deliberately retaining a new build as owner: that is an # unfencing decision, not an operation. set -euo pipefail usage() { echo "usage: $0 --superseded-by LABEL --confirm" >&2; exit 2; } label="" confirm=0 while [ $# -gt 0 ]; do case "$1" in --superseded-by) label="${2:-}"; shift 2 ;; --confirm) confirm=1; shift ;; *) usage ;; esac done [[ "$label" =~ ^[A-Za-z0-9._:-]+$ ]] && [ "$confirm" = 1 ] || usage if [ -n "${EVO_PGPASS_SOURCE:-}" ]; then umask 077 export PGPASSFILE PGPASSFILE="$(mktemp /tmp/evo-pgpass.XXXXXXXX)" trap 'rm -f -- "$PGPASSFILE"' EXIT cat "$EVO_PGPASS_SOURCE" > "$PGPASSFILE" fi psql -X --no-password -q -A -t -F ' ' -v ON_ERROR_STOP=1 -v "label=$label" <<'SQL' BEGIN; INSERT INTO evolution.retained_builds(build_identity) VALUES (sha256(convert_to('AEVOLUTION-SUPERSEDED-v1', 'UTF8'))) ON CONFLICT (build_identity) DO NOTHING; SELECT 'retired', encode(b.build_identity, 'hex'), evolution.upgrade_barrier(b.build_identity) FROM evolution.retained_builds b WHERE b.retired_at IS NULL AND b.build_identity <> sha256(convert_to('AEVOLUTION-SUPERSEDED-v1', 'UTF8')) ORDER BY b.build_identity; SELECT 'current', count(*) FROM evolution.retained_builds b WHERE b.retired_at IS NULL AND b.build_identity <> sha256(convert_to('AEVOLUTION-SUPERSEDED-v1', 'UTF8')); SELECT 'held', count(*) FROM evolution.region_leases WHERE holder IS NOT NULL; SELECT 'final', h.world_id, h.region_id, h.commit_sequence, h.end_tick, h.published_epoch FROM evolution.region_heads h ORDER BY 2, 3; SELECT 'superseded-by', :'label'; COMMIT; SQL