Skip to content

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

PITR and WAL Archiving Parameters (infra/config/postgres/pitr.conf)
ParameterTypeDefaultDescription
wal_level
stringreplicaRecords sufficient WAL detail for continuous archiving and PITR replay.
archive_mode
stringonEnables the archiver background process to invoke archive_command per completed WAL segment.
PG_ARCHIVE_COMMAND
string/bin/sh /etc/postgresql/pitr-archive.sh %p %fArchiver command; override for external wal-g or pgBackRest targets.
archive_timeout
integer300Forces a WAL segment switch at least every 300 seconds under low write volume.
wal_compression
stringonCompresses full-page writes inside WAL to reduce storage I/O.
wal_keep_size
string512MBRetains at least 512 MB of recent WAL segments inside pg_wal.
PITR_WAL_DIR
string/var/lib/postgresql/wal-archiveDestination directory holding <wal>.gz segments with 0700 permissions.
PITR_WAL_RETENTION_DAYS
integer7Number 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 ScriptSource FileBackup ScopeRehearsal Verification (rehearse)
pnpm backup:corescripts/backup-core.mjslattice database (pg_dump --format=custom)Restores into dx_restore_* and verifies core tables (records, foundation_tasks, enterprise_entities, workflow_runs).
pnpm backup:fullscripts/full-backup-core.mjslattice DB + foundation_files objectsVerifies SHA-256 of every file in objects/ and tests authenticated download via internal API.
pnpm backup:nativescripts/native-database-recovery.mjsnocobase database (pg_dump --format=custom)Validates 16 required native tables and compares per-table MD5 row fingerprints in dx_restore_*.
pnpm backup:stackscripts/stack-recovery.mjsAll 7 stateful platform componentsSupports --ephemeral Postgres containers and runs verifyWalArchiveReadiness.
bash
pnpm backup:stack
# Or select specific components
node scripts/stack-recovery.mjs backup --components=nocobase-db,keycloak,lattice-db

Stack Bundle Layout and Integrity Checks ​

scripts/stack-recovery.mjs packages seven components into output/backups/stack/stack-<32hex>/:

ComponentBundle ArtifactIntegrity & Rehearsal Check
nocobase-dbnocobase/core-<id>.dumpVerifies PGDMP header, SHA-256, 16 required tables, and MD5 row fingerprints.
uploadsuploads.tar.gzValidates Gzip magic bytes, checks tar paths against traversal, and extracts to a temporary directory.
keycloakkeycloak.dump + keycloak-realm.jsonValidates PGDMP archive and parses the exported realm JSON.
orion-mongoorion-mongo.archive.gzVerifies Gzip header and runs mongorestore --archive --gzip --dryRun.
n8nn8n-workflows.json + n8n-credentials.jsonConfirms workflow and credential array counts match manifest.json.
lattice-dblattice.dumpValidates PGDMP, restores into dx_restore_*, and verifies core API tables.
engine-objectsengine-objects.tar.gzValidates 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.

bash
# 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,uploads

Base 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 ​

bash
# 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:

bash
docker compose --env-file .env -f infra/compose/prod.yml stop nocobase n8n

Once 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 calling pg_wal_replay_resume() and advancing the PostgreSQL timeline.
  • Inspect walArchiveReady inside generated stack-rehearsal-*.json reports: verifyWalArchiveReadiness confirms archiveMode === 'on', a non-noop archiveCommand, a writable WAL directory, and executable pitr-archive.sh.
  • Back up NOCOBASE_ENCRYPTION_FIELD_KEY and NOCOBASE_APP_KEY in an external secrets manager; without these keys, AES-256-GCM-wrapped Ed25519 keys in latticePkiKeys and encrypted connector credentials cannot be decrypted after restore.

Apache-2.0 · LatticeDX v0.4.2 · Powered by NocoBase · v2.2.50 · Apache-2.0 (Clause 5.2)