Skip to content
MSO CloudDocumentation

Backup & Restore Runbook

On this page

Operational runbook for the automated Postgres DR backup on the MSO Cloud production host. This is the platform-rebuild path (whole database), NOT the per-tenant product archive (Super Admin → Data → "Sao lưu & Phục hồi", ADR 0011). For the wider recovery sequence (Redis, object store, cutover) see Disaster recovery.

A backup on the same host as the database is not disaster recovery. RustFS, Postgres and these dumps all share one failure domain. DR only exists once each dump is pushed OFF this box — see Off-host.

What runs#

Piece Path Job
Backup script scripts/ops/backup-postgres.sh pg_dump -Fc → integrity check → sha256 + manifest → retention → optional off-host push
Restore drill scripts/ops/verify-restore.sh restore latest dump into a throwaway container, row-count sanity, tear down
Scheduled sidecar infra/db-backup/ + docker-compose.backup.yml.example nightly cron (02:00 ICT) running the backup script in-cluster
Whole-deployment snapshot (manual) scripts/dr-snapshot.sh DB dump + full object-store bucket export (S3, mc mirror), for a one-shot pre-change safety snapshot

Production today runs the shared host cron instead of the sidecar. ubuntu's crontab (copy at /home/backup/crontab.ubuntu) runs at 03:00 ICT (host time zone +07): flock -n /tmp/bk-mso.lock bash /home/backup/backup_container_daily.sh /home/backup/env/msocloudnet.env. It runs pg_dump -Fc, then rsyncs the dump, the checkout and the msocloud_rustfs_data volume to the NAS (RETENTION_DAYS=7). It appends to /home/backup/logs/msocloudnet.log. Do not run the sidecar beside it.

scripts/ops/alert-check.sh checks web health, container state, NAS backup freshness (26 h) and disk use, and is meant to run from cron every 15 minutes (whether it is installed on this host is unverified). It reads MSO_BACKUP_LOG, default /home/backup/logs/msocloudnet.log, and alerts on a Permission denied, command not found or No such file or directory line in the last run as well as on a run that never logged its end marker. When the state changes it emails MSO_ALERT_EMAIL through Resend, and it repeats an alert after 6 h while the problem lasts. MSO_ALERT_DRY_RUN=1 prints the email instead of sending it.

Volumes on this host: msocloud_postgres19_data (the live Postgres 19 cluster; the pre-upgrade msocloud_postgres_data is kept as the rollback until sign-off, see data-plane-upgrade.md), msocloud_rustfs_data (object store, including org archives), msocloud_archive_staging (archive worker scratch, safe to lose).

The dump is custom format (pg_restore-restorable, selective, parallel) and carries user credentials, envelope-encrypted connector/AI secrets, the audit ledger and buyer PII. Encryption keys are NOT in the dump — a restore also needs the matching ENCRYPTION_KEY_V<n>; back key material up separately. Treat every dump as production-secret: owner-only, access-controlled, off-host.

RPO / RTO (honest, current)#

  • RPO = age of the last dump (nightly ⇒ up to ~24 h of data loss). Continuous WAL archiving is NOT configured, so sub-day RPO is not available today.
  • RTO ≈ dump size / restore throughput + cutover. Measured 2026-11-06: the 2026-10-31 production dump (1,195,549,138 B, sha256 OK) restored in 5 min 34 s into a 2 CPU / 2 GB drill container (240 public tables, RESULT=PASS). The live database is now 16 GB on disk; re-measure with the restore drill as data grows.
  • To improve RPO below a day: add WAL archiving / PITR (out of scope here) or raise the cron cadence and ensure each run pushes off-host.

Enable the nightly schedule (next deploy)#

The sidecar is opt-in and NOT started by start-prod.sh. To turn it on:

cp docker-compose.backup.yml.example docker-compose.backup.yml
# set BACKUP_OFFHOST_* in .env.prod first (see below), then:
docker compose -f docker-compose.prod.yml -f docker-compose.backup.yml \
  --profile backup up -d --build db-backup
docker logs -f msocloud-db-backup-prod    # structured key=value events per run

Relevant .env.prod knobs (all optional, sane defaults):

Var Default Meaning
BACKUP_PGHOST msocloud-postgres-prod Postgres container/DNS name on internal
BACKUP_KEEP_DAILY 14 dumps to retain; older are pruned each run (every limit with its source: Limits and budgets)
BACKUP_CRON 0 19 * * * 19:00 UTC = 02:00 ICT
BACKUP_INCLUDE_MINIO 0 1 also mc mirrors the object bucket
BACKUP_RUN_ON_START 0 1 fires one backup on container start
BACKUP_ALERT_WEBHOOK (unset) URL that receives a JSON alert on failure

