StackBlueprint

EP01 DISASTER RECOVERY · AGE-ENCRYPTED PG_DUMP → CLOUDFLARE R2

Automated Encrypted n8n PostgreSQL Backups to Cloudflare R2 & Restore Drill

An untested VPS snapshot is not a backup. Archive pg_dump --format=custom, N8N_ENCRYPTION_KEY, and credentials into an age-encrypted Cloudflare R2 object daily ($0 on R2’s 10 GB free tier) and verify it inside an isolated --network none Postgres 17 container.

Cloudflare R2 Storage Cost $0.00/mo 10 GB free tier · $0 egress fees on restore
Encryption Model Asymmetric age Public key on VPS · private identity offline
Verification Method Isolated Drill --network none + pg_restore --single-transaction

1. Four Artifacts Every Recoverable n8n Snapshot Must Contain

Restoring PostgreSQL without the matching encryption key or n8n version leaves credentials unreadable or schema migrations mismatched. Every EP01 snapshot bundles four files inside one immutable .tar.age archive:

Archive Member Source Command Why Recovery Fails Without It
database.dump pg_dump --format=custom Atomic PostgreSQL 17 dump covering workflows, credentials, users, and RAG/DLQ tables
instance.env Root .env copy Carries N8N_ENCRYPTION_KEY required to decrypt credentials_entity
credentials.json n8n export:credentials --all Portable encrypted export if migrating credentials across environments
n8n-version.txt n8n --version Pins the exact n8n image tag to boot on a clean host before upgrading

2. Automated R2 Backup Script & Systemd Timer (EP01 Kit)

Below is the core backup pipeline from 02-backup-r2.sh in n8n Production Kit v1. It enforces umask 077, acquires a non-blocking flock lock, pipes directly into age -r "$recipient" (no unencrypted archive written to disk), and uploads immutably via rclone:

#!/usr/bin/env bash
set -euo pipefail
umask 077
# Lock prevents overlapping runs; trap removes temp workspace & alerts Slack/Discord on error
exec 9>.backup.lock
flock -n 9 || { echo 'A backup is already running.' >&2; exit 1; }

snapshot="n8n-$(date -u +%Y%m%dT%H%M%SZ)-$(openssl rand -hex 4).tar.age"
cp .env "$work/instance.env"

"${compose[@]}" exec -T postgres sh -ec \
  'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" --format=custom' > "$work/database.dump"

"${compose[@]}" exec -T n8n n8n --version > "$work/n8n-version.txt"

# Stream tar straight into asymmetric age encryption (public key only on VPS)
tar -C "$work" -cf - database.dump instance.env credentials.json n8n-version.txt |
  age -r "$recipient" -o "$work/$snapshot"

# --immutable prevents a compromised host from overwriting prior backups
"${storage[@]}" copyto --immutable "$work/$snapshot" "$destination/$snapshot"
printf 'Backup uploaded: %s\n' "$snapshot"

Daily Systemd Timer (stackblueprint-n8n-backup.timer)

[Unit]
Description=Run the StackBlueprint n8n backup daily

[Timer]
OnCalendar=*-*-* 03:00:00 UTC
RandomizedDelaySec=15min
Persistent=true

[Install]
WantedBy=timers.target

3. Isolated Zero-Touch Restore Drill (03-restore-drill.sh)

Run this drill on a staging host or locally whenever you upgrade PostgreSQL or n8n. It decrypts the snapshot with your offline age identity, verifies N8N_ENCRYPTION_KEY is non-empty, restores into an air-gapped --network none container, and asserts non-zero table counts:

# Decrypt snapshot to umask 077 workspace and assert key presence
age -d -i "$identity" -o "$work/snapshot.tar" "$work/snapshot.age"
tar -xOf "$work/snapshot.tar" database.dump > "$work/database.dump"
tar -xOf "$work/snapshot.tar" instance.env | grep '^N8N_ENCRYPTION_KEY=' > "$work/key"
test -s "$work/key"

# Boot air-gapped Postgres 17 drill container (zero host network exposure)
container=$(docker run -d --network none --label stackblueprint.restore-drill=true \
  --memory 768m --cpus 1 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:17-alpine \
  postgres -c listen_addresses=localhost)

# Atomic restore + schema integrity verification
docker exec -i "$container" pg_restore --exit-on-error --single-transaction \
  --no-owner --no-acl -U postgres -d postgres < "$work/database.dump"

docker exec "$container" psql -X -v ON_ERROR_STOP=1 -U postgres -d postgres \
  -c 'SELECT count(*) AS workflows FROM workflow_entity' \
  -c 'SELECT count(*) AS credentials FROM credentials_entity' \
  -c 'SELECT count(*) AS users FROM "user"'

Before running backups or restoring workflows onto a fresh host, scan your n8n JSON exports and Compose stack locally with StackGuard n8n Workflow Security Audit to verify zero plaintext secrets leaked into workflow nodes.

Affiliate disclosure: Hostinger links are affiliate links; we may earn a commission at no extra cost to you. See Affiliate Disclosure. Published by Oriancy.

Frequently Asked Questions

Why is pg_dump alone not enough to restore a self-hosted n8n instance?
Every credential inside credentials_entity is encrypted with N8N_ENCRYPTION_KEY from your .env file. Without archiving instance.env and n8n-version.txt in the same encrypted bundle, restored workflows fail to authenticate.
Why encrypt n8n backups with an age public key before uploading to Cloudflare R2?
With asymmetric age encryption, your VPS only holds the public age1... recipient key while the decryption identity stays offline in your password manager. Even full VPS compromise cannot decrypt prior R2 snapshots.
How do you verify an n8n R2 backup without touching production PostgreSQL?
Stream the backup into an ephemeral postgres:17-alpine container launched with --network none, run pg_restore --exit-on-error --single-transaction, and confirm expected row counts across workflow_entity, credentials_entity, and "user".