AutomationFlowsData & Sheets › Daily N8n + Postgres Backup (4am)

Daily N8n + Postgres Backup (4am)

Daily n8n + Postgres Backup (4AM). Uses executeCommand. Scheduled trigger; 11 nodes.

Cron / scheduled trigger★★★★☆ complexity11 nodesExecute Command
Data & Sheets Trigger: Cron / scheduled Nodes: 11 Complexity: ★★★★☆ Added:

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "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"
    }
  ],
  "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",
  "id": "P0W67yk4ubaFtZEs",
  "tags": []
}
Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

Daily n8n + Postgres Backup (4AM). Uses executeCommand. Scheduled trigger; 11 nodes.

Source: https://gist.github.com/Rugby-Ball/e10dea81444ae31a22fe133ecd10d926 — original creator credit. Request a take-down →

More Data & Sheets workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

Data & Sheets

Automated Knowledge Management Backup & Recovery. Uses executeCommand, postgres, httpRequest, googleDrive. Scheduled trigger; 30 nodes.

Execute Command, Postgres, HTTP Request +1
Data & Sheets

Marcel - Sauvegarde Automatique. Uses executeCommand, awsS3, postgres. Scheduled trigger; 17 nodes.

Execute Command, AWS S3, Postgres
Data & Sheets

Bussola Publica - Ingestao Diaria 06h. Uses executeCommand, postgres, emailSend. Scheduled trigger; 7 nodes.

Execute Command, Postgres, Email Send
Data & Sheets

Bussola Publica - Ingestao Diaria 06h. Uses executeCommand, postgres, emailSend. Scheduled trigger; 7 nodes.

Execute Command, Postgres, Email Send
Data & Sheets

Project Genji - Complete Data Pipeline. Uses schedule, executeCommand, postgres. Scheduled trigger; 4 nodes.

Schedule, Execute Command, Postgres