{
  "name": "Automate N8N Workflows & Credentials Backup to S3 with Retention Management",
  "nodes": [
    {
      "id": "387e5dfd-f988-49df-a14e-4bc05d228861",
      "name": "Get many workflows",
      "type": "n8n-nodes-base.n8n",
      "position": [
        416,
        -96
      ],
      "parameters": {
        "filters": {},
        "requestOptions": {}
      },
      "typeVersion": 1
    },
    {
      "id": "e9fcf8bb-c91f-4aec-8449-9dbcc83f09ff",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "position": [
        144,
        96
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "19b52777-1679-4503-b8cc-293b668da0b0",
              "name": "bucketName",
              "type": "string",
              "value": "<YOUR BUCKET NAME HERE>"
            },
            {
              "id": "5b2ac2d4-8876-4d2f-9485-ecc4fa5604a6",
              "name": "timstamp",
              "type": "string",
              "value": "={{ $now.format('yyyy-MM-dd') }}"
            },
            {
              "id": "93766682-af35-40da-ba50-3c561345477a",
              "name": "retention",
              "type": "number",
              "value": 31
            },
            {
              "id": "9fa68ac0-cc4a-4747-80c7-f31c61e426dc",
              "name": "retentionUnit",
              "type": "string",
              "value": "days"
            },
            {
              "id": "78788eab-3e2f-4105-967f-017acea5aa11",
              "name": "tempCredentialBackupFile",
              "type": "string",
              "value": "/tmp/CredentialBackup.json"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a376ab24-6411-462f-9132-2e22f823c3b2",
      "name": "Extract Date",
      "type": "n8n-nodes-base.set",
      "position": [
        912,
        240
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "dba5ef9c-005b-448c-97df-6878af7c1bb4",
              "name": "date",
              "type": "string",
              "value": "={{ $json.LastModified.toDateTime().format('yyyy-MM-dd') }}"
            },
            {
              "id": "9e2cbb10-8e3e-459f-81b5-4bd80ff58b6e",
              "name": "key",
              "type": "string",
              "value": "={{ $json.Key }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e8f2e585-bf39-40ee-a91d-c17348708b28",
      "name": "Keep Outdated Backups",
      "type": "n8n-nodes-base.filter",
      "position": [
        1200,
        240
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "ab51911a-545b-465d-b182-32c43e5566f5",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.date }}",
              "rightValue": ""
            },
            {
              "id": "d1953bda-4da4-4ecf-acc7-a8d76aa1a489",
              "operator": {
                "type": "dateTime",
                "operation": "before"
              },
              "leftValue": "={{ $json.date.toDateTime() }}",
              "rightValue": "={{ $now.minus($('Config').first().json.retention, $('Config').first().json.retentionUnit) }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "f8eea97f-9d2d-4023-938e-c410978cb490",
      "name": "Delete Outdated Backups",
      "type": "n8n-nodes-base.s3",
      "position": [
        1424,
        240
      ],
      "parameters": {
        "fileKey": "={{ $json.key }}",
        "options": {},
        "operation": "delete",
        "bucketName": "={{ $('Config').first().json.bucketName }}"
      },
      "typeVersion": 1
    },
    {
      "id": "6457633f-b64f-4288-b7ee-102ef5592961",
      "name": "Export Credentials",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        368,
        592
      ],
      "parameters": {
        "command": "=n8n export:credentials --all --output={{ $json.tempCredentialBackupFile }}"
      },
      "typeVersion": 1
    },
    {
      "id": "969eea4c-d592-435e-8bd3-e39ad5112378",
      "name": "Load Credentials",
      "type": "n8n-nodes-base.readWriteFile",
      "position": [
        592,
        592
      ],
      "parameters": {
        "options": {},
        "fileSelector": "={{ $('Config').first().json.tempCredentialBackupFile }}"
      },
      "typeVersion": 1
    },
    {
      "id": "a340ac8c-ce0a-40f4-ab55-e90014878763",
      "name": "Delete Temporary File",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        1040,
        592
      ],
      "parameters": {
        "command": "=rm {{ $('Config').first().json.tempCredentialBackupFile }}"
      },
      "typeVersion": 1
    },
    {
      "id": "6d7e2501-fe77-4ed9-8f10-50bcafebaa6b",
      "name": "Store Credentials Backup",
      "type": "n8n-nodes-base.s3",
      "position": [
        816,
        592
      ],
      "parameters": {
        "fileName": "=credentials/{{ $('Config').first().json.timstamp }}.json",
        "operation": "upload",
        "bucketName": "={{ $('Config').first().json.bucketName }}",
        "additionalFields": {}
      },
      "typeVersion": 1
    },
    {
      "id": "68e5990e-9b17-4581-8498-3e656efbf611",
      "name": "Store Workflow Backup",
      "type": "n8n-nodes-base.s3",
      "position": [
        880,
        -96
      ],
      "parameters": {
        "tagsUi": {
          "tagsValues": [
            {
              "key": "name",
              "value": "={{ $json.name }}"
            }
          ]
        },
        "fileName": "=workflows/{{ $json.id }}/{{ $('Config').first().json.timstamp }}.json",
        "operation": "upload",
        "binaryData": false,
        "bucketName": "={{ $('Config').first().json.bucketName }}",
        "fileContent": "={{ JSON.stringify($json) }}",
        "additionalFields": {}
      },
      "typeVersion": 1
    },
    {
      "id": "d9403837-7a26-4d9b-9460-6cc2c62a625c",
      "name": "Daily Backup",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -80,
        96
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 23
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "d3feef71-cb5d-4986-b7e3-556c3ad4606c",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        816,
        -240
      ],
      "parameters": {
        "height": 320,
        "content": "## TODO\n- Set the credentials for your backup S3 bucket here."
      },
      "typeVersion": 1
    },
    {
      "id": "3d017215-bd7f-4a66-839c-6621f617aa2d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1344,
        112
      ],
      "parameters": {
        "height": 320,
        "content": "## TODO\n- Set the credentials for your backup S3 bucket here."
      },
      "typeVersion": 1
    },
    {
      "id": "94b6fc3c-7356-4038-ac3b-13ff55327a7f",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        752,
        464
      ],
      "parameters": {
        "height": 304,
        "content": "## TODO\n- Set the credentials for your backup S3 bucket here."
      },
      "typeVersion": 1
    },
    {
      "id": "c8d84477-1d0b-4521-9273-6190ccc9b19b",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        448,
        144
      ],
      "parameters": {
        "height": 288,
        "content": "## TODO\n- Set the credentials for your backup S3 bucket here."
      },
      "typeVersion": 1
    },
    {
      "id": "bb4f024b-715f-4dbc-8dcf-5a093863ea62",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        64,
        -176
      ],
      "parameters": {
        "height": 432,
        "content": "## TODO\n- Adjust retention (how long backups are kept, default: 31 days)\n- Configure the name of the bucket that shall be used for storing the backups."
      },
      "typeVersion": 1
    },
    {
      "id": "3742caa7-1b6d-4b48-ae8b-642a5576c4c7",
      "name": "Get Existing Backups",
      "type": "n8n-nodes-base.s3",
      "position": [
        528,
        240
      ],
      "parameters": {
        "resource": "bucket",
        "operation": "search",
        "returnAll": true,
        "bucketName": "={{ $json.bucketName }}",
        "additionalFields": {}
      },
      "typeVersion": 1
    },
    {
      "id": "2369ab66-4837-4a28-87ef-9479eaf47a3f",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -192,
        -176
      ],
      "parameters": {
        "height": 432,
        "content": "## TODO\n- Adjust the trigger to suit your schedule (default: daily execution at 11pm)\n- Remember to activate the workflow once all TODOs are resolved\n- Test the workflow by executing it."
      },
      "typeVersion": 1
    },
    {
      "id": "a4523efd-8786-4cbc-a5f2-51d11bd8282e",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        336,
        -240
      ],
      "parameters": {
        "height": 320,
        "content": "## TODO\n- Create an N8N API Key in the N8n settings\n- Configure it in the credentials of this node."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Config": {
      "main": [
        [
          {
            "node": "Get many workflows",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Existing Backups",
            "type": "main",
            "index": 0
          },
          {
            "node": "Export Credentials",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Backup": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Date": {
      "main": [
        [
          {
            "node": "Keep Outdated Backups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load Credentials": {
      "main": [
        [
          {
            "node": "Store Credentials Backup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Export Credentials": {
      "main": [
        [
          {
            "node": "Load Credentials",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many workflows": {
      "main": [
        [
          {
            "node": "Store Workflow Backup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Existing Backups": {
      "main": [
        [
          {
            "node": "Extract Date",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Keep Outdated Backups": {
      "main": [
        [
          {
            "node": "Delete Outdated Backups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store Credentials Backup": {
      "main": [
        [
          {
            "node": "Delete Temporary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}