# Zero-Cost Encrypted Backups for Self-Hosted AI Stacks: Automated pg_dump + age + Cloudflare R2 & Restore Drill

- URL: https://nodebudget.dev/guides/encrypted-offsite-backups-n8n-postgres-r2/
- Author: Yuanqiang Leng (https://github.com/a895047059)
- Updated: 2026-11-26

## TL;DR
An untested backup stored on the same VPS disk is not a backup—it is a wish. Combine nightly `pg_dump | gzip` + `age` public-key encryption + `rclone copy` to Cloudflare R2’s free 10 GB tier (zero egress fees), store your `N8N_ENCRYPTION_KEY` separately in a password manager, and run a 3-minute restore drill into a throwaway container once a quarter.

## 1. The Two Disaster Mistakes That Wipe Out Months of Automations

Mistake #1 is backing up `/root/backups/n8n.sql.gz` onto the exact same VPS SSD. If the host RAID array fails or a billing glitch terminates the VM, both your live server and your backups vanish in the same second.

Mistake #2 is backing up PostgreSQL faithfully every night while forgetting `.env` (`N8N_ENCRYPTION_KEY`). When you restore `n8n.sql.gz` onto a fresh VPS with a freshly generated encryption key, n8n boots cleanly—and then throws decryption errors on every single Google, Slack, OpenAI, and Stripe credential you ever saved.

> **Never Upload Unencrypted SQL Dumps to Object Storage**: Even with encrypted credentials, your n8n execution tables contain raw customer emails, CRM payloads, and LLM prompts in plaintext JSONB. Encrypt locally with `age` public-key cryptography before a single byte leaves your VPS—that way even if your server is compromised, an attacker cannot decrypt historical archives without your offline private key.

## 2. Copy-Paste Nightly Encrypted Backup Script (Cloudflare R2 Free 10 GB)

With `age` (single static Go binary, zero GPG keyring headaches) and `rclone`, your entire encrypted disaster-recovery pipeline fits in ten lines of bash and costs **$0.00/month** on Cloudflare R2’s 10 GB free tier:

```bash
#!/usr/bin/env bash
set -euo pipefail
STAMP=$(date +%F-%H%M)
TMP="/tmp/n8n-${STAMP}.sql.gz.age"
AGE_PUBKEY="age1yourpublickeyhere..."   # Keep private key OFFLINE in 1Password/Bitwarden

docker exec nodebudget-postgres pg_dump -U n8n n8n \
  | gzip -9 \
  | age -r "${AGE_PUBKEY}" > "${TMP}"

rclone copyto "${TMP}" "r2:nodebudget-backups/n8n-${STAMP}.sql.gz.age"
rm -f "${TMP}"
# Keep 30 days of encrypted daily snapshots automatically
rclone delete --min-age 30d r2:nodebudget-backups/
```

---
Source: NodeBudget (https://nodebudget.dev) · License: CC BY 4.0 · Disclosure: Some outbound links under /go/* earn affiliate commission. As an Amazon Associate I earn from qualifying purchases.