# Backups and Recovery

---

# Backup and Recovery

_Acta / Backups and Recovery_

> Protect PostgreSQL data, installation keys, and matching release executables.

The independent `acta-backup` service protects a PostgreSQL cluster with
pgBackRest backups and archived write-ahead logs (WAL). Its policy and job
state remain available when Acta or the source database is unavailable.
Browser controls require a human account with Manage backups; restoration and
traffic cutover remain operator actions.

## Choose a procedure

Use these pages for the relevant stage:

- [Backup Coverage](/acta/backups-and-recovery/backup-coverage.md)
- [Provisioning Backups](/acta/backups-and-recovery/backup-setup.md)
- [Backup Policy and Health](/acta/backups-and-recovery/backup-policy.md)
- [Restoring an Installation](/acta/backups-and-recovery/restoring.md)
- [Cutting Over to Restored Data](/acta/backups-and-recovery/recovery-cutover.md)
- [Checking Backup Recovery](/acta/backups-and-recovery/recovery-checks.md)

---

# Backup Coverage

_Acta / Backups and Recovery_

> Identify the database, keys, release executable, and external files needed for recovery.

A recoverable installation needs the database, its installation key, recovery
credentials, and the exact executable recorded with its backup.

Backups cover the complete PostgreSQL cluster, including tasks, comments,
memberships, memories, MCP grants, and document bytes and versions. Physical
backups cover every database in the cluster: use a dedicated
Acta cluster or explicitly account for other databases when planning recovery.

Each completed backup carries an age-encrypted recovery record: installation
security key, database identity, PostgreSQL major version/replay settings,
migration checksums, installation ID, public URL and exact Acta executable
digest. The matching executable must also be preserved in an operator-managed
release archive, separately replicated off the source machine. This executable
embeds the web bundle. Files stored outside Acta are not covered.

For the integrated Docker package, see [production
deployment](/acta/backups-and-recovery/recovery-service.md).

---

# Provisioning Backups

_Acta / Backups and Recovery_

> Provision the recovery worker, encrypted repository, release archive, keys, and PostgreSQL log archiving.

Provision the independent backup worker and its recovery material before
turning on schedules. A backup stored only on the source machine does not
protect against losing that machine.

## Before you start

The recovery suite exercises PostgreSQL 17, pgBackRest 2.59.1 and age.
Validate upgrades using that suite before changing deployed versions. The
verification-report parser deliberately rejects unexpected formats.

## Provision the worker

1. Run `tools/export`. Install `acta-backup` independently
  of the web executable. Provision pgBackRest and age on the database/recovery
  host.
2. Adapt `deploy/backup/backup.example.json`, `pgbackrest.example.conf`, and
  `acta-backup.service`. The service must have access to the PostgreSQL data
  directory for physical backups, local backup/restore directories, a database
  account able to inspect cluster settings and all Acta tables, and the
  repository. The example runs under PostgreSQL's OS account. Keep all
  deployment paths and credentials operator-owned; none are editable through
  the web API.
3. Provision an encrypted **off-machine** repository. A local path in the
  example is suitable only when that mount is independently durable. Configure
  transport and credentials using pgBackRest, including repository encryption.
  Disable pgBackRest auto-expiration (`expire-auto=n`), external expiration
  timers and unrelated backup schedulers for this stanza: Acta owns retention
  decisions.
4. Create an age key using `age-keygen -o recovery.age`, record its public
  recipient with `age-keygen -y recovery.age`, and keep an independently
  recoverable private copy. Keep the repository cipher password and access
  credentials outside the failed machine too. A backup without its recovery
  keys is not recoverable. `recovery_identity_file` is optional for
  backup-only workers; it is required for automated drills. This worker
  possesses decryption authority when enabled.
5. Provision private mode-0600 `database_url_file`, `security_key_file` (the
  actual Acta key, never a newly generated replacement) and `token_file` (at
  least 32 random characters). `state_dir` and `restore_root` must be private
  durable directories. Set up the socket parent with a dedicated shared group:
  the worker owns it; the web process can traverse it and access the 0660
  socket. If running under different users, each process gets its own 0600
  copy of the same API token.
