Backups and restores

Backups of each database in chains of fulls and increments, their destination, schedule and retention, sealing, restoring into a new database, and moving a database to another deployment.

A deployment has two kinds of backup, and you want both:

  • PostgreSQL's, of each whole store: a standby, base backups and WAL archiving for point-in-time recovery. They are PostgreSQL's tools (your provider's, pgBackRest, WAL-G, …), and recover a store as it was.
  • The server's, of each database, described on this page. They are written outside PostgreSQL, to a directory or an S3-compatible bucket, and restore into a new database.

What a backup is

Each database is backed up in chains. A chain starts with a full: every document's current state, its retained history, and the outcomes of recent transactions (so a retry is still answered after a restore). Each increment after it holds what every partition committed since the backup before. A full is read a page at a time, so the database goes on taking writes; what is written meanwhile is in its stream.

  • A backup counts once it is read back: every object checked against the SHA-256 its manifest names, every checkpoint verified and every event replayed, its state hash checked. A finding fails it.
  • Each partition is cut at its own instant. Every write acknowledged before the backup's earliest cut is in it, and none committed after its latest; between the two, the backup is not consistent across documents of different partitions.
  • Each backup carries its database's collection registry: schemas, and every unfinished schema migration with its artifact.
  • A backup holds no access data: no users, grants or keys.
  • Until an increment holding them is read back, PostgreSQL keeps the events it needs (a history hold), so a stopped schedule or a destination out of reach keeps history in the store, for up to 7 days or twice backup_full_interval, whichever is longer.

A chain's objects are under databases/<database>/chains/<chain>/ of the destination, and go whole when it expires. Its manifests are never sealed: they name every object by key, size and SHA-256, so a chain can be checked without a key.

Settings

Backups are deployment settings, set with mimicd admin settings set and followed by every process without a restart. Nothing is backed up until backup_destination is set.

SettingDefaultMeaning
backup_destinationnonefile:///path, or s3://bucket[/prefix][?endpoint=&region=&force_path_style=]
backup_full_interval1dA chain starts with a full every interval (1 h to 7 d); off or 0: no schedule
backup_interval1hAn increment of the open chain every interval (1 min to 1 d)
backup_retention7dA scheduled chain's increments are kept this long from its start
backup_daily_retention30dIts full is kept this long (never less than backup_retention)
backup_keep3The newest complete fulls of a database never expired while it exists
backup_expire_min_age7dNo chain younger than this is expired, by the schedule or by command
backup_chain_segments64The most stream segments a chain holds on a partition (2 to 1,024)
sealer_modedigestaead seals objects with the key sealer_primary_key names
sealer_primary_keynoneThe id of the key that seals, from the keys file
sealer_open_clearyesno: an object in the clear does not open (with aead)
mimicd admin settings set backup_destination=s3://acme-backups/mimic backup_full_interval=1d backup_interval=1h

settings set sets all its values or none, and refuses a combination that breaks a relation: backup_interval at most backup_full_interval, backup_retention at least backup_interval, the backups a chain takes (backup_full_interval / backup_interval) at most half of backup_chain_segments, and aead only with a primary key.

Destinations

  • A directory (file:///var/backups/mimic) keeps its objects and its .staging/ on one file system: no mount under its root.
  • An S3-compatible bucket: its credentials come from every process's environment (AWS_ACCESS_KEY_ID, an instance role, …), never from the URL. Every object is written once: the store must refuse a put over an object (If-None-Match: *), which each process checks as it opens the store by putting probe/<random> twice, then deleting it. Where the delete does not take (credentials that only put, object lock), the probes stay: expire probe/ with a lifecycle rule. Every put carries its CRC32, so a versioned bucket under object lock serves as any other.
  • A destination that refuses for good (access denied, no such bucket) fails the backup at once, logged as mimic_backup_destination_refused; what passes with time (a timeout, throttling, credentials refreshed late) is waited out.
  • Changing backup_destination sends the next full to the new one. Earlier chains stay where they are, and are read back and expire there. Nothing is copied.

The schedule

Every backup_full_interval a new chain starts with a full, and every backup_interval an increment is added to the open chain.

  • A failed full is taken again after backup_full_interval, a failed increment at the next backup_interval.
  • A backup that records no progress for an hour fails (mimic_backup_stalled). A large one runs as long as it moves on, but no longer than its history may be held (7 days, or twice backup_full_interval, whichever is longer): then it fails (mimic_backup_held_too_long) and its chain ends.
  • A chain closes once it holds backup_chain_segments stream segments on any partition, and the next run starts a full.
  • backup_full_interval=off ends the open chain within 10 minutes and releases its history holds.
  • No backup is taken of a database while it imports an archive; the schedule takes its first full once the import finished.

Retention and expiry

Chains expire by age: a scheduled chain's increments backup_retention after it started, its full backup_daily_retention after. A chain's expiry deletes its objects. Before anything is deleted, guards keep:

  • an open chain, one a backup runs in, and one a restore reads;
  • while the database exists, the chain of its newest complete backup, any chain younger than backup_expire_min_age, and its backup_keep newest complete fulls.

A removed database's chains are not deleted with it: they expire by age, and none younger than backup_expire_min_age is expired, so its backups outlive it. A backup is never deleted at will: there is no command to delete one, and an expiry asked by command goes through the same guards.

Backups by hand

mimicd admin backup list --database DB
mimicd admin backup show --database DB --backup ID
mimicd admin backup start --database DB --key KEY [--retention 30d]   # a full of its own, now
mimicd admin backup verify --database DB --backup ID --key KEY        # read it back again
mimicd admin backup expire --database DB --chain ID --retention 7d    # through the guards
mimicd admin backup status --key KEY
mimicd admin backup cancel --key KEY

Each takes the home's URL, --store NAME=URL for every other store, and --sealer.keys-file where backups are sealed. A start runs under a key you choose: asked again with the same key and arguments, it is the same operation. A command drives its work until it ends, or leaves it to a server process with --detach.

  • backup start takes a manual backup: a chain of its own, its full alone, kept --retention (1 h to 3,650 d; by default as a scheduled full).
  • An expiry its guards refuse answers resource_exhausted (exit code 4), naming the guard that keeps the chain: open_chain, running, restoring, backup_expire_min_age, newest_backup, backup_keep or retention. Ask again once what it judges has changed.

The API serves the same: GET, POST /v1/admin/databases/{db}/backups, GET …/backups/{backup}, POST …/backups/{backup}/verify and POST /v1/admin/databases/{db}/chains/{chain}/expire. Listing and reading take backups.read; starting, reading back and expiring take backups.write (the backup_operator role); cancelling is a superuser's. Each starts an operation read back at /v1/admin/operations/{key}.

Restoring

A restore makes a new database from any complete backup a chain keeps. The database the backup was taken from is not changed.

mimicd admin restore --from-database DB --backup ID --database NEW \
  --budget-group default --key restore-1 [--to-store NAME]

Over the API it is POST /v1/admin/databases/{new}/restore, a superuser's.

  • The new database has the source's partitions, each document's state, history and outcomes as the backup holds them. It goes on the active store holding the fewest partitions, or --to-store; it waits for room on no store.
  • No access data is restored: create the new database's keys and grants anew, so a key revoked since the backup cannot come back.
  • A transaction's outcome from before the backup is read by its id in the new database; the same request sent there again is refused transaction_id_reused, never applied twice.
  • The collection registry comes back as the backup holds it. A migration that was sealing comes back verifying (migration_unsealed_restored in the status): activate it again.
  • A restore to a moment between two backups is not built.

Sealing

With sealer_mode = aead, backups' objects are encrypted with XChaCha20-Poly1305. Every process (and every operator command that reads or writes backups) is given the same keys file, with --sealer.keys-file or MIMIC_SEALER_KEYS_FILE:

{ "keys": [{ "id": "2026-10", "key": "<64 hexadecimal digits>" }] }
mimicd admin settings set sealer_mode=aead sealer_primary_key=2026-10
  • Keep every key as long as a backup it sealed is kept, in a secret store with an offline copy: a backup whose key is lost does not open.
  • Rotating. Add a new key, make it the primary, and mark the old one "retiring": true. What the deployment keeps of its own secrets under the old key (webhook endpoints' secrets, ACME's keys) is sealed again under the primary, a batch a minute; backups' objects stay under it until their backups expire. mimicd admin status (sealed) and mimic_sealed_objects count what each key still keeps: drop it once it counts nothing.
  • Once nothing in the clear needs to open, set sealer_open_clear=no.

Moving a database to another deployment

A database moves to another deployment as an archive, taken under a fence of its home, so that only one deployment takes its writes.

Fence it

mimicd admin database fence --database DB --key fence-1

From then on no process commits a write to the database: writes are refused database_fenced (unavailable, so clients keep their writes and send them again), while reads, sessions' reads, exports and backups go on. The fence waits for a move, a schema migration or a collection's removal under way to end, and starts none.

Export it

mimicd archive export --database DB --to ./db.tar   # with the home's URL, and --store for every other store
mimicd archive verify ./db.tar

The export reads every partition under the fence (--parallel at once, 4 by default), records the archive's digest on the fence, and writes a Mimic Archive: a directory, or a tar file for a path ending in .tar. It keeps each document's state, its transaction ids (so a retry is answered at the target), and what its edit streams need; not its history.

Import it in the target

MIMIC_TARGET_PASSWORD=… mimicd cutover --archive ./db.tar --database DB \
  --target https://mimic-new.example.com --target-user admin --partitions 4 --require-fenced

See Importing archives.

Carry its access

mimicd admin access export --database DB --with-passwords > access.json   # in the source
mimicd admin access import --database DB < access.json                    # in the target

This carries the users granted on the database and their grants (password hashes only with --with-passwords), its keys by their digests (clients keep their secrets) and its token keys. The import is one transaction, and lists every conflict with nothing changed. The file holds digests and may hold hashes: keep it as a credential.

Switch, then remove or lift

Point clients at the target, take a backup there, then remove the database in the source. Webhook endpoints and triggers are not in the archive: make them again in the target. To call the move off before the target took any write, lift the fence: mimicd admin database unfence --database DB --key lift-1. Once an archive was exported under the fence, the lift is refused unless it says --after-abort: abort the target's import first (mimicd cutover … --abort), since lifting after an import finished makes two deployments take the database's writes.

Test your restores

Read backups back (backup verify), and restore into a scratch database regularly, checking the documents your application cares about. Recover a copy of a store from PostgreSQL's backups too.