{
  "id": "fQdd3kaxfpdJeKBB",
  "meta": {
    "templateId": "4338"
  },
  "name": "N8N Backup Flow to Nextcloud (7-Day Retention)",
  "tags": [],
  "nodes": [
    {
      "id": "5a95d578-fa4a-400f-9f4c-e0263484831e",
      "name": "On clicking 'execute'",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        416,
        432
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "502e7322-74c1-4fce-933d-73628215adb6",
      "name": "n8n",
      "type": "n8n-nodes-base.n8n",
      "position": [
        1008,
        544
      ],
      "parameters": {
        "filters": {},
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "2a7c2c11-30ab-4e4b-b1c8-bd5a4ecaac11",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1280,
        544
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "8e3dd7ae-18dd-4389-920e-ff37a83685c3",
      "name": "Backups",
      "type": "n8n-nodes-base.sort",
      "notes": "Backup List",
      "position": [
        1200,
        352
      ],
      "parameters": {
        "options": {},
        "sortFieldsUi": {
          "sortField": [
            {
              "order": "descending",
              "fieldName": "=path"
            }
          ]
        }
      },
      "notesInFlow": true,
      "typeVersion": 1
    },
    {
      "id": "e1e8f61c-a6fb-4982-a141-6326bdc03df1",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        416,
        640
      ],
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "5ec178d4-99ac-4dc0-91ff-bdd4e5a18212",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        240
      ],
      "parameters": {
        "color": 5,
        "width": 1520,
        "height": 680,
        "content": "## N8N Backup Flow\n### - Create /N8N-Backup Directory manual in Nextcloud\n### - Edit Backup Path"
      },
      "typeVersion": 1
    },
    {
      "id": "d8aa50c8-123b-44be-8777-53bdb50557d9",
      "name": "Backup Path",
      "type": "n8n-nodes-base.set",
      "notes": "Backupdirectory",
      "position": [
        608,
        544
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "bd7c7d59-267d-4201-bfe9-e9ef478557a2",
              "name": "backup",
              "type": "string",
              "value": "=/N8N-Backup/S4/{{ $now.format('yyyy-MM-dd_HHmmss') }}"
            }
          ]
        }
      },
      "executeOnce": true,
      "notesInFlow": true,
      "typeVersion": 3.4
    },
    {
      "id": "f808618a-6677-4fc9-8227-f5e5acc96bdf",
      "name": "Nextcloud Directory",
      "type": "n8n-nodes-base.nextCloud",
      "notes": "Create Directory",
      "position": [
        816,
        544
      ],
      "parameters": {
        "path": "={{ $json.backup }}",
        "resource": "folder"
      },
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 1
    },
    {
      "id": "1f179a8c-0af6-4510-b9aa-f928b91525f4",
      "name": "Nextcloud Upload",
      "type": "n8n-nodes-base.nextCloud",
      "position": [
        1728,
        560
      ],
      "parameters": {
        "path": "={{ $('Backup Path').item.json.backup }}/{{ $binary.data.fileName }}",
        "binaryDataUpload": true
      },
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "efaa98ff-efe8-4d2c-8496-21f259620f87",
      "name": "Nextcloud List Dir",
      "type": "n8n-nodes-base.nextCloud",
      "position": [
        1008,
        352
      ],
      "parameters": {
        "path": "/N8N-Backup",
        "resource": "folder",
        "operation": "list"
      },
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      },
      "executeOnce": true,
      "retryOnFail": false,
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "id": "aeb2430a-c6af-4583-97ea-1fcbdc596e6a",
      "name": "Limits Backups",
      "type": "n8n-nodes-base.code",
      "notes": "Retention 7 ",
      "position": [
        1392,
        352
      ],
      "parameters": {
        "jsCode": "// Behalte nur Items ab Index 7 (\u00e4ltere Eintr\u00e4ge)\nreturn items.slice(7);\n"
      },
      "notesInFlow": true,
      "typeVersion": 2
    },
    {
      "id": "650e7660-3c1c-48f0-983e-76d9b76f8857",
      "name": "Nextcloud - Delete old backups",
      "type": "n8n-nodes-base.nextCloud",
      "notes": "Delete old Backups",
      "position": [
        1600,
        352
      ],
      "parameters": {
        "path": "={{ $json.path }}",
        "resource": "folder",
        "operation": "delete"
      },
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "retryOnFail": false,
      "typeVersion": 1
    },
    {
      "id": "8037f92d-8572-4753-b1a8-531afe980858",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        1504,
        560
      ],
      "parameters": {
        "jsCode": "return items.map(item => {\n  // Sonderzeichen entfernen und formatieren\n  const cleanName = item.json.name\n    .replace(/[^\\w\\s-]/g, '')\n    .replace(/\\s+/g, '_')\n    .substring(0, 50); // Maximale L\u00e4nge begrenzen\n\n  const fileName = `${cleanName || 'backup'}.json`;\n  const jsonData = JSON.stringify(item.json);\n  \n  // WICHTIG: Setze den Dateinamen auch im JSON f\u00fcr den Pfad-Zugriff\n  item.json.fileName = fileName;\n\n  item.binary = {\n    data: {\n      data: Buffer.from(jsonData).toString('base64'),\n      mimeType: 'application/json',\n      fileName: fileName\n    }\n  };\n  return item;\n});"
      },
      "typeVersion": 2
    }
  ],
  "active": false,
  "settings": {},
  "versionId": "fd5ad770-c790-4f5c-a8d3-f3707de38eda",
  "connections": {
    "n8n": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Nextcloud Upload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Backups": {
      "main": [
        [
          {
            "node": "Limits Backups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Backup Path": {
      "main": [
        [
          {
            "node": "Nextcloud Directory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Limits Backups": {
      "main": [
        [
          {
            "node": "Nextcloud - Delete old backups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Nextcloud List Dir",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Nextcloud Upload": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Backup Path",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Nextcloud List Dir": {
      "main": [
        [
          {
            "node": "Backups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Nextcloud Directory": {
      "main": [
        [
          {
            "node": "n8n",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On clicking 'execute'": {
      "main": [
        [
          {
            "node": "Backup Path",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Nextcloud - Delete old backups": {
      "main": [
        []
      ]
    }
  }
}