6. Archive the deployed `acta-server` under
  `release_dir/<sha256>/acta-server`, immutable and executable. `release_file`
  is JSON with `id`, `sha256`, `public_url`, and a stable `installation`
  identifier. Replicate the release archive and recovery configuration
  independently, retaining artifacts for at least as long as any backup refers
  to them. Deployment must update the release record together with the server.
  A backup spanning a schema, key, or release change fails consistency
  verification; take a new full backup.
7. Configure PostgreSQL `archive_mode=on` and an `archive_command` using the
  provisioned pgBackRest stanza, for example `pgbackrest
  --config=/etc/acta-backup/pgbackrest.conf --stanza=acta archive-push %p`.
  `archive_mode` needs a PostgreSQL restart. Set `archive_timeout` within the
  chosen archive-age limit for continuous recovery. Run `stanza-create` and
  `check` with the same operator configuration before starting the service.
  Both snapshot and continuous modes need the WAL necessary to make physical
  backups consistent; continuous mode additionally monitors ongoing archive
  freshness.
8. Start the independent service. Set `ACTA_BACKUP_SOCKET` and
  `ACTA_BACKUP_TOKEN_FILE` on the web process, then restart the web process.
  Automatic backups start **disabled**. Select policy explicitly in Site
  Settings, take a full backup and complete a restore drill before relying on
  the deployment.

The systemd example must be adapted to actual mount/socket paths. PostgreSQL
and pgBackRest need access under the configured service account. Recovery
PostgreSQL uses a private Unix socket and no TCP listener, with preload
libraries and logical replication workers disabled. These controls do not
replace an OS/container egress boundary: allow access only to the backup
repository during drills, with no access to external clients, mail, webhook
or push endpoints. Repository credentials and operator logs are sensitive; do
not expose them through Acta.

## Check the result

In Site Settings, inspect backup health, run a full backup, and complete a
restore drill. Check that the matching release and recovery keys are available
independently of the source machine.

---

# Adding the Recovery Service

_Acta / Backups and Recovery_

> Add the independent backup worker to the production Compose deployment and verify its repository access.

The Compose recovery service keeps backup policy, jobs, release archives, and
restore targets outside the application process. Provision its repository
independently before relying on it.

The `backups` profile runs the existing independent recovery worker. Its
policy, release archive, socket, and restore targets use durable named
volumes. It stays alive during app/DB outages. The supplied package uses a
**mounted repository**; provision an independently durable/off-machine mount
before enabling it. App and Caddy have no access to the repository or restored
databases. The backup container has only the internal database network and no
Internet route. A direct S3/SFTP repository needs an explicitly restricted
egress setup before using it here.

Follow [the recovery runbook](/acta/backups-and-recovery/backups.md) for keys, retention,
verification and cutover semantics. Before enabling the profile:

1. Create `/srv/acta-config/backup` mode 0700. Copy
  `deploy/production/backup.example.json` as `backup.json` and
  `deploy/production/pgbackrest.example.conf` as `pgbackrest.conf`, both mode
  0600.
2. Supply a random repository cipher password in `pgbackrest.conf`. Provision
  `recovery.age` privately in that directory and its public recipient in
  `backup.json`; retain independent recovery copies. The worker's UID 999
  needs read/write access to the repository mount; give only that account
  access.
3. Write a private `backup/database-url` containing
  `postgres://postgres@/acta?host=/var/run/postgresql`. This uses the private
  shared PostgreSQL socket and local authentication, not a published
  administrator port. Only the database and recovery worker mount that socket.
4. Set `ACTA_BACKUP_REPOSITORY` in `.env` to the absolute provisioned mount
  path. The deployment refuses to create a missing mount directory. The worker
  copies operator files privately into its durable configuration area and
  archives the exact server executable on each start. Keep app and worker on
  the same image build/release; include both when updating, and take a new
  full backup afterward.
5. Start the worker first to prepare its config, then enable DB archiving
  using the additional Compose file. The worker starts with automatic
  schedules disabled. Its image copies `acta-backup` from `bin/`, which
  `tools/export` writes:

```sh
docker compose --env-file /srv/acta-config/.env -f compose.production.yaml \
  --profile backups up -d --build backup

docker compose --env-file /srv/acta-config/.env -f compose.production.yaml \
-f compose.backups.yaml --profile backups up -d --build

docker compose --env-file /srv/acta-config/.env -f compose.production.yaml \
-f compose.backups.yaml --profile backups exec -u 999:10001 backup \
pgbackrest --config=/var/lib/acta-backup/config/pgbackrest.conf --stanza=acta
stanza-create

docker compose --env-file /srv/acta-config/.env -f compose.production.yaml \
  -f compose.backups.yaml --profile backups exec -u 999:10001 backup \
  pgbackrest --config=/var/lib/acta-backup/config/pgbackrest.conf --stanza=acta check
```

