Business tech · Intermediate · 24 min
Back up and update a self-hosted n8n
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Docker Compose · n8n 2.49.7
You will back up a self-hosted n8n 2.49 instance (workflow and credential exports, a database dump and the encryption key), prove the backup by restoring it on a fresh instance, then update the pinned image tag with a rollback plan ready.
On this page
Ember Studio, a seven-person design agency with staff in Lagos and Nairobi, runs its enquiry and invoicing automations on its own n8n server. Those workflows now matter: if the server's disk fails or an update goes wrong, client enquiries stop arriving. This tutorial gives the studio a backup it has actually restored, and a way to update n8n without gambling.
An n8n backup has three parts, and each is useless without the others:
- The database. PostgreSQL holds the workflows, the credentials, users, settings and execution history.
- The encryption key. n8n encrypts every saved credential (API keys, passwords, OAuth tokens) with
N8N_ENCRYPTION_KEYbefore it writes it to the database. Restore the database with a different key and every credential is unreadable. - The settings.
compose.yamland.envsay which image tag, database and URLs the instance uses.
You will take two kinds of copy: n8n's own exports of workflows and credentials, which are small and easy to check, and a full pg_dump of the database. Then you restore the exports on a throwaway instance to prove they work, and finally update the image tag with a rollback ready.
The commands assume the setup from the self-host n8n tutorial: a folder ~/n8n holding compose.yaml and .env, a service called n8n running the image n8nio/n8n:2.49.7, and a service called postgres. If your folder or service names differ, change them in the commands. Run everything on the n8n server, in one terminal.
Step 1 · Find the encryption key
Before you copy anything, confirm where your key lives. The self-host tutorial puts it in .env as N8N_ENCRYPTION_KEY. Count the matching lines without printing the key:
cd ~/n8n
grep -c '^N8N_ENCRYPTION_KEY=' .envIf the answer is 0, n8n generated a key itself on first start and saved it in its data folder, /home/node/.n8n/config, inside the n8n volume. Read it with docker compose exec n8n cat /home/node/.n8n/config, add it to .env as N8N_ENCRYPTION_KEY=<the key> (the value from that file, not a new one), and restart with docker compose up -d. A key in .env is a key you can see, copy and back up. A key only inside a volume disappears with the volume.
Now save the key in your password manager as its own entry, for example "n8n encryption key, Ember Studio". Keep it there, not only with the backups. Anyone who holds both the database and the key can read every credential n8n stores, so the two should not travel together.
The backup archive later in this tutorial leaves .env out for the same reason, so the password manager is where the rest of .env is kept too. The self-host tutorial only had you save the encryption key. Add the other values, POSTGRES_PASSWORD and N8N_DOMAIN (and any variable you have added since), to the same entry or a second one, for example "n8n .env, Ember Studio". List the variable names without their values so you know what the entry must hold:
cut -d= -f1 ~/n8n/.envCheck it worked
grep -c prints 1. Compare the last six characters of the key with the entry in your password manager:
grep '^N8N_ENCRYPTION_KEY=' ~/n8n/.env | tail -c 7The six characters match the end of the saved entry, and your password manager holds a value for every name the cut command printed: N8N_DOMAIN, POSTGRES_PASSWORD and N8N_ENCRYPTION_KEY on a setup from the self-host tutorial.
Step 2 · Export workflows and credentials
Make a dated, private folder for this backup. install -d -m 700 creates it with owner-only permissions from the start:
cd ~/n8n
BACKUP=~/n8n/backups/$(date +%Y-%m-%d-%H%M)
install -d -m 700 ~/n8n/backups "$BACKUP"
echo "$BACKUP"Keep this terminal open. Later steps use $BACKUP. If you open a new terminal, set it again to the folder the last command printed.
Run the n8n CLI inside the running container. --backup is shorthand for --all --pretty --separate: every item, one readable JSON file each. The exports go to a temporary folder in the container, are copied out, and the temporary folder is removed:
docker compose exec -u node n8n n8n export:workflow --backup --output=/tmp/n8n-export/workflows/
docker compose exec -u node n8n n8n export:credentials --backup --output=/tmp/n8n-export/credentials/
docker compose cp n8n:/tmp/n8n-export/. "$BACKUP/n8n-export/"
docker compose exec -u node n8n rm -rf /tmp/n8n-exportThe credential export is still encrypted with your key. That is what you want for a backup: the files are useless to anyone without the key, and a restore onto an instance with the same key needs nothing else.
About decrypted exports
export:credentials also has a --decrypted flag that writes every API key and password in plain text. n8n documents it for moving to an instance that has a different key. You do not need it for backups, so leave it out. If you ever do need it, write the file into a folder created with install -d -m 700, copy it straight into the new instance, and delete it the same hour. Never put a decrypted export in a backup archive, a chat message or a shared drive.
Check it worked
ls "$BACKUP"/n8n-export/workflows "$BACKUP"/n8n-export/credentials
grep -c '"data": "' "$BACKUP"/n8n-export/credentials/*.jsonls lists one .json file per workflow and per credential, named by their IDs. grep prints 1 for each credential file: its data field is one long encrypted string, not readable fields such as your token. If a folder is empty, that instance has no workflows or credentials of that kind yet.
Step 3 · Dump the database and package the backup
The exports cover workflows and credentials. The database also holds users, sharing settings, tags, variables and execution history, so take a full dump as well. This runs pg_dump inside the postgres container with the user and database names already set there, and writes a compressed custom-format dump into the backup folder:
cd ~/n8n
docker compose exec -T postgres sh -c 'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fc' > "$BACKUP/n8n.dump"
docker compose exec -T postgres pg_restore --list < "$BACKUP/n8n.dump" | grep -c 'TABLE DATA'The second command reads the dump back and counts the tables with data in it. A count above zero, and an entry for workflow_entity if you search for it, means the dump is complete enough to restore. The PostgreSQL backup tutorial covers dump formats, restoring into an empty database and scheduling the dump with cron.
Add a copy of compose.yaml, so the backup records the exact image tag it came from. Leave .env out of the archive: it holds the encryption key and database password, and you saved every value in it to your password manager in the first step. Then pack the folder into one archive. The subshell sets a private umask for tar only, so the archive is created readable by you alone:
install -m 600 ~/n8n/compose.yaml "$BACKUP/"
(umask 077; tar -C ~/n8n/backups -czf "$BACKUP.tar.gz" "$(basename "$BACKUP")")Copy the archive off the server: to another machine, object storage or whatever the studio's 3-2-1 backup plan uses. With rsync over SSH it looks like this; replace the user, host and folder with your own:
rsync -av "$BACKUP.tar.gz" backup-user@backup.example.net:n8n-backups/The exports are small, usually well under a megabyte, but the dump grows with execution history. If your office connection runs on prepaid mobile data, send the archive from the server to the backup host directly, as above, rather than downloading it to a laptop first.
Check it worked
ls -l "$BACKUP.tar.gz"
tar -tzf "$BACKUP.tar.gz"The archive's permissions start -rw-------. The listing shows compose.yaml, n8n.dump and the n8n-export folder with its workflows and credentials files.
Step 4 · Restore on a fresh instance
A backup you have not restored is a guess. Prove this one on a throwaway n8n container on the same server. It uses SQLite, has no data, listens only on 127.0.0.1:5679 so it does not clash with the live instance, and gets nothing but your encryption key.
Write the key into a private env file for it. The subshell's umask creates the file owner-only before the key is written:
(umask 077; grep '^N8N_ENCRYPTION_KEY=' ~/n8n/.env > ~/n8n/restore-test.env)Start the test instance from the same image tag and wait until it reports ready. A fresh instance creates its database on first start, and running the import CLI while those migrations are still going can fail. Then copy the exports in, give them to the node user n8n runs as, and import credentials first, then workflows:
docker run -d --name n8n-restore-test --env-file ~/n8n/restore-test.env -p 127.0.0.1:5679:5678 n8nio/n8n:2.49.7
until curl -fsS http://127.0.0.1:5679/healthz/readiness >/dev/null; do sleep 2; done
docker cp "$BACKUP/n8n-export" n8n-restore-test:/tmp/restore
docker exec -u root n8n-restore-test chown -R node:node /tmp/restore
docker exec -u node n8n-restore-test n8n import:credentials --separate --input=/tmp/restore/credentials
docker exec -u node n8n-restore-test n8n import:workflow --separate --input=/tmp/restore/workflowsImported workflows arrive switched off, so nothing on the test instance fires a webhook or sends an email.
Check it worked
Check the instance is up, that every workflow is there, and that the credentials decrypt with your key:
curl -fsS http://127.0.0.1:5679/healthz/readiness
docker exec -u node n8n-restore-test n8n list:workflow
docker exec -u node n8n-restore-test n8n export:credentials --all --decrypted --pretty --output=/tmp/check.json
docker exec -u node n8n-restore-test sh -c 'grep -c "\"data\": {" /tmp/check.json'The readiness check prints {"status":"ok"}, which means the test instance's database is connected and migrated. list:workflow prints one id|name line for each workflow, and the list matches the live instance. The last command prints the number of credentials: each one decrypted into readable fields. If the export fails with a decryption error instead, the key in restore-test.env is not the key that encrypted the backup, and you have found that out on a test, not during an outage.
Remove the test instance and its key file. The decrypted check file was inside the container, so it goes with it:
docker rm -f -v n8n-restore-testrm -f ~/n8n/restore-test.envRepeat this test after every change to how you back up, and every few months anyway.
Step 5 · Read the release notes before you update
n8n ships often, sometimes several patch releases in a week, and its own advice is to update at least once a month so you never jump many versions at once. First confirm what you run now:
docker compose exec n8n n8n --versionThen read, for every version between yours and the target:
- The release notes: the n8n changelog at docs.n8n.io/changelog, or the releases page of the
n8n-io/n8nrepository on GitHub. Note fixes for nodes your workflows use. - The breaking changes list:
packages/cli/BREAKING-CHANGES.mdin the same repository. It names each version that changes behaviour and what to do about it, such as renamed environment variables, removed nodes or new security checks.
Write down anything that applies to Ember Studio's setup, for example a changed environment variable you must add to .env, before you touch the server.
Check it worked
You have the current version, the target version, and a short list of the breaking changes between them that affect your workflows, or a note that none do.
Step 6 · Update the pinned image tag
Take a fresh backup first: repeat the export and dump steps, so $BACKUP points at a copy made minutes before the update. That dump is your way back.
Open compose.yaml in an editor and change the tag on the n8n service's image line, from:
image: n8nio/n8n:2.49.7to the exact version you chose, for example n8nio/n8n:2.50.1. Use a version number, never latest, so the server only changes when you change this line. Leave the postgres image alone; updating the database is a separate job.
Pull the new image and recreate only the n8n container. n8n runs any database migrations the new version needs as it starts:
cd ~/n8n
docker compose pull n8n
docker compose up -d n8n
docker compose logs --since 10m n8nRead the logs for migration messages and errors. Startup with migrations can take a minute.
Check it worked
docker compose ps
docker compose exec n8n n8n --version
curl -fsS http://127.0.0.1:5678/healthz/readinessdocker compose ps shows the n8n service running, the version is the one you chose, and the readiness check prints {"status":"ok"} (with n8n published on 127.0.0.1:5678 as in the self-host tutorial). Use /healthz/readiness, not /healthz: plain /healthz answers as soon as the web server is up, while readiness answers ok only once the database is connected and the new version's migrations have finished, which is what an update puts at risk. If it fails with an error, give it a minute and try again before you read the logs. Open the editor, run one workflow by hand, and check one active workflow's latest execution succeeded.
Step 7 · Roll back a failed update
Putting the old tag back is not enough on its own. The new version may have changed the database, and the old version cannot be trusted to run on a migrated database. n8n documents two ways back: revert the migrations one at a time with n8n db:revert, or restore the database from the backup you took before updating. Restoring the dump is the one to practise, because it puts everything back to a known state, as long as you restore into an empty database.
Restoring replaces everything in the n8n database with the pre-update copy. Executions, edits and new credentials made since that backup are lost. Stop n8n first so nothing writes to the database. Then drop and recreate the database rather than restoring over it: pg_restore --clean only removes objects that are in the dump, so tables and indexes the new version's migrations added would stay behind and break the next update attempt when it tries to create them again. --exit-on-error stops at the first problem, so a half-finished restore fails loudly instead of quietly:
cd ~/n8n
docker compose stop n8n
docker compose exec -T postgres sh -c 'dropdb -U "$POSTGRES_USER" --force "$POSTGRES_DB" && createdb -U "$POSTGRES_USER" "$POSTGRES_DB"'
docker compose exec -T postgres sh -c 'pg_restore -U "$POSTGRES_USER" -d "$POSTGRES_DB" --no-owner --exit-on-error' < "$BACKUP/n8n.dump"If either command prints an error, stop and fix it before you start n8n: starting the old version on a partly restored database makes things worse.
Change the image line in compose.yaml back to the old tag, n8nio/n8n:2.49.7, then start n8n again:
docker compose up -d n8n
docker compose logs --since 10m n8nCheck it worked
docker compose exec n8n n8n --version prints the old version, curl -fsS http://127.0.0.1:5678/healthz/readiness answers {"status":"ok"} (the restored database is connected and its migrations match the old version), and the workflows in the editor match what you had before the update. Record what failed and check the n8n community forum or GitHub issues for that version before you try again.
Next steps
- Set up the instance these commands assume: self-host n8n with Docker Compose and PostgreSQL.
- Schedule the database dump and practise a full database restore: back up and restore PostgreSQL with pg_dump.
- Give the studio's backups more than one home: back up a small team's files with the 3-2-1 rule.
- Build something worth backing up: build your first n8n workflow with a webhook trigger.
Related tutorials
- Self-host n8n with Docker Compose and PostgreSQLYou will run n8n 2.49 and PostgreSQL 18 from one compose.yaml on your own server, with secrets in a private env file, HTTPS through Caddy and an owner account ready for your first workflow.
- How to Back Up and Restore a PostgreSQL Database with pg_dumpYou will back up a PostgreSQL 18 database in three formats, restore it into a fresh database, check the copy matches, and schedule a nightly backup that deletes old files.
- Build your first n8n workflow with a webhook triggerYou will have a published n8n workflow that takes a website enquiry by POST, checks a secret header, rejects enquiries without an email address and answers with JSON.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials