Self-hosting n8n is cheap and gives you full control over your data. It also makes you responsible for the part a cloud plan quietly does for you: backups. Most self-hosted setups we see have one of three problems. There is no backup at all. The backup sits on the same disk as the data. Or there is a backup nobody has ever tried to restore.
This is the setup we use for our own n8n instances and for clients: a nightly local dump, an encrypted offsite copy in Cloudflare R2, a Telegram message when anything goes wrong, and a restore test that proves the copy is usable. It runs on a single server with Docker Compose and costs close to nothing.
What actually needs a backup
An n8n instance is more than its workflows. To bring it back on a new machine you need three things:
- The database. Workflows, credentials, users, tags, variables and execution history all live there. With PostgreSQL that is one
pg_dump. With the default SQLite it is thedatabase.sqlitefile in the n8n data folder. - The encryption key. n8n encrypts every saved credential with
N8N_ENCRYPTION_KEY. If you did not set it yourself, n8n generated one and stored it in/home/node/.n8n/configinside the container volume. Restore the database without this key and every credential is unreadable: you will be re-entering API keys and OAuth connections by hand. - Your environment file. The
.envwith database passwords, the encryption key, webhook URL and other settings. Without it you will rebuild the configuration from memory.
If you store binary data on the filesystem (N8N_DEFAULT_BINARY_DATA_MODE=filesystem), add the binary data folder too.
Workflow JSON exports in git are nice for reviewing changes, but they are not a backup: they do not include credentials, users or the key.
Step 1: a nightly local dump
A small script next to the Compose file, started by cron at night:
#!/usr/bin/env bash # Nightly n8n backup: database dump and the .env with the encryption key. Keeps 14 days. set -euo pipefail cd "$(dirname "$0")" umask 077 # backups are readable only by the owner dir="backups/$(date +%F)" mkdir -p "$dir" docker compose -f compose.prod.yml exec -T postgres \ pg_dump -U postgres -Fc n8n </dev/null > "$dir/n8n.dump" cp .env "$dir/env.backup" find backups -mindepth 1 -maxdepth 1 -type d -mtime +14 -exec rm -rf {} + echo "$(date '+%F %T') ok: $(du -sh "$dir" | cut -f1)" Three details that save time later:
-
-Tand</dev/nullondocker compose exec. Without them the command behaves differently under cron than in your terminal, and it can swallow the rest of a script's input. -
-Fcwrites PostgreSQL's custom format: compressed, andpg_restorecan restore it into an existing database with--clean. -
umask 077, because the dump contains encrypted credentials and the.envcontains the key to decrypt them.
40 3 * * * /srv/n8n/backup.sh >> /srv/n8n/backups/backup.log 2>&1 This protects you from a broken upgrade or a deleted workflow. It does not protect you from a dead disk, a lost server or a compromised machine. For that you need a copy somewhere else.
Step 2: an encrypted copy in Cloudflare R2
We use restic for the offsite copy and Cloudflare R2 as storage:
- restic encrypts on your server before anything leaves it. R2 only ever sees encrypted blobs.
- Deduplication. Nightly dumps of the same database share most of their data, so 30 days of history take far less than 30 full copies.
- R2 has no egress fees, so a full restore does not come with a surprise bill. The free tier includes 10 GB of storage, which is plenty for most n8n instances.
In the Cloudflare dashboard, create an R2 bucket and an API token with Object Read & Write for that bucket only. Then restic needs four variables. Keep them out of git: in a root-only file, a password manager or SOPS.
export AWS_ACCESS_KEY_ID=... # R2 token export AWS_SECRET_ACCESS_KEY=... export AWS_DEFAULT_REGION=auto # R2 uses "auto" instead of a region export RESTIC_REPOSITORY="s3:https://<account-id>.r2.cloudflarestorage.com/<bucket>/restic" export RESTIC_PASSWORD=... # long random string, see the warning below restic init Store
RESTIC_PASSWORDin a second place, such as a password manager. It is the only way to decrypt the repository. If it is lost, so are all the backups.
The offsite script runs after the local dump. Before uploading, it checks that today's dump exists and is not empty. Without this check, a silently failing cron job would upload yesterday's data every night and you would never notice.
#!/usr/bin/env bash set -uo pipefail source /etc/n8n-backup.env # the variables above + TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID; chmod 600 src=/srv/n8n/backups today=$(date +%F) notify() { # Telegram message; the token goes through stdin, not argv printf 'url = "https://api.telegram.org/bot%s/sendMessage"\n' "$TELEGRAM_BOT_TOKEN" | curl -s -m 20 -o /dev/null -K - --data-urlencode "chat_id=$TELEGRAM_CHAT_ID" --data-urlencode "text=$1" } fail() { notify "❌ n8n offsite backup: $1"; exit 1; } find "$src/$today" -name '*.dump' -size +0 2>/dev/null | grep -q . || fail "no local dump for $today" restic backup --quiet --tag n8n "$src" || fail "restic backup failed" restic forget --quiet --tag n8n --keep-daily 14 --keep-weekly 8 --keep-monthly 12 --prune \ || fail "cleanup of old snapshots failed" if [[ "$(date +%u)" == 7 ]]; then # on Sundays: verify 10% of the stored data restic check --quiet --read-data-subset=10% || fail "restic check: repository damaged" notify "✅ n8n offsite backups OK: $(restic snapshots --json | jq length) snapshots" fi The retention policy keeps two weeks of daily snapshots, two months of weekly ones and a year of monthly ones. Thanks to deduplication this is still small.
Silence means success, and any failure sends a message. The weekly "OK" message matters as much as the failure alerts: if it stops arriving, the backup job itself is broken.
Step 3: prove that it restores
A backup is a hypothesis until you restore it. We test at two levels.
Weekly, automatically: restore the latest snapshot into a temporary folder and compare checksums with the local files.
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT restic restore latest --quiet --tag n8n --target "$tmp" last=$(ls "$tmp/srv/n8n/backups" | grep -E '^[0-9]{4}-' | sort | tail -1) diff <(cd "$tmp/srv/n8n/backups/$last" && sha256sum -- * | sort) \ <(cd "/srv/n8n/backups/$last" && sha256sum -- * | sort) \ && echo "restore OK: $last" || echo "MISMATCH in $last" Every few months, by hand: a real restore into a throwaway n8n. Start an empty stack with the same image version and the restored .env, then load the dump:
docker compose -f compose.test.yml up -d postgres docker compose -f compose.test.yml exec -T postgres \ pg_restore -U postgres -d n8n --clean --if-exists < n8n.dump docker compose -f compose.test.yml up -d n8n Log in, open a workflow that uses credentials and run a step. If the credential works, the encryption key and the database belong together, and your backup is real. Write down how long this took: that is your actual recovery time.
What this costs
For a typical small instance with a few hundred megabytes of database:
- R2 storage stays within the free tier.
- restic and cron are free and run on the same server.
- Setting it up takes an afternoon.
- Maintenance comes down to reading the weekly Telegram message.
Checklist
- [ ]
N8N_ENCRYPTION_KEYis set explicitly and stored outside the server - [ ] Nightly
pg_dump(or SQLite copy) plus.env, kept for at least 14 days - [ ] Encrypted offsite copy in a different provider or region
- [ ] The restic password is stored in a second place
- [ ] Alerts on failure and a weekly "OK" message
- [ ] An automatic restore check, plus a full restore test every few months
If your n8n runs workflows the business depends on, this afternoon of work is the cheapest insurance you will buy this year.
![[CVE-2026-116331] Deep Dive - How One SORTOP Handed Over PostgreSQL](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmceztdcf80os633lbll3.png)
