Backups and recovery
A daily cron writes one snapshot of the database, still encrypted, to the BACKUPS bucket as
backups/YYYY-MM-DD.json, and deletes backups older than 90 days. It also prunes audit rows and
expired token records. D1 Time Travel keeps a further 30 days of history.
Since v0.6.4 the snapshot is format 2. It’s read in one transaction, and holds:
- the environment ledger, with which environments read data keys;
- every master-key secret row, with its details;
- each environment’s data keys, still wrapped, and the secrets under them;
- every federation;
- the current master key’s id, and the ids of the wrapping keys the data keys need.
Backups from earlier releases (format 1) still restore.
Until the M3 cutover the master-key rows stay complete, so a restore works even without a wrapping key: an exchange whose data-keyed copy can’t be read reads the master-key row instead.
Maintenance
Section titled “Maintenance”With PORTONA_MAINTENANCE: bindings.text("on") in the deployment’s config, the Worker refuses
exchanges (503) and admin changes, and writes nothing to D1 for them. The admin page shows a banner.
The daily cron does nothing either, so it can’t replace the day’s backup with a copy of a database
mid-recovery, or record the configured environments ahead of the ledger being restored.
It’s a binding rather than a database row, so restoring or rolling back the database can’t turn it
off. Remove it and deploy to reopen.
Recovering
Section titled “Recovering”-
Turn maintenance on and deploy. Check that
POST /v1/exchangereturns 503. -
Recover, in one of three ways:
-
Restore a backup in place, to undo changes to some secrets. Each backed-up secret and federation replaces the row of the same environment and name; other rows, and the record of used tokens, are kept. The ledger only fills gaps, so its current rows win, and secrets and federations of a name retired since they were written are skipped. Data keys also fill gaps only. A data-keyed copy comes back only beside the data key it was encrypted with, and never for an environment ID the ledger has retired. A copy left out is copied again by the next backfill. The batch runs as one transaction and adds a
restoreaudit row.Terminal window bunx --no-install portona-restore backups/2026-11-28.json > restore.jsonbunx --no-install cf d1 query <database-id> --batch @restore.jsonportona-restorefirst prints the backup’s format and the master and wrapping key ids its rows need. Check that the Worker has each of them before running the batch. -
Restore into a fresh database, to get exactly the backup’s rows: create a D1 database, apply the migrations, run the same two commands against it, then point
DBat it. -
Roll back with Time Travel (
cf d1 time-travel restore), which rolls back every table.
-
-
Keep maintenance on for 61 minutes (the longest token lifetime plus clock skew) after a fresh database or a Time Travel rollback. Both lose the record of which tokens were already used, so a token used before the recovery could otherwise be replayed until it expires. An in-place restore keeps that record and can reopen once checked.
-
Check the keys. The Worker decrypts rows under
BROKER_MASTER_KEYandBROKER_MASTER_KEY_PREVIOUSonly. On the admin page, Keys lists the master key ids in use. If a restore brought back rows under an archived key, set the two keys to match, re-encrypt (see Rotating the master key), and confirm no rows remain on the older key before switching keys again. If the Worker lost its wrapping key, put back the one its escrow names (see Wrapping key escrow), never a new one. Data keys under another wrapping key don’t stop exchanges: they read the master-key rows. -
Check every row (see below), and compare it with a check saved before the recovery, if you have one.
-
Turn maintenance off and deploy.
Checking every row
Section titled “Checking every row”With the wrapping key set, /admin/api/verify checks every secret row by row. Open it in your
browser while signed in to the admin page; it answers in maintenance too, since it changes nothing.
Inside the Worker it decrypts each master-key row and its data-keyed copy, and for each row reports:
row:ok, orundecryptableif the Worker’s master keys can’t decrypt it;copy:ok,none,stale(a copy of an earlier write, which the next backfill replaces),undecryptable, ordiffers(it decrypts to another value than its row);digest: an HMAC of the environment, name and value, under a key derived from the wrapping key.
It never shows values. The same value gives the same digest on any Worker with the same wrapping
key, whatever its master key, so save the JSON before a recovery, or a master key rotation, and
compare the digests row by row afterwards. summary counts each outcome, and orphanCopies counts
copies with no row of a live environment to belong to.