Skip to content

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.

StepEnvironmentDatabaseCodeAgent automation
1 — DevNative :5432lyncareos_devVite + API :8021 (owner tests here first)migrate.ps1 -Target Dev · audit PENDING: none
2 — Docker UATCompose production profilelyncareos :5433docker compose --profile production up -d --build → :8020Same in push-to-production.ps1 · gate FailOnPending
3 — VPSDO Sydneylyncareosdeploy.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.

deploy.ps1 + full pg_dump

lyncareos_dev

Docker UAT lyncareos

VPS lyncareos


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.

TierWhereWhatHowFailure policy
1 — VPSlyncareos-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 lostSame script, tier 2; wrapper pull-vps-backup-to-workstation.ps1Best-effort — if this PC is offline, run still passes; next run retries scp
3 — Cloud (B2)Backblaze B2 + rcloneGeo-redundantDeferred (P2) — 99-ARCHIVE/25…Not a launch blocker at current scale

at scale

VPS Postgres lyncareos

VPS /tmp dump

Workstation backups/

Offsite B2

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.

Terminal window
# 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 -SkipWorkstation

Wrapper (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)”
TriggerCommand
Weekly (e.g. Sunday 02:00)pwsh -NoProfile -File D:\Projects\LynCareOS\scripts\vps-production-backup.ps1
Each deployBuilt 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):

Terminal window
# 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):

Terminal window
.\scripts\dev-stop.ps1
.\scripts\restore-vps-production-to-dev.ps1 -BackupDevFirst # or -UseLatestLocal -NonInteractive
.\scripts\dev-start.ps1

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


RuleWhy
Name the target before every commandlyncareos_dev (native :5432) ≠ lyncareos (VPS Docker) ≠ local Docker :5433
Never restore over production during a drillUse a side database (lyncareos_restore_test)
Backup immediately before migrations or destructive workRoll-forward path if restore fails
Custom-format dumps for drillspg_dump -Fc + pg_restore — faster verify than plain SQL
Keep the dump file until the drill passesEvidence for A5 sign-off
Uploads are separatePostgres restore does not restore /app/uploads — copy lyncare_uploads volume when moving hosts (01-ARCHITECTURE)

Dev first

VPS after dev pass

Yes

No

Audit migrations

Backup source DB

Environment

Restore to lyncareos_restore_test

Restore to lyncareos_restore_test on VPS

Compare row counts

Match?

Drop test DB + log pass

STOP - do not touch production

Investigate dump / permissions / version


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.

TierDatabaseHow to auditHow to apply
Devlyncareos_dev @ localhost:5432.\migration-audit.ps1 -Target Dev.\migrate.ps1 -Target Dev
Production (VPS)lyncareos in lyncareos-postgres-1.\migration-audit.ps1 -Target Vps -NonInteractiveNormally .\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).

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:

Terminal window
.\scripts\dev-postgres-super-check.ps1 # verify auth + show BYPASSRLS flags
.\scripts\grant-dev-backup-role.ps1 # or: dev-postgres-super-check.ps1 -Grant

VPS Docker Postgres does not need this workaround for routine dumps (verified on lyncareos-prod-syd1).

StepActionPass?
D1.\db-backup.ps1 -Target Dev or custom dump below
D2Create lyncareos_restore_test on same server
D3pg_restore into test DB
D4Compare users / tenants counts: lyncareos_dev vs test
D5Spot-check essentialqa user exists in test (if seeded)
D6DROP DATABASE lyncareos_restore_test
D7Record pass in backups/drill-log.jsonl
Terminal window
# 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 counts
psql -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;"
# Cleanup
psql -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.

StepAction
V1SSH root@134.199.173.206 (key ~/.ssh/lyncareos_vps)
V2Custom dump on VPS (pg_dump -Fc to /tmp/)
V3Restore into lyncareos_restore_test
V4Compare counts vs lyncareos
V5Drop test DB
V6Optional: scp dump to workstation backups\vps_*.dump
V7Update 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):

Terminal window
.\scripts\backup-restore-drill-vps-production.ps1 -IUnderstand

Steps: 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:

  1. Stop app and scan_worker on VPS.
  2. DROP DATABASE lyncareos; then CREATE DATABASE lyncareos OWNER lyncare; (separate psql commands).
  3. pg_restore into lyncareos (dump path is inside lyncareos-postgres-1, e.g. /tmp/….dump).
  4. Restart stack; verify /api/billing/tiers and login.
  5. Confirm lyncare_uploads volume intact or restore files from separate backup.

Commands: 07 §15.3 — destructive block.


ScriptPurpose
migration-audit.ps1 -Target Dev | VpsRead-only pending/orphan report
migrate.ps1 -Target DevApply pending migrations to dev
db-backup.ps1 -Target Dev | ProductionPlain SQL dump (adhoc)
scripts/grant-dev-backup-role.ps1One-time BYPASSRLS on native dev lyncare (needs LYNCARE_PG_SUPER_PASSWORD)
scripts/backup-restore-drill-dev.ps1Automated Phase 1 dev drill
scripts/backup-restore-drill-vps.ps1Automated Phase 2 VPS drill (requires dev pass unless -SkipDevCheck)
scripts/backup-restore-drill-vps-production.ps1Phase 3 destructive restore over live lyncareos (-IUnderstand)
scripts/vps-production-backup.ps1Tier 1 required + tier 2 best-effort; log backup-run-log.jsonl
scripts/pull-vps-backup-to-workstation.ps1Wrapper → vps-production-backup.ps1
scripts/deploy.ps1VPS deploy + pre-deploy dump + post-deploy workstation pull (best-effort)

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):

PhaseResultEvidence
Migrations Dev + VpspassV104, PENDING: none
Phase 1 — Dev drillpassbackups/lyncareos_dev_20260602_173528.dump — 9 users / 5 tenants
Phase 2 — VPS side DBpass/tmp/lyncareos_20260602_173547.dump — 9 users / 4 tenants
Phase 3 — Destructive prod restorepasslyncareos_prod_drill_20260602_175454.dump — 9/4 before & after; /api/billing/tiers 200
Tier 2 — Workstation (best-effort)automated + verifiedvps-production-backup.ps1; lyncareos_vps_20260602_180702.dump — log backup-run-log.jsonl
Tier 3 — Cloud B2deferred (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.