Off-host: the part that makes it DR#

Set these in .env.prod (the sidecar joins egress so it can reach the internet):

BACKUP_OFFHOST_KIND=rclone            # rclone | aws | rsync
BACKUP_OFFHOST_TARGET=r2:msocloud-dr  # bucket/prefix, s3://…, or user@host:/path
BACKUP_OFFHOST_FLAGS=                 # extra flags passed to the tool

Until a target is set, every run logs event=backup.offhost_unconfigured … NOT_DISASTER_RECOVERY and the exit stays 0 (a backup still lands locally). Wire your storage creds to the chosen tool.

Existing host framework: this box already runs /home/backup/*.sh (rsync to a NAS at a separate site) via host cron. Two integration options: (a) point BACKUP_DIR's host volume into a path that NAS rsync already carries, or (b) use BACKUP_OFFHOST_KIND=rsync to push each dump to the NAS directly. Either way, confirm the dumps actually leave the box — check the NAS, not just local disk.

Run a manual backup (host, ad-hoc)#

pg_dump is read-only and safe against production.

BACKUP_DIR=/secure/backups/msocloud ./scripts/ops/backup-postgres.sh
# exec mode (docker exec into msocloud-postgres-prod) is auto-selected on the host

Run a restore drill (proves the backup is restorable)#

Fully isolated — a throwaway container, no published port, torn down after. Safe on the production host; it never touches prod.

BACKUP_DIR=/secure/backups/msocloud ./scripts/ops/verify-restore.sh
# → RESULT=PASS backup is restorable dump=yng_mso-….dump

The drill first checks the dump against its .sha256 sidecar and stops with RESULT=FAIL … stage=checksum on a mismatch; REQUIRE_CHECKSUM=1 also refuses a dump with no sidecar. backup-postgres.sh writes <dump>.partial and renames it only after pg_restore --list passes, so a crash mid-dump never leaves a .dump that retention or the drill would pick as newest.

Do this on a cadence (weekly is reasonable) and after any schema-shape change. A backup you have never restored is a hypothesis, not a backup.

Per-org archives: interruption and resume#

The per-org backup and restore jobs (Super Admin → Data, ADR 0011) run in the archive-worker container. A restart, deploy or OOM no longer turns a running job into a failure:

  1. On SIGTERM the worker kills its psql/tar children at once (the open transaction rolls back) and leaves the job running. Log: org-backup interrupted by shutdown; resumes on next boot / org-restore interrupted by shutdown; resumes on next boot.
  2. On boot, before it takes any job, the worker removes leftover staging directories and requeues every running job through the job outbox. Log: archive jobs requeued or settled after restart with the count. The screen shows the job as queued with "The worker restarted while this job was running; it resumes automatically."
  3. The resumed backup exports again into the same object key; if the previous run had already uploaded and verified its artifact (progress.stage = 'uploaded'), it only re-verifies the object and completes (org-backup resumed from its upload checkpoint). A restore re-runs its single transaction; a completed safety snapshot is reused.
  4. After 3 attempts a job fails instead ("The worker restarted during each of 3 attempts…"); request it again once the cause (usually memory) is fixed.

A backup completes only after the stored object is read back and its length and SHA-256 match what was exported. Safety snapshots are never pruned until the operator sets both ORG_SAFETY_SNAPSHOT_KEEP and ORG_SAFETY_SNAPSHOT_MAX_AGE_DAYS on the archive worker; then a same-org restore keeps that org's newest KEEP snapshots and deletes older ones past MAX_AGE_DAYS (proposal: 3 and 30).

A native backup taken before later migrations restores forward: the worker checks the archive's columns against the current tables before writing. An archive from a schema this database never applied is refused with "SCHEMA MISMATCH … Deploy this environment to at least the archive's schema, then request the restore again."

Real recovery (host/data-plane lost)#

  1. Provision Postgres of the major the dump was taken on (postgres:19.0; a 19 dump does not restore into 16).
  2. createdb yng_mso then pg_restore --no-owner --no-privileges -d yng_mso <dump>.
  3. Supply the matching ENCRYPTION_KEY_V<n> in .env.prod (NOT in the dump) or every connector/AI secret decrypts to garbage.
  4. Restore RustFS objects (mc mirror the scripts/dr-snapshot.sh bucket export back into the bucket, or your object store) and Redis if queue state matters.
  5. Follow the cutover + smoke sequence in Disaster recovery.

Residual risk / not yet built#

  • Off-host push is operator-wired, not on by default — same-host-only until set.
  • No continuous WAL/PITR — RPO is the dump interval, not minutes.
  • The scheduled sidecar activates on the next deploy; it is intentionally not started retroactively.