Office docsRun it

Back up and restore

An installation backs itself up, encrypted and on a schedule, to an S3-compatible object store. Backup is off until you configure a destination.

IT admins15 min

Configure backups before the first install

The backup profile and key location are fixed when the installation is created. An installation without backups cannot be upgraded in place, because an upgrade takes a backup first.

What you need

  • An S3-compatible bucket that supports path-style addressing, with credentials that can read, write and delete in it.
  • HTTPS on the endpoint, and the CA file that signs its certificate. The installation pins that CA and never falls back to the system trust store.
  • A directory for the encryption key on a different filesystem from the installation's data.

Set the destination

Set all of these before you run install.sh, or none. The installer reads them from the environment and writes them into .env, so restarts mount the same files. A partial configuration fails at the first backup, a day later.

SettingValue
KW_BACKUP_S3_ENDPOINTThe endpoint URL. It must use https and end in /.
KW_BACKUP_S3_BUCKETA DNS-style bucket name, such as office-backups.
KW_BACKUP_S3_REGIONLowercase letters, digits and hyphens, up to 32 characters, such as us-east-1.
KW_BACKUP_S3_CREDENTIALS_HOST_FILEThe credentials file on the host.
KW_BACKUP_S3_CREDENTIALS_FILEWhere that file is mounted inside the container.
KW_BACKUP_S3_CA_HOST_FILEThe endpoint's CA file on the host.
KW_BACKUP_S3_CA_FILEWhere the CA file is mounted inside the container.
KW_KEY_CUSTODIANWhere the encryption key is kept, inside the container. See Keep the key off the data volume.
bash
$ export KW_BACKUP_S3_ENDPOINT=https://objectstore.example.com:9000/
$ export KW_BACKUP_S3_BUCKET=office-backups
$ export KW_BACKUP_S3_REGION=us-east-1
$ export KW_BACKUP_S3_CREDENTIALS_HOST_FILE=/secure/backup-credentials.json
$ export KW_BACKUP_S3_CREDENTIALS_FILE=/run/kernalworks/backup-credentials.json
$ export KW_BACKUP_S3_CA_HOST_FILE=/secure/objectstore-ca.pem
$ export KW_BACKUP_S3_CA_FILE=/run/kernalworks/objectstore-ca.pem
$ export KW_KEY_CUSTODIAN=/srv/custodian-host/keys
$ ./install.sh --tailscale --admin you@example.com

The credentials file

The file holds exactly three keys. sessionToken may be null but must be present.

json
{ "accessKeyId": "…", "secretAccessKey": "…", "sessionToken": null }

Set its mode to 0600 and its owner to uid 1000, the user the container runs as. The installation refuses the file otherwise, with office_backup_secret_unsafe.

bash
$ chmod 600 /secure/backup-credentials.json
$ chown 1000:1000 /secure/backup-credentials.json

Keep the key off the data volume

Backups are encrypted under a key the installation creates. It refuses a key location on the same filesystem as its data, because a key stored beside the data is lost with it. Removable or remote media is the intent.

Two Docker named volumes do not satisfy this, because they share one filesystem. Bind-mount a directory from a different filesystem into the office container, and point KW_KEY_CUSTODIAN at a subdirectory inside it. The container creates that subdirectory with mode 0700.

yaml
# compose.yaml, in the office service's volumes
- /mnt/backup-keys:/srv/custodian-host

The installer does not add this mount for you. Add it to the office service in compose.yaml before the first install.

Keep a second copy of the key

Losing the key loses every backup. Show the recovery phrase again at any time, and keep a second custodian copy on other media.

bash
$ docker compose -p kernalworks-office -f compose.yaml exec office node /opt/kernalworks/operations/src/office.mjs export-recovery-phrase --config /srv/kernalworks/data/office-installation.json
$ docker compose -p kernalworks-office -f compose.yaml exec office node /opt/kernalworks/operations/src/office.mjs add-custodian-copy --config /srv/kernalworks/data/office-installation.json --to /media/usb2/backup-key.json

Choose snapshot or continuous

Snapshot (default)Continuous
What it isA logical dump (pg_dump)A copy of the database cluster plus its WAL (pg_basebackup)
Recovery pointThe last backupA chosen moment, with point-in-time recovery
Crosses a major PostgreSQL versionYesNo
Restored intoA running clusterAn empty data directory

Neither kind takes the installation down while it runs.

Retention

Retention is declared in the installation manifest as operations.backup.retention, a number of days. Old backups are pruned on the schedule, oldest first, within limits you cannot turn off.

  • Three backups always survive, whatever the window.
  • A run deletes at most eight backups.
  • The newest backup and the last verified one are never deleted.
  • An incomplete backup is left alone for 48 hours.

If your bucket enforces object locking, the installation does not prune and says so. Retention is then the bucket's policy.

Run a backup now

Backups run on a schedule. Run one by hand to test the destination or to see the detail of a failure.

bash
$ docker compose -p kernalworks-office -f compose.yaml exec office node /opt/kernalworks/operations/src/office.mjs backup --config /srv/kernalworks/data/office-installation.json

At startup the log lists the scheduled jobs. If it shows not scheduled: backup, the installation was created without a destination.

Restore from a backup

An installation cannot restore over itself, and its image has no PostgreSQL server. Restore on a host that has PostgreSQL server binaries, into a separate cluster or data directory. kw-office is the operations command. Inside the office container it runs as node /opt/kernalworks/operations/src/office.mjs.

# Into a second cluster with the same database name and server version
$ kw-office restore --config <manifest> --backup-id <id> --target-config <connection-file>

A restore changes the installation's recovery epoch, which ends every session. People sign in again afterwards.

For a continuous backup, the operation reports verified: false on purpose. Only starting PostgreSQL on the files shows whether they hold your data.

Check that backups restore

Nothing verifies your backups automatically, because verifying means restoring into a second PostgreSQL cluster, which the installation does not have. Until you restore one, none of your backups has been proved to restore.

Restore into a throwaway cluster and compare every table's row count with the source. Do this about once a month. The installation's recovery health warns at 30 days and turns critical at 90.

When a backup fails

ErrorCause
office_backup_key_custodian_on_data_volumeThe key location shares a filesystem with the data. See Keep the key off the data volume.
office_backup_secret_unsafeThe credentials file is readable by group or others, or not owned by uid 1000.
office_backup_secret_unavailableThe credentials file is not mounted where the settings say.
root certificate file … does not existPGSSLROOTCERT is unset, so the PostgreSQL clients cannot verify the database.
permission denied for schema …The backup role lacks pg_read_all_data.
office_recovery_command_failedA PostgreSQL client program failed. The error names the command and carries its redacted output.

The database files on the data volume are not encrypted by the installation. Encrypt the disk on the host. See Secrets and rotation.