<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kubmeta</title>
    <description>The latest articles on DEV Community by Kubmeta (kubmeta).</description>
    <link>https://dev.to/kubmeta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F16583%2Fc235fa44-2798-42a5-8796-460436607e3a.png</url>
      <title>DEV Community: Kubmeta</title>
      <link>https://dev.to/kubmeta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kubmeta"/>
    <language>en</language>
    <item>
      <title>Offsite backups for self-hosted n8n: restic, Cloudflare R2 and a restore test</title>
      <dc:creator>Mher Poghosyan</dc:creator>
      <pubDate>Sun, 29 Nov 2026 20:58:30 +0000</pubDate>
      <link>https://dev.to/kubmeta/offsite-backups-for-self-hosted-n8n-restic-cloudflare-r2-and-a-restore-test-57e4</link>
      <guid>https://dev.to/kubmeta/offsite-backups-for-self-hosted-n8n-restic-cloudflare-r2-and-a-restore-test-57e4</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually needs a backup
&lt;/h2&gt;

&lt;p&gt;An n8n instance is more than its workflows. To bring it back on a new machine you need three things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The database.&lt;/strong&gt; Workflows, credentials, users, tags, variables and execution history all live there. With PostgreSQL that is one &lt;code&gt;pg_dump&lt;/code&gt;. With the default SQLite it is the &lt;code&gt;database.sqlite&lt;/code&gt; file in the n8n data folder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The encryption key.&lt;/strong&gt; n8n encrypts every saved credential with &lt;code&gt;N8N_ENCRYPTION_KEY&lt;/code&gt;. If you did not set it yourself, n8n generated one and stored it in &lt;code&gt;/home/node/.n8n/config&lt;/code&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your environment file.&lt;/strong&gt; The &lt;code&gt;.env&lt;/code&gt; with database passwords, the encryption key, webhook URL and other settings. Without it you will rebuild the configuration from memory.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you store binary data on the filesystem (&lt;code&gt;N8N_DEFAULT_BINARY_DATA_MODE=filesystem&lt;/code&gt;), add the binary data folder too.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: a nightly local dump
&lt;/h2&gt;

&lt;p&gt;A small script next to the Compose file, started by cron at night:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# Nightly n8n backup: database dump and the .env with the encryption key. Keeps 14 days.&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -euo pipefail
&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"$(dirname "&lt;/span&gt;&lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="s2"&gt;")"&lt;/span&gt;

&lt;span class="nb"&gt;umask&lt;/span&gt; 077                                   &lt;span class="c"&gt;# backups are readable only by the owner&lt;/span&gt;
&lt;span class="nv"&gt;dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"backups/$(date +%F)"&lt;/span&gt;
mkdir -p &lt;span class="s2"&gt;"$dir"&lt;/span&gt;

docker compose -f compose.prod.yml &lt;span class="nb"&gt;exec&lt;/span&gt; -T postgres \
  pg_dump -U postgres -Fc n8n &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"$dir/n8n.dump"&lt;/span&gt;
cp .env &lt;span class="s2"&gt;"$dir/env.backup"&lt;/span&gt;

find backups -mindepth 1 -maxdepth 1 -type d -mtime +14 -exec rm -rf &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt; +
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"$(date '+%F %T') ok: $(du -sh "&lt;/span&gt;&lt;span class="nv"&gt;$dir&lt;/span&gt;&lt;span class="s2"&gt;" | cut -f1)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details that save time later:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-T&lt;/code&gt; and &lt;code&gt;&amp;lt;/dev/null&lt;/code&gt; on &lt;code&gt;docker compose exec&lt;/code&gt;. Without them the command behaves differently under cron than in your terminal, and it can swallow the rest of a script's input.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-Fc&lt;/code&gt; writes PostgreSQL's custom format: compressed, and &lt;code&gt;pg_restore&lt;/code&gt; can restore it into an existing database with &lt;code&gt;--clean&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;umask 077&lt;/code&gt;, because the dump contains encrypted credentials and the &lt;code&gt;.env&lt;/code&gt; contains the key to decrypt them.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;40 3 * * * /srv/n8n/backup.sh &amp;gt;&amp;gt; /srv/n8n/backups/backup.log 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: an encrypted copy in Cloudflare R2
&lt;/h2&gt;

&lt;p&gt;We use &lt;a href="https://restic.net/" rel="noopener noreferrer"&gt;restic&lt;/a&gt; for the offsite copy and Cloudflare R2 as storage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;restic encrypts on your server&lt;/strong&gt; before anything leaves it. R2 only ever sees encrypted blobs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deduplication.&lt;/strong&gt; Nightly dumps of the same database share most of their data, so 30 days of history take far less than 30 full copies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;R2 has no egress fees&lt;/strong&gt;, 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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the Cloudflare dashboard, create an R2 bucket and an API token with &lt;em&gt;Object Read &amp;amp; Write&lt;/em&gt; for that bucket only. Then restic needs four variables. Keep them out of git: in a root-only file, a password manager or SOPS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;...                &lt;span class="c"&gt;# R2 token&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;...
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;AWS_DEFAULT_REGION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;auto              &lt;span class="c"&gt;# R2 uses "auto" instead of a region&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;RESTIC_REPOSITORY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"s3:https://&amp;lt;account-id&amp;gt;.r2.cloudflarestorage.com/&amp;lt;bucket&amp;gt;/restic"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;RESTIC_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;...                  &lt;span class="c"&gt;# long random string, see the warning below&lt;/span&gt;
restic init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Store &lt;code&gt;RESTIC_PASSWORD&lt;/code&gt; 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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -uo pipefail
&lt;span class="nb"&gt;source&lt;/span&gt; /etc/n8n-backup.env                  &lt;span class="c"&gt;# the variables above + TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID; chmod 600&lt;/span&gt;
&lt;span class="nv"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/srv/n8n/backups
&lt;span class="nv"&gt;today&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;date +%F&lt;span class="o"&gt;)&lt;/span&gt;