Retain both Compose files and the profile for subsequent starts. Monitor WAL
disk usage immediately: failed archiving retains WAL on the database disk.
Configure policy in Site Settings, take a full backup and pass a restore drill
before relying on it. Configure a reachable independent alert mechanism and
external uptime monitor. To run offline recovery, use
`docker compose run --rm --no-deps backup` with the worker's flags preceding
`restore`; restore never starts app workers. Replicate the release archive and
recovery credentials independently along with repository data. A local named
volume alone is not disaster recovery.

---

# Backup Policy and Health

_Acta / Backups and Recovery_

> Interpret scheduling, retention, restore evidence, log freshness, and independent backup alerts.

Backup health separates integrity checks, successful restore drills, and
archived-log freshness. A successful upload alone does not establish
recoverability.

Schedules are fixed intervals anchored to an explicit UTC timestamp, rather
than local wall-clock cron. Set the anchor to the desired first slot.
Daylight-saving changes do not shift that UTC cadence. Backup/full/drill
intervals and freshness alerts are configurable in minutes; missed slots
coalesce into one job. Full backups take precedence, then incrementals, then
drills. Failures back off five minutes. A saved job captures its policy and
keeps the same UUID through crash reconciliation. Jobs never overlap and
policy changes are refused while busy.

Retention counts full chains, bounded by operator minimum/maximum (minimum
two). No expiration occurs before a full backup passes a real restore. The
most recent restored full chain stays protected until a newer full passes;
unverified newer fulls prevent cleanup. Extra retention therefore consumes
space deliberately when drills fail. WAL retention follows retained full
chains. Monitor repository free space independently. Switching destination
changes future jobs, leaving previous repositories untouched; the operator
remains responsible for their retention.

A recovery point is complete only after the metadata consistency check;
integrity also requires a valid pgBackRest report, including every checked
data/WAL file. Upload success and exit status alone do not establish
integrity. A drill starts PostgreSQL from backup, validates cluster
identity/schema/key and document hashes, then invokes the **recorded
release's** read-only verifier, including account security-data decryption.
The drill tests the newest integrity-checked full backup. Standalone restores
can target any set, including incrementals and explicit times.

Health distinguishes last integrity-checked backup, last successful restore,
and last archived-WAL evidence. It does not invent a guaranteed latest
recoverable instant from one archive timestamp. Repository errors, stale
evidence, disabled schedules, failed jobs, and overdue drills are visible.
Jobs keep the latest 100 records. The settings page retains the latest 500
recovery points plus older full/restore evidence; the standalone `list`
command provides the complete catalog. Catalog inspection streams individual
records, discarding unneeded encrypted metadata rather than accumulating it in
memory. Protected pgBackRest/operator logs provide lower-level diagnostics.

`alert_command` can name an operator-owned executable. On health changes it
receives JSON on stdin (`kind: acta.backup.health`, `warnings`, `at`); failed
delivery is visible and retried after five minutes. This path works without
the Acta DB. Configure it before production and test its delivery. Also use an
external uptime monitor for the backup service/host itself: no process can
report its own death. Health inspection runs between bounded jobs, so size job
timeouts and the external monitor's deadline for the installation. State write
errors fail closed and cause service restart; policy is never acknowledged
before its atomic durable write.

---

# Restoring an Installation

_Acta / Backups and Recovery_

> Recover a backup or point in time into a private target and verify it without starting application workers.

Restore into a separate private target when the source service or database is
unavailable. Verification does not start application workers or switch
production traffic.

## Before you start

Run commands under the recovery PostgreSQL OS account. All flags precede the
command. Restore is offline with respect to the source database and the Acta
web service; only the repository, keys, matching release archive, PostgreSQL
binaries, and adapted operator configuration are required. Keep target paths
short and use letters, digits, `/`, `.`, `_`, `-` only.

## Restore a recovery point

The commands list available points and restore the selected backup:

```sh
acta-backup --config /etc/acta-backup/backup.json --destination offsite list
acta-backup --config /etc/acta-backup/backup.json --destination offsite \
  --set "<backup-label>" --identity /secure/recovery.age \
  --target /var/lib/acta-restores/review restore
```

