Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Last active October 14, 2025 18:57
Show Gist options
  • Select an option

  • Save Rugby-Ball/e10dea81444ae31a22fe133ecd10d926 to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/e10dea81444ae31a22fe133ecd10d926 to your computer and use it in GitHub Desktop.
Backs upn8n WorkFlows, Postgres DB, and credentials every day at 4am, and purges any older than 10 days. #n8n #Backup #Public
{
"name": "Daily n8n + Postgres Backup (4AM)",
"nodes": [
{
"parameters": {
"rule": {
"interval": [
{}
]
}
},
"id": "fb0ec52f-3c61-4f6e-9a0d-2e5b8054ef8f",
"name": "Schedule Trigger",
"type": "n8n-nodes-base.scheduleTrigger",
"typeVersion": 1,
"position": [
-1104,
80
]
},
{
"parameters": {
"value": "={{ $json.timestamp.split('.')[0] + 'Z' }}",
"dataPropertyName": "ts",
"custom": true,
"toFormat": "YYYYMMDD_HHmmss",
"options": {
"fromFormat": ""
}
},
"id": "24b1db5d-a0f8-45ec-b235-08ee1c6d355a",
"name": "Date & Time",
"type": "n8n-nodes-base.dateTime",
"typeVersion": 1,
"position": [
-848,
80
]
},
{
"parameters": {
"keepOnlySet": true,
"values": {
"string": [
{
"name": "BACKUP_DIR",
"value": "=/files/backups/{{$json.ts}}"
}
]
},
"options": {}
},
"id": "fc884ded-1bef-4c7c-a71e-2842fa41d2d9",
"name": "Set Backup Dir",
"type": "n8n-nodes-base.set",
"typeVersion": 2,
"position": [
-576,
80
]
},
{
"parameters": {
"command": "=n8n export:workflow --all --pretty --output '{{$json.stdout}}/workflows.json'"
},
"id": "0e637c08-9612-4a96-bdba-c686da9c55e4",
"name": "Export Workflows",
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
-80,
-48
]
},
{
"parameters": {
"command": "=n8n export:credentials --all --decrypted --output \"{{ $('Set Backup Dir').item.json.BACKUP_DIR }}/credentials.json\""
},
"id": "70f4aa70-bf54-4117-8a73-6acf5a2e325d",
"name": "Export Credentials",
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
96,
176
],
"onError": "continueRegularOutput"
},
{
"parameters": {
"command": "=PGPASSWORD=\"$DB_POSTGRESDB_PASSWORD\" pg_dump -Fc -h \"$DB_POSTGRESDB_HOST\" -p \"$DB_POSTGRESDB_PORT\" -U \"$DB_POSTGRESDB_USER\" -d \"$DB_POSTGRESDB_DATABASE\" -f \"{{$json.stdout}}/postgres.dump\""
},
"id": "7c8f1250-4620-43ff-80d3-3c5ea6ab4e1c",
"name": "Dump Postgres",
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
-64,
176
]
},
{
"parameters": {
"command": "={\n (n8n --version || true) | awk '{print \"n8n_version=\"$0}';\n printf \"ts=%s\\n\" \"{{$node[\"Date & Time\"].json[\"ts\"]}}\";\n printf \"encryption_key_sha256=\";\n printf \"%s\" \"${N8N_ENCRYPTION_KEY}\" | sha256sum | awk '{print $1}';\n echo;\n} > \"{{$node[\"Set Backup Dir\"].json[\"BACKUP_DIR\"]}}/manifest.txt\"\n"
},
"id": "7c2d719b-b777-4437-8a94-697e6ab4dd4b",
"name": "Write Manifest",
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
576,
160
]
},
{
"parameters": {
"command": "set -e; ls -1dt /files/backups/20* 2>/dev/null | tail -n +11 | xargs -r rm -rf"
},
"id": "c46dcdeb-f703-4f28-82da-8ba61bd5a072",
"name": "Prune Old Backups",
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
768,
160
]
},
{
"parameters": {
"command": "=mkdir -p '{{$json.BACKUP_DIR}}' && echo \"{{$json.BACKUP_DIR}}\""
},
"id": "9346707d-8122-4adc-81e4-c8eede5254ad",
"name": "Make_Folder",
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
-320,
80
]
},
{
"parameters": {
"command": "=echo [] > \"{{ $('Set Backup Dir').item.json.BACKUP_DIR }}/credentials.json\"\n"
},
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
416,
272
],
"id": "8517706d-c8d0-40d1-9342-755070d3742e",
"name": "No_Credentials_catch"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "8a397f55-7780-40d2-bc0e-dbf178c5e23c",
"leftValue": "={{ $json.exitCode }}",
"rightValue": 0,
"operator": {
"type": "number",
"operation": "equals"
}
}
],
"combinator": "or"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
240,
176
],
"id": "27b154aa-e62b-4ce5-98c2-0b7ed85f418c",
"name": "if-error"
}
],
"pinData": {},
"connections": {
"Schedule Trigger": {
"main": [
[
{
"node": "Date & Time",
"type": "main",
"index": 0
}
]
]
},
"Date & Time": {
"main": [
[
{
"node": "Set Backup Dir",
"type": "main",
"index": 0
}
]
]
},
"Set Backup Dir": {
"main": [
[
{
"node": "Make_Folder",
"type": "main",
"index": 0
}
]
]
},
"Dump Postgres": {
"main": [
[
{
"node": "Export Credentials",
"type": "main",
"index": 0
}
]
]
},
"Write Manifest": {
"main": [
[
{
"node": "Prune Old Backups",
"type": "main",
"index": 0
}
]
]
},
"Export Workflows": {
"main": [
[]
]
},
"Make_Folder": {
"main": [
[
{
"node": "Export Workflows",
"type": "main",
"index": 0
},
{
"node": "Dump Postgres",
"type": "main",
"index": 0
}
]
]
},
"Export Credentials": {
"main": [
[
{
"node": "if-error",
"type": "main",
"index": 0
}
]
]
},
"No_Credentials_catch": {
"main": [
[
{
"node": "Write Manifest",
"type": "main",
"index": 0
}
]
]
},
"if-error": {
"main": [
[
{
"node": "Write Manifest",
"type": "main",
"index": 0
}
],
[
{
"node": "No_Credentials_catch",
"type": "main",
"index": 0
}
]
]
}
},
"active": true,
"settings": {
"executionOrder": "v1"
},
"versionId": "444fd6fd-c281-4335-91e6-6ebf0964417b",
"meta": {
"instanceId": "5ded2e780f36a846b6626b720138a6a2ebc5762e3dd8672b56b270b5bcfb5269"
},
"id": "P0W67yk4ubaFtZEs",
"tags": []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment