Discuss your project

Blog

Offsite backups for self-hosted n8n: restic, Cloudflare R2 and a restore test

Mher Poghosyan · 29 November 2026 · 6 min read

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:

  1. 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 the database.sqlite file in the n8n data folder.
  2. 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/config inside 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.
  3. Your environment file. The .env with 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:

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:

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_PASSWORD in 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:

Checklist

If your n8n runs workflows the business depends on, this afternoon of work is the cheapest insurance you will buy this year.

#n8n#devops#backup#selfhosted