Skip to content

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.

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.

  1. Turn maintenance on and deploy. Check that POST /v1/exchange returns 503.

  2. 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 restore audit row.

      Terminal window
      bunx --no-install portona-restore backups/2026-11-28.json > restore.json
      bunx --no-install cf d1 query <database-id> --batch @restore.json

      portona-restore first 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 DB at it.

    • Roll back with Time Travel (cf d1 time-travel restore), which rolls back every table.

  3. 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.

  4. Check the keys. The Worker decrypts rows under BROKER_MASTER_KEY and BROKER_MASTER_KEY_PREVIOUS only. 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.

  5. Check every row (see below), and compare it with a check saved before the recovery, if you have one.

  6. Turn maintenance off and deploy.

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, or undecryptable if 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, or differs (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.