Replace `<backup-label>` with a label from the catalog.

For point-in-time recovery add `--time 2026-09-10T12:30:00Z` before `restore`.
Choose an appropriate backup before that time and preserve all required WAL. A
missing WAL segment or an unreachable target must fail recovery; never
interpret it as permission to recover to a different time. Snapshot restore
stops at the selected backup's earliest consistent point. Both paths refuse an
existing target, even an empty directory. Failed targets remain for diagnosis;
do not overwrite them.

## Check the result

Successful recovery leaves stopped PostgreSQL data, a private recovered
`security.key`, encrypted-source metadata decrypted in `acta-recovery.json`,
and `verified.json` evidence. Inspect row counts, document verification,
release identity, and requested time. Protect the entire target as secret
data. Verification runs no application workers, network listeners, or
migrations. For a separate UI review on an isolated host, start the recorded
executable with `ACTA_RECOVERY_MODE=true`, the recovered database and key;
that disables push, MCP/OAuth entry points, backup controls, and
push mutations. This is not a replacement for network isolation and is not a
production setting.

---

# Cutting Over to Restored Data

_Acta / Backups and Recovery_

> Prepare restored security state, stop competing writers, switch production traffic, and verify a fresh backup.

Cutover makes restored data writable production state. Stop the former
installation and prepare the restored security state before reopening access.

## Before you start

Stop all writers to the original installation and isolate the restored target.
First keep an independently verified backup of any surviving current
production state. Cutover deliberately requires an operator action outside the
browser.

## Prepare the target

Run preparation against the verified target:

```sh
acta-backup --config /etc/acta-backup/backup.json \
  --target /var/lib/acta-restores/review prepare-cutover
```

Preparation requires a verified, stopped target. It rechecks the restored
data, then transactionally revokes **all** restored browser, CLI and MCP
sessions, clears in-flight authentication/setup grants, and discards pending
push deliveries. Old notifications are marked read/resolved, retaining their
historical records. Users and external clients must authenticate again.
Passwords and
MFA enrollment remain; credentials legitimately changed after the backup have
rolled back, so apply any known post-backup account disables/password changes
before reopening access. Preparation is repeatable and records
`prepared.json`; it does not switch traffic.

Restore and cutover preparation attempt to stop their isolated PostgreSQL
process on failure, using a separate cleanup timeout even if the operation was
canceled. A cleanup failure is reported alongside the original error. If the
error includes `stop recovery PostgreSQL`, inspect the target's PostgreSQL log
and confirm that its process has stopped before retrying or removing the
target.

## Switch the installation

Configure production PostgreSQL explicitly (including WAL archiving,
authentication, network bindings, and paths), preserving the recovered data
and matching key/release. Do not reuse the verification configuration as a
production configuration. Start Acta against that target with recovery mode
disabled, validate locally, then switch the service/proxy. Re-provision push
subscriptions after sign-in. Keep the former installation stopped to avoid two
writable copies. Take and drill a new full backup after cutover before
resuming ordinary retention.

## Check the result

Confirm that the former installation remains stopped, users authenticate
against the restored installation, and a new full backup passes its restore
drill.

---

# Checking Backup Recovery

_Acta / Backups and Recovery_

> Run isolated snapshot, point-in-time, integrity, and cutover-preparation checks.

Use the isolated recovery suite to exercise backups, point-in-time recovery,
verification, and preparation for cutover. Keep its fixtures separate from
live data.

`python3 scripts/test-backup-recovery.py` builds a fresh disposable PostgreSQL
17 container with no network, an encrypted repository, real migrations and
seeded account/task/document/session/notification data. It tests snapshot
recovery, PITR excluding a later transaction, matching executable/key
verification, revoking restored sessions and alerts, existing-target refusal,
deliberate corruption and retention protection. It never connects to the
development or production DB. `KEEP_BACKUP_TEST=1` retains the container and
private scratch files for diagnosis. Also run
`go test -race ./internal/backup`, normal backend integration tests and
`node web/tests/backups.browser.mjs` (from `web`, use
`node tests/backups.browser.mjs`).

References: [pgBackRest commands](https://pgbackrest.org/command.html),
[PostgreSQL continuous
recovery](https://www.postgresql.org/docs/17/continuous-archiving.html).
