23 — Backup and restore drill (dev-first, then production)
Version: 1.10
Last Updated: 2026-08-05 17:38:42
Maps to: 04-ROADMAP §1 A5 · 07-PLATFORM-RUNBOOK §15
Canonical procedure for proving LynCareOS can recover PostgreSQL data. Always rehearse on dev first; only then run the non-destructive VPS test-restore. Full production overwrite is documented as a last resort.
Promotion pipeline (dev → Docker UAT → production VPS)
Section titled “Promotion pipeline (dev → Docker UAT → production VPS)”Matches 20-PRODUCTION-DEPLOYMENT-GUIDE §2a. Code and migrations move together at Docker and VPS tiers.
| Step | Environment | Database | Code | Agent automation |
|---|---|---|---|---|
| 1 — Dev | Native :5432 | lyncareos_dev | Vite + API :8021 (owner tests here first) | migrate.ps1 -Target Dev · audit PENDING: none |
| 2 — Docker UAT | Compose production profile | lyncareos :5433 | docker compose --profile production up -d --build → :8020 | Same in push-to-production.ps1 · gate FailOnPending |
| 3 — VPS | DO Sydney | lyncareos | deploy.ps1 (Docker Hub image) | Full backup → deploy → VPS audit PENDING: none |
Rule: The product owner does not run backup scripts. AI agents use push-to-production.ps1 or deploy.ps1 — both paths run a full database backup on the VPS before any production schema/app change. Failed-deploy recovery: apply-vps-pending-migrations.ps1 (backup first). See doc 20 §2b.
Production backup copies (three tiers)
Section titled “Production backup copies (three tiers)”PostgreSQL and uploads are separate. At current scale, tier 1 + tier 2 are automated; tier 3 (B2) is deferred until traffic warrants geo-redundant cloud copy.
| Tier | Where | What | How | Failure policy |
|---|---|---|---|---|
| 1 — VPS | lyncareos-prod-syd1 /var/backups/lyncareos/ | Latest pg_dump -Fc of lyncareos | .\scripts\vps-production-backup.ps1 (or deploy.ps1 pre-deploy dump) | Required — script exits non-zero if tier 1 fails |
| 2 — Workstation (this PC) | backups\ (gitignored) | Off-server copy if VPS is lost | Same script, tier 2; wrapper pull-vps-backup-to-workstation.ps1 | Best-effort — if this PC is offline, run still passes; next run retries scp |
| 3 — Cloud (B2) | Backblaze B2 + rclone | Geo-redundant | Deferred (P2) — 99-ARCHIVE/25… | Not a launch blocker at current scale |
Canonical script — vps-production-backup.ps1
Section titled “Canonical script — vps-production-backup.ps1”Tier 1 (required): pg_dump -Fc on VPS to /var/backups/lyncareos/lyncareos_vps_<timestamp>.dump (or reuse -RemoteDumpPath after deploy).
Tier 2 (best-effort): scp to backups\ when this workstation can reach the VPS. Unreachable PC is not a crisis — log pass_tier1_only, retry on the next scheduled run.
# From repo root — fresh dump + pull (default).\scripts\vps-production-backup.ps1
# After deploy: copy existing pre-deploy dump only (no second pg_dump).\scripts\vps-production-backup.ps1 -RemoteDumpPath "/var/backups/lyncareos/pre-deploy-deploy-20260602-1110.dump"
# Strict: fail if workstation copy cannot complete.\scripts\vps-production-backup.ps1 -RequireWorkstation
# VPS dump only (skip tier 2).\scripts\vps-production-backup.ps1 -SkipWorkstationWrapper (same behaviour): .\scripts\pull-vps-backup-to-workstation.ps1
Log: backups/backup-run-log.jsonl (tier1, tier2, result: pass | pass_tier1_only).
Full backup (not partial): pg_dump -Fc with no table filter — entire lyncareos database.
Retention (10 + 10): Newest 10 production dump files on VPS /var/backups/lyncareos/ and 10 on this workstation (lyncareos_vps_*.dump, pre-deploy*.dump, vps_*.dump). Older files are deleted automatically. Dev-only dumps in backups\ (e.g. lyncareos_dev_*.dump) are not pruned by this script.
Verified 2026-06-02: lyncareos_vps_20260602_180702.dump — VPS + workstation (~532 KB).
Suggested schedule (Windows Task Scheduler)
Section titled “Suggested schedule (Windows Task Scheduler)”| Trigger | Command |
|---|---|
| Weekly (e.g. Sunday 02:00) | pwsh -NoProfile -File D:\Projects\LynCareOS\scripts\vps-production-backup.ps1 |
| Each deploy | Built into deploy.ps1 post-step (-RemoteDumpPath pre-deploy dump) |
Run only when this PC is powered on and can SSH to the VPS; missed weeks are OK — tier 1 remains on the VPS.
Restore from workstation copy (VPS lost or destructive mistake):
# Copy dump to a machine that can reach Postgres, then Phase 3 restore procedure# using pg_restore — see Phase 3 below and restore-pgdump-to-local-uat.ps1 for local Docker rehearsal.Refresh native dev from production (comparative testing — not a drill):
.\scripts\dev-stop.ps1.\scripts\restore-vps-production-to-dev.ps1 -BackupDevFirst # or -UseLatestLocal -NonInteractive.\scripts\dev-start.ps1See 11-DATABASE-SAFETY §2o. Contains real PHI — secured dev PC only.
Uploads volume (lyncare_uploads) — not in pg_dump. For full host recovery, also archive /var/lib/docker/volumes/ (or your VPS uploads bind path) — document host path in 07 §15.1.
Principles (fail-safes)
Section titled “Principles (fail-safes)”| Rule | Why |
|---|---|
| Name the target before every command | lyncareos_dev (native :5432) ≠ lyncareos (VPS Docker) ≠ local Docker :5433 |
| Never restore over production during a drill | Use a side database (lyncareos_restore_test) |
| Backup immediately before migrations or destructive work | Roll-forward path if restore fails |
| Custom-format dumps for drills | pg_dump -Fc + pg_restore — faster verify than plain SQL |
| Keep the dump file until the drill passes | Evidence for A5 sign-off |
| Uploads are separate | Postgres restore does not restore /app/uploads — copy lyncare_uploads volume when moving hosts (01-ARCHITECTURE) |
Phase 0 — Migrations on both tiers (before drill)
Section titled “Phase 0 — Migrations on both tiers (before drill)”Apply schema changes before trusting a backup for launch sign-off.
| Tier | Database | How to audit | How to apply |
|---|---|---|---|
| Dev | lyncareos_dev @ localhost:5432 | .\migration-audit.ps1 -Target Dev | .\migrate.ps1 -Target Dev |
| Production (VPS) | lyncareos in lyncareos-postgres-1 | .\migration-audit.ps1 -Target Vps -NonInteractive | Normally .\scripts\deploy.ps1 (runner on app start). Manual only if deploy failed mid-migration — 07 §10 |
Pass criteria: PENDING: none on Dev and Vps audits. Latest repo file should match highest schema_migrations.version (currently V104).
Not production: .\migrate.ps1 -Target Production applies to local Docker localhost:5433/lyncareos only.
Phase 1 — Dev drill (mandatory rehearsal)
Section titled “Phase 1 — Dev drill (mandatory rehearsal)”Target: native Windows PostgreSQL lyncareos_dev.
Script: .\scripts\backup-restore-drill-dev.ps1 (automates checklist below).
Native dev and RLS (V034)
Section titled “Native dev and RLS (V034)”On native Windows Postgres, FORCE ROW LEVEL SECURITY blocks pg_dump for the app role. backup-restore-drill-dev.ps1 handles this automatically: as table owner, it sets NO FORCE on affected tables for the duration of pg_dump, then restores FORCE in a finally block (source DB security posture unchanged after the drill).
Optional fallback (if the script cannot alter tables): one-time BYPASSRLS via superuser. Add to backend/.env (gitignored):
LYNCARE_PG_SUPER_PASSWORD=<Windows postgres superuser password>Then:
.\scripts\dev-postgres-super-check.ps1 # verify auth + show BYPASSRLS flags.\scripts\grant-dev-backup-role.ps1 # or: dev-postgres-super-check.ps1 -GrantVPS Docker Postgres does not need this workaround for routine dumps (verified on lyncareos-prod-syd1).
Manual checklist (if not using script)
Section titled “Manual checklist (if not using script)”| Step | Action | Pass? |
|---|---|---|
| D1 | .\db-backup.ps1 -Target Dev or custom dump below | |
| D2 | Create lyncareos_restore_test on same server | |
| D3 | pg_restore into test DB | |
| D4 | Compare users / tenants counts: lyncareos_dev vs test | |
| D5 | Spot-check essentialqa user exists in test (if seeded) | |
| D6 | DROP DATABASE lyncareos_restore_test | |
| D7 | Record pass in backups/drill-log.jsonl |
Commands (native Postgres, PowerShell)
Section titled “Commands (native Postgres, PowerShell)”# From repo root — custom format (preferred for drill)$ts = Get-Date -Format "yyyyMMdd_HHmmss"$dump = "backups\lyncareos_dev_$ts.dump"$env:PGPASSWORD = "lyncare"pg_dump -h localhost -p 5432 -U lyncare -d lyncareos_dev -Fc -f $dump
# Side database (non-destructive)psql -h localhost -p 5432 -U lyncare -d postgres -c "DROP DATABASE IF EXISTS lyncareos_restore_test;"psql -h localhost -p 5432 -U lyncare -d postgres -c "CREATE DATABASE lyncareos_restore_test OWNER lyncare;"
pg_restore --no-owner -h localhost -p 5432 -U lyncare -d lyncareos_restore_test $dump
# Verify countspsql -h localhost -p 5432 -U lyncare -d lyncareos_dev -c "SELECT 'dev' AS db, (SELECT COUNT(*) FROM users) AS users, (SELECT COUNT(*) FROM tenants) AS tenants;"psql -h localhost -p 5432 -U lyncare -d lyncareos_restore_test -c "SELECT 'test' AS db, (SELECT COUNT(*) FROM users) AS users, (SELECT COUNT(*) FROM tenants) AS tenants;"
# Cleanuppsql -h localhost -p 5432 -U lyncare -d postgres -c "DROP DATABASE lyncareos_restore_test;"$env:PGPASSWORD = ""Abort / recovery: If restore fails, production is untouched. Dev data in lyncareos_dev is unchanged. Fix pg_restore errors, or restore dev from the .dump only after typing yes to a deliberate overwrite (db-restore.ps1 is destructive — use only when you intend to replace lyncareos_dev).
Phase 2 — VPS drill (A5 sign-off, non-destructive)
Section titled “Phase 2 — VPS drill (A5 sign-off, non-destructive)”Run only after Phase 1 passes. Full command list: 07-PLATFORM-RUNBOOK §15.3.
| Step | Action |
|---|---|
| V1 | SSH root@134.199.173.206 (key ~/.ssh/lyncareos_vps) |
| V2 | Custom dump on VPS (pg_dump -Fc to /tmp/) |
| V3 | Restore into lyncareos_restore_test |
| V4 | Compare counts vs lyncareos |
| V5 | Drop test DB |
| V6 | Optional: scp dump to workstation backups\vps_*.dump |
| V7 | Update 04-ROADMAP A5 row with date + filename |
Phase 3 — Production disaster restore (destructive)
Section titled “Phase 3 — Production disaster restore (destructive)”Destructive — overwrites live lyncareos. Brief app outage while app and scan_worker are stopped.
Automated (from workstation):
.\scripts\backup-restore-drill-vps-production.ps1 -IUnderstandSteps: pre-counts → fresh pg_dump in container → docker cp to host + scp to backups\ → stop app → drop/recreate lyncareos → pg_restore → start app → HTTP 200 on /api/billing/tiers.
Executed on production VPS 2026-06-02: pass — 9 users / 4 tenants before and after; dump lyncareos_prod_drill_20260602_175454.dump; local copy backups/vps_lyncareos_prod_drill_20260602_175454.dump; VPS curl /api/billing/tiers → 200.
Manual checklist:
- Stop
appandscan_workeron VPS. DROP DATABASE lyncareos;thenCREATE DATABASE lyncareos OWNER lyncare;(separatepsqlcommands).pg_restoreintolyncareos(dump path is insidelyncareos-postgres-1, e.g./tmp/….dump).- Restart stack; verify
/api/billing/tiersand login. - Confirm
lyncare_uploadsvolume intact or restore files from separate backup.
Commands: 07 §15.3 — destructive block.
Scripts reference
Section titled “Scripts reference”| Script | Purpose |
|---|---|
migration-audit.ps1 -Target Dev | Vps | Read-only pending/orphan report |
migrate.ps1 -Target Dev | Apply pending migrations to dev |
db-backup.ps1 -Target Dev | Production | Plain SQL dump (adhoc) |
scripts/grant-dev-backup-role.ps1 | One-time BYPASSRLS on native dev lyncare (needs LYNCARE_PG_SUPER_PASSWORD) |
scripts/backup-restore-drill-dev.ps1 | Automated Phase 1 dev drill |
scripts/backup-restore-drill-vps.ps1 | Automated Phase 2 VPS drill (requires dev pass unless -SkipDevCheck) |
scripts/backup-restore-drill-vps-production.ps1 | Phase 3 destructive restore over live lyncareos (-IUnderstand) |
scripts/vps-production-backup.ps1 | Tier 1 required + tier 2 best-effort; log backup-run-log.jsonl |
scripts/pull-vps-backup-to-workstation.ps1 | Wrapper → vps-production-backup.ps1 |
scripts/deploy.ps1 | VPS deploy + pre-deploy dump + post-deploy workstation pull (best-effort) |
Drill log
Section titled “Drill log”Append one JSON line per run to backups/drill-log.jsonl. A5 production sign-off requires a phase":"vps" pass; Phase 1 dev is mandatory rehearsal on the workstation (same script flow, different RLS prerequisites).
2026-06-02 — A5 signed off (roadmap v3.5):
| Phase | Result | Evidence |
|---|---|---|
| Migrations Dev + Vps | pass | V104, PENDING: none |
| Phase 1 — Dev drill | pass | backups/lyncareos_dev_20260602_173528.dump — 9 users / 5 tenants |
| Phase 2 — VPS side DB | pass | /tmp/lyncareos_20260602_173547.dump — 9 users / 4 tenants |
| Phase 3 — Destructive prod restore | pass | lyncareos_prod_drill_20260602_175454.dump — 9/4 before & after; /api/billing/tiers 200 |
| Tier 2 — Workstation (best-effort) | automated + verified | vps-production-backup.ps1; lyncareos_vps_20260602_180702.dump — log backup-run-log.jsonl |
| Tier 3 — Cloud B2 | deferred (P2) | At scale — roadmap A8; not required for A5/A5b sign-off |
Re-run Phase 1–2 quarterly or before major migrations. Full log: backups/drill-log.jsonl.