Backup and Point-in-Time Recovery (PITR)
LatticeDX stores persistent state across two independent PostgreSQL 16 instances (postgres for core API, Keycloak, and n8n; nocobase-db running pgvector/pgvector:0.8.1-pg16), paired with RustFS object storage and MongoDB for Orion-LD. To support low-RPO recovery and pre-incident verification, the platform pairs continuous Write-Ahead Log (WAL) archiving for Point-in-Time Recovery (PITR) with automated checksummed backup and restore-rehearsal scripts.
PostgreSQL 16 WAL Archiving and PITR Architecture
In infra/compose/prod.yml, both postgres and nocobase-db load infra/config/postgres/pitr.conf, mount infra/config/postgres/pitr-archive.sh, and write compressed WAL segments to dedicated volumes (postgres-wal-archive and nocobase-wal-archive).
| Parameter | Type | Default | Description |
|---|---|---|---|
wal_level | string | replica | Records sufficient WAL detail for continuous archiving and PITR replay. |
archive_mode | string | on | Enables the archiver background process to invoke archive_command per completed WAL segment. |
PG_ARCHIVE_COMMAND | string | /bin/sh /etc/postgresql/pitr-archive.sh %p %f | Archiver command; override for external wal-g or pgBackRest targets. |
archive_timeout | integer | 300 | Forces a WAL segment switch at least every 300 seconds under low write volume. |
wal_compression | string | on | Compresses full-page writes inside WAL to reduce storage I/O. |
wal_keep_size | string | 512MB | Retains at least 512 MB of recent WAL segments inside pg_wal. |
PITR_WAL_DIR | string | /var/lib/postgresql/wal-archive | Destination directory holding <wal>.gz segments with 0700 permissions. |
PITR_WAL_RETENTION_DAYS | integer | 7 | Number of days compressed .gz segments remain locally before pruning. |
WALG_S3_PREFIX | string | — | Optional S3 prefix (such as s3://bucket/pitr) enabling direct wal-g wal-push streaming. |
Safety Guarantees in pitr-archive.sh
/etc/postgresql/pitr-archive.sh validates WAL filenames strictly (rejecting /, \, .., and leading -), applies umask 077, and writes to a temporary file ${dest}.tmp.$$ before atomically renaming to <wal_name>.gz. If the target archive already exists, the script validates integrity with gzip -t and compares bytes with cmp -s: identical segments return exit 0 for idempotency, while mismatched contents fail non-zero so PostgreSQL preserves the source WAL segment.
When WALG_S3_PREFIX is configured and the wal-g binary is present, pitr-archive.sh delegates directly to exec wal-g wal-push "$wal_path". In environments using pgBackRest, override PG_ARCHIVE_COMMAND="pgbackrest --stanza=latticedx archive-push %p" without altering container layouts.
Scoped Backup and Rehearsal Scripts
Alongside continuous WAL archiving, scripts/ provides four Node.js recovery scripts that compute SHA-256 manifests, verify archive structure, and run non-destructive restore rehearsals against isolated databases or ephemeral containers.
| Package Script | Source File | Backup Scope | Rehearsal Verification (rehearse) |
|---|---|---|---|
pnpm backup:core | scripts/backup-core.mjs | lattice database (pg_dump --format=custom) | Restores into dx_restore_* and verifies core tables (records, foundation_tasks, enterprise_entities, workflow_runs). |
pnpm backup:full | scripts/full-backup-core.mjs | lattice DB + foundation_files objects | Verifies SHA-256 of every file in objects/ and tests authenticated download via internal API. |
pnpm backup:native | scripts/native-database-recovery.mjs | nocobase database (pg_dump --format=custom) | Validates 16 required native tables and compares per-table MD5 row fingerprints in dx_restore_*. |
pnpm backup:stack | scripts/stack-recovery.mjs | All 7 stateful platform components | Supports --ephemeral Postgres containers and runs verifyWalArchiveReadiness. |
pnpm backup:stack
# Or select specific components
node scripts/stack-recovery.mjs backup --components=nocobase-db,keycloak,lattice-dbStack Bundle Layout and Integrity Checks
scripts/stack-recovery.mjs packages seven components into output/backups/stack/stack-<32hex>/:
| Component | Bundle Artifact | Integrity & Rehearsal Check |
|---|---|---|
nocobase-db | nocobase/core-<id>.dump | Verifies PGDMP header, SHA-256, 16 required tables, and MD5 row fingerprints. |
uploads | uploads.tar.gz | Validates Gzip magic bytes, checks tar paths against traversal, and extracts to a temporary directory. |
keycloak | keycloak.dump + keycloak-realm.json | Validates PGDMP archive and parses the exported realm JSON. |
orion-mongo | orion-mongo.archive.gz | Verifies Gzip header and runs mongorestore --archive --gzip --dryRun. |
n8n | n8n-workflows.json + n8n-credentials.json | Confirms workflow and credential array counts match manifest.json. |
lattice-db | lattice.dump | Validates PGDMP, restores into dx_restore_*, and verifies core API tables. |
engine-objects | engine-objects.tar.gz | Validates tar archive of FILE_STORAGE_ROOT and records object entry counts. |
Every artifact path is checked against symlinks before reading or writing, and manifest.json records envPresence booleans for critical encryption keys (APP_KEY, ENCRYPTION_FIELD_KEY, DB_PASSWORD, N8N_ENCRYPTION_KEY, AGENT_API_KEY) without writing secret values to disk.
Guarded Live Stack Restore
Live restores via stack-recovery.mjs restore require an explicit confirmation flag --confirm-restore=<bundle-id> matching the target directory name and support --dry-run to validate all SHA-256 checksums and archive headers without mutating running services.
# Dry-run validation before touching live containers
node scripts/stack-recovery.mjs restore output/backups/stack/stack-<bundle_id> \
--confirm-restore=stack-<bundle_id> --dry-run
# Selective component restore
node scripts/stack-recovery.mjs restore output/backups/stack/stack-<bundle_id> \
--confirm-restore=stack-<bundle_id> \
--components=nocobase-db,uploadsBase Backup and Point-in-Time Recovery Runbook
Recovering to an exact timestamp before an accidental schema drop or logical corruption requires a physical base backup (pg_basebackup) paired with the .gz WAL sequence stored in *-wal-archive.
Step 1: Verify WAL Readiness and Capture a Physical Base Backup
# 1. Confirm archive_mode and archive_command on nocobase-db
docker compose -f infra/compose/prod.yml exec -T nocobase-db \
psql -U "$NOCOBASE_DB_USER" -d nocobase -c "SHOW wal_level; SHOW archive_mode; SHOW archive_command;"
# 2. Verify compressed .gz WAL segments inside the archive volume
docker compose -f infra/compose/prod.yml exec -T nocobase-db \
ls -lh /var/lib/postgresql/wal-archive | tail -n 10
# 3. Stream a compressed physical base backup from nocobase-db
mkdir -p ./backups/base/nocobase-$(date -u +%Y%m%dT%H%M%SZ)
docker compose -f infra/compose/prod.yml exec -T nocobase-db \
pg_basebackup -U "$NOCOBASE_DB_USER" -D - -Ft -z -Xs -P \
> ./backups/base/nocobase-base.tar.gz
# 4. Switch the current WAL segment so recent transactions archive immediately
docker compose -f infra/compose/prod.yml exec -T nocobase-db \
psql -U "$NOCOBASE_DB_USER" -d postgres -c "SELECT pg_switch_wal();"Step 2: Replay WAL Segments to recovery_target_time
Stop dependent application writers (nocobase, n8n) before replacing the data directory so no stale connections interfere with replay:
docker compose --env-file .env -f infra/compose/prod.yml stop nocobase n8nOnce pg_is_in_recovery() returns f, verify the nocobase_app runtime role (NOBYPASSRLS) and FORCE ROW LEVEL SECURITY state across tenant tables before starting the nocobase service.
Operational Notes and Security Boundaries
- Use
recovery_target_action = 'pause'during logical recovery so operators can inspect restored tables before callingpg_wal_replay_resume()and advancing the PostgreSQL timeline. - Inspect
walArchiveReadyinside generatedstack-rehearsal-*.jsonreports:verifyWalArchiveReadinessconfirmsarchiveMode === 'on', a non-nooparchiveCommand, a writable WAL directory, and executablepitr-archive.sh. - Back up
NOCOBASE_ENCRYPTION_FIELD_KEYandNOCOBASE_APP_KEYin an external secrets manager; without these keys, AES-256-GCM-wrapped Ed25519 keys inlatticePkiKeysand encrypted connector credentials cannot be decrypted after restore.
