Skip to content

Latest commit

 

History

History
125 lines (103 loc) · 5.79 KB

backup.md

File metadata and controls

125 lines (103 loc) · 5.79 KB

Backups and restores

Backups are optional (backup.enabled=false by default). The Helm chart can schedule PostgreSQL custom-format dumps to a persistent volume or an S3 compatible store. The scripts in scripts/backup/ also create dumps, copy them off the cluster, restore them, and run restore drills through the same CronJob. They use plain kubectl, with no deployment-directory dependency.

Enable backups

backup:
  enabled: true
  schedule: "0 2 * * *"
  keep: 14
  keepPreUpgrade: 5
  target: pvc
  pvc:
    size: 40Gi
    storageClass: ""

See helm/optio/values.yaml for S3 endpoint, bucket, credentials, prefix, deadline and resource options. The chart reads the API's database URL, including existingSecrets references, and mounts the built-in database's TLS CA when that is on.

A dump includes work, settings, users, encrypted credentials and migration history. It does not include repository PVCs, Redis, agent worktrees or external services. Each completed dump has a SHA-256 sidecar; the job checks it with pg_restore --list before keeping it. Partial dumps are discarded. Scheduled and manual dumps have separate retention from pre-upgrade dumps. PVC creation uses a retained hook so a Helm uninstall does not delete your backup data. Keep a copy outside the cluster and test that it restores.

Create and copy dumps

scripts/backup/backup-db.sh --namespace optio
scripts/backup/backup-db.sh --namespace optio --reason pre-upgrade --to /secure/backups
scripts/backup/backup-db.sh --namespace optio --download latest --to /secure/backups
scripts/backup/restore-db.sh --namespace optio --list

--namespace defaults to the selected kube context's namespace, then default. --release defaults to the namespace; pass it when your Helm release has a different name. --kubeconfig and --context select kubectl configuration; KUBECONFIG works normally. Set KUBECTL to select an executable.

--to and --download work with PVC backups. A temporary copy job mounts the volume, transfers bytes with kubectl exec, verifies checksums and exits. A heartbeat keeps it alive during long copies. Downloaded dumps and checksums are created with mode 0600 inside a private temporary directory before being published. Newly created destination directories use mode 0700, regardless of the caller's umask.

For S3 targets, copy dumps and checksums with your S3 tools. Exit status 3 from backup-db means the namespace has no backup CronJob; 1 means the backup or copy failed.

Restore and drill

# Restore into a disposable database, verify the key, then remove that database:
scripts/backup/restore-db.sh --namespace optio latest --into-database optio_drill
# --keep retains the drill database for inspection.

# Replace the live database (asks for confirmation):
scripts/backup/restore-db.sh --namespace optio latest
# Or restore a copy transferred from another installation:
scripts/backup/restore-db.sh --namespace optio /secure/backups/optio-20261024T021500Z.dump

A live restore stops the API and records its replicas on the Deployment. When the database account may create databases, the restore first builds a new database, swaps names and keeps the previous one as <db>_before_<time>. Otherwise it restores in one transaction. A failure starts the API on the unchanged database. A successful restore starts it at the recorded replica count, runs its migrations, verifies that the encryption key opens stored credentials (apps/api/scripts/check-encryption-key.ts, run in the API pod) and records the restore in the action log as database.restored, with the dump's name and the kept database. --yes skips the confirmation for controlled automation.

Each restore or drill Job contains its own dump and target database values; another invocation cannot replace a Pending Job's inputs. A live restore atomically acquires <release>-backup-live-restore before stopping the API. Other live restores are refused until it finishes. If the operator stops or times out, the lock remains with the named Job; use --finish for that Job to recover it. A drill does not acquire or delete a live restore's lock.

If a restore times out or the operator stops the script while its job may still be changing data, the API stays stopped. Follow the printed command:

scripts/backup/restore-db.sh --namespace optio --finish optio-backup-restore-1024101522

It waits for the existing job, then brings the API back and verifies the key. Do not manually start the API while a restore job is still running. Use the script's --timeout for slow restores. Drills only create and drop their own database; a pre-existing database is refused.

Before restoring a pre-upgrade dump, roll the application back to a compatible version using helm rollback, then restore the dump. For another cluster, deploy with the original encryption key and backups enabled before restoring. Never replace the encryption key in place: losing it makes restored encrypted credentials unreadable. To change it, follow Rotating the encryption key, which re-seals the stored values; dumps made before a rotation still need the old key. Keep it separately from the database dumps, in your normal secret backup process.

Verification and troubleshooting

Watch kubectl -n optio get cronjobs,jobs and job logs. Check storage capacity, PVC access modes, S3 connectivity, database permissions and the key if a job fails. A missing or damaged checksum is refused; a failed dump is not promoted to a completed backup. For an external backup system, use pg_dump -Fc and retain the encryption key under the same policy.

The script tests need no cluster: bash scripts/backup/test-backup-scripts.sh. The job's PostgreSQL test uses throwaway databases and roles: sh scripts/backup/test-backup.sh --url <superuser-postgres-url>.