notify&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;                                  &lt;span class="c"&gt;# Telegram message; the token goes through stdin, not argv&lt;/span&gt;
  &lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'url = "https://api.telegram.org/bot%s/sendMessage"\n'&lt;/span&gt; &lt;span class="s2"&gt;"$TELEGRAM_BOT_TOKEN"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
    curl -s -m 20 -o /dev/null -K - --data-urlencode &lt;span class="s2"&gt;"chat_id=$TELEGRAM_CHAT_ID"&lt;/span&gt; --data-urlencode &lt;span class="s2"&gt;"text=$1"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
fail&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; notify &lt;span class="s2"&gt;"❌ n8n offsite backup: $1"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 1&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

find &lt;span class="s2"&gt;"$src/$today"&lt;/span&gt; -name &lt;span class="s1"&gt;'*.dump'&lt;/span&gt; -size +0 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;|&lt;/span&gt; grep -q . &lt;span class="o"&gt;||&lt;/span&gt; fail &lt;span class="s2"&gt;"no local dump for $today"&lt;/span&gt;

restic backup --quiet --tag n8n &lt;span class="s2"&gt;"$src"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; fail &lt;span class="s2"&gt;"restic backup failed"&lt;/span&gt;
restic forget --quiet --tag n8n --keep-daily 14 --keep-weekly 8 --keep-monthly 12 --prune \
  &lt;span class="o"&gt;||&lt;/span&gt; fail &lt;span class="s2"&gt;"cleanup of old snapshots failed"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$(date +%u)"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; 7 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;           &lt;span class="c"&gt;# on Sundays: verify 10% of the stored data&lt;/span&gt;
  restic check --quiet --read-data-subset=10% &lt;span class="o"&gt;||&lt;/span&gt; fail &lt;span class="s2"&gt;"restic check: repository damaged"&lt;/span&gt;
  notify &lt;span class="s2"&gt;"✅ n8n offsite backups OK: $(restic snapshots --json | jq length) snapshots"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: prove that it restores
&lt;/h2&gt;

&lt;p&gt;A backup is a hypothesis until you restore it. We test at two levels.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Weekly, automatically:&lt;/strong&gt; restore the latest snapshot into a temporary folder and compare checksums with the local files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;tmp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;mktemp -d&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;trap&lt;/span&gt; &lt;span class="s1"&gt;'rm -rf "$tmp"'&lt;/span&gt; EXIT
restic restore latest --quiet --tag n8n --target &lt;span class="s2"&gt;"$tmp"&lt;/span&gt;
&lt;span class="nv"&gt;last&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;ls &lt;span class="s2"&gt;"$tmp/srv/n8n/backups"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;'^[0-9]{4}-'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; sort &lt;span class="o"&gt;|&lt;/span&gt; tail -1&lt;span class="o"&gt;)&lt;/span&gt;
diff &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"$tmp/srv/n8n/backups/$last"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; sha256sum -- * &lt;span class="o"&gt;|&lt;/span&gt; sort&lt;span class="o"&gt;)&lt;/span&gt; \
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"/srv/n8n/backups/$last"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; sha256sum -- * &lt;span class="o"&gt;|&lt;/span&gt; sort&lt;span class="o"&gt;)&lt;/span&gt; \
  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"restore OK: $last"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"MISMATCH in $last"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Every few months, by hand:&lt;/strong&gt; a real restore into a throwaway n8n. Start an empty stack with the same image version and the restored &lt;code&gt;.env&lt;/code&gt;, then load the dump:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose -f compose.test.yml up -d postgres
docker compose -f compose.test.yml &lt;span class="nb"&gt;exec&lt;/span&gt; -T postgres \
  pg_restore -U postgres -d n8n --clean --if-exists &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; n8n.dump
docker compose -f compose.test.yml up -d n8n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this costs
&lt;/h2&gt;

&lt;p&gt;For a typical small instance with a few hundred megabytes of database:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;R2 storage stays within the free tier.&lt;/li&gt;
&lt;li&gt;restic and cron are free and run on the same server.&lt;/li&gt;
&lt;li&gt;Setting it up takes an afternoon.&lt;/li&gt;
&lt;li&gt;Maintenance comes down to reading the weekly Telegram message.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;code&gt;N8N_ENCRYPTION_KEY&lt;/code&gt; is set explicitly and stored outside the server&lt;/li&gt;
&lt;li&gt;[ ] Nightly &lt;code&gt;pg_dump&lt;/code&gt; (or SQLite copy) plus &lt;code&gt;.env&lt;/code&gt;, kept for at least 14 days&lt;/li&gt;
&lt;li&gt;[ ] Encrypted offsite copy in a different provider or region&lt;/li&gt;
&lt;li&gt;[ ] The restic password is stored in a second place&lt;/li&gt;
&lt;li&gt;[ ] Alerts on failure and a weekly "OK" message&lt;/li&gt;
&lt;li&gt;[ ] An automatic restore check, plus a full restore test every few months&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your n8n runs workflows the business depends on, this afternoon of work is the cheapest insurance you will buy this year.&lt;/p&gt;

</description>
      <category>n8n</category>
      <category>devops</category>
      <category>backup</category>
      <category>selfhosted</category>
    </item>
  </channel>
</rss>
