{ "id": 4, "name": "Back Up Your n8n Workflows To Github", "active": false, "nodes": [ { "parameters": {}, "name": "Start", "type": "n8n-nodes-base.start", "position": [ 240, 300 ], "typeVersion": 1, "id": "2798d936-e742-4d37-ac2a-22ebf242f6c7" }, { "parameters": {}, "name": "Merge", "type": "n8n-nodes-base.merge", "position": [ 1900, 420 ], "typeVersion": 1, "id": "0c9059b6-a6cc-4f52-b621-4fb8f25a81cb" }, { "parameters": { "dataType": "string", "value1": "={{$json[\"github_status\"]}}", "rules": { "rules": [ { "value2": "same" }, { "value2": "different", "output": 1 }, { "value2": "new", "output": 2 } ] } }, "name": "github_status", "type": "n8n-nodes-base.switch", "position": [ 2200, 420 ], "typeVersion": 1, "id": "384f7893-90f5-453a-897d-cdd60c1a6a65" }, { "parameters": {}, "name": "same", "type": "n8n-nodes-base.noOp", "position": [ 2400, 240 ], "typeVersion": 1, "id": "7fe302c9-69b0-4c97-9ed2-6e378a652247" }, { "parameters": {}, "name": "different", "type": "n8n-nodes-base.noOp", "position": [ 2400, 420 ], "typeVersion": 1, "id": "87e5a717-9a8e-4717-9ea6-b9e6d12cf08a" }, { "parameters": {}, "name": "new", "type": "n8n-nodes-base.noOp", "position": [ 2400, 580 ], "typeVersion": 1, "id": "c1c9ad9c-ccd8-4bcf-98d2-267f7fedfed1" }, { "parameters": { "resource": "file", "operation": "edit", "owner": "={{$node[\"Globals\"].json[\"repo\"][\"owner\"]}}", "repository": "={{$node[\"Globals\"].json[\"repo\"][\"name\"]}}", "filePath": "={{$node[\"Globals\"].json[\"repo\"][\"path\"]}}{{JSON.parse($node[\"Move Binary Data\"].json[\"data\"]).name}}.json", "fileContent": "={{$node[\"isDiffOrNew\"].json[\"n8n_data_stringy\"]}}", "commitMessage": "=[N8N Backup] {{JSON.parse($node[\"Move Binary Data\"].json[\"data\"]).name}}.json ({{$json[\"github_status\"]}})" }, "name": "GitHub Edit", "type": "n8n-nodes-base.github", "position": [ 2600, 420 ], "typeVersion": 1, "id": "9bb92059-998a-43c6-bd05-8ce0069d169a", "credentials": { "githubApi": { "id": "3", "name": "GitHub account" } } }, { "parameters": { "resource": "file", "owner": "={{$node[\"Globals\"].json[\"repo\"][\"owner\"]}}", "repository": "={{$node[\"Globals\"].json[\"repo\"][\"name\"]}}", "filePath": "={{$node[\"Globals\"].json[\"repo\"][\"path\"]}}{{JSON.parse($node[\"Move Binary Data\"].json[\"data\"]).name}}.json", "fileContent": "={{$node[\"isDiffOrNew\"].json[\"n8n_data_stringy\"]}}", "commitMessage": "=[N8N Backup] {{JSON.parse($node[\"Move Binary Data\"].json[\"data\"]).name}}.json ({{$json[\"github_status\"]}})" }, "name": "GitHub Create", "type": "n8n-nodes-base.github", "position": [ 2600, 580 ], "typeVersion": 1, "id": "338768fd-5856-45e1-bdff-9cc63abdf1a9", "credentials": { "githubApi": { "id": "3", "name": "GitHub account" } } }, { "parameters": { "functionCode": "// File Returned with Content\nif (Object.keys(items[0].json).includes(\"content\")) {\n // Get JSON Objects\n var origWorkflow = JSON.parse(Buffer.from(items[0].json.content, 'base64'));\n var n8nWorkflow = items[1].json.data;\n var orderedOriginal = JSON.stringify(origWorkflow, null, 2);\n \n // Determine Difference\n if ( orderedOriginal === n8nWorkflow ) {\n items[0].json.github_status = \"same\";\n items[0].json.content_decoded = orderedOriginal;\n } else {\n items[0].json.github_status = \"different\";\n items[0].json.content_decoded = orderedOriginal;\n items[0].json.n8n_data_stringy = n8nWorkflow;\n }\n// No File Returned / New Workflow\n} else {\n // Order JSON Object\n var n8nWorkflow = (items[1].json.data);\n \n // Proper Formatting\n items[0].json.github_status = \"new\";\n items[0].json.n8n_data_stringy = n8nWorkflow;\n}\n\n// Return Items\nreturn items;" }, "name": "isDiffOrNew", "type": "n8n-nodes-base.function", "position": [ 2060, 420 ], "typeVersion": 1, "id": "5f7ca7f7-bf69-440e-ac93-e08814c6948c" }, { "parameters": { "triggerTimes": { "item": [ { "mode": "everyX" } ] } }, "name": "Every 2 hours", "type": "n8n-nodes-base.cron", "position": [ 240, 580 ], "typeVersion": 1, "id": "0140784d-9d9d-4654-9eb1-306caf04e763" }, { "parameters": { "filePath": "={{$json[\"filename\"]}}" }, "name": "Read Binary File", "type": "n8n-nodes-base.readBinaryFile", "position": [ 1300, 440 ], "typeVersion": 1, "id": "e23e4247-1ca6-4f6f-a459-c5236ccb3c78" }, { "parameters": { "setAllData": false, "options": {} }, "name": "Move Binary Data", "type": "n8n-nodes-base.moveBinaryData", "position": [ 1500, 440 ], "typeVersion": 1, "id": "0d5041fe-b79c-428f-983f-77220dff7275" }, { "parameters": { "values": { "string": [ { "name": "repo.owner", "value": "thethanghn" }, { "name": "repo.name", "value": "n8n-workflows" }, { "name": "repo.path", "value": "workflows/" }, { "name": "backupFolder", "value": "/Users/thangnguyen/projects/n8n/" } ] }, "options": {} }, "name": "Globals", "type": "n8n-nodes-base.set", "position": [ 400, 440 ], "typeVersion": 1, "id": "16a4e6c7-1935-4722-8d02-451ba90f7e10" }, { "parameters": { "command": "=n8n export:workflow --backup --output={{$json[\"backupFolder\"]}}" }, "name": "Run CLI to export Workflows", "type": "n8n-nodes-base.executeCommand", "position": [ 560, 440 ], "typeVersion": 1, "id": "b37ae188-6ee7-4dea-9997-5757a487354d" }, { "parameters": { "command": "=ls -1 {{$node[\"Globals\"].json[\"backupFolder\"]}}" }, "name": "List filenames", "type": "n8n-nodes-base.executeCommand", "position": [ 740, 440 ], "typeVersion": 1, "id": "03f50403-3e29-4e4f-a47c-58349f464cc6" }, { "parameters": { "functionCode": "const newItems = [];\nfor (file of items[0].json.stdout.split('\\n')) {\n newItems.push({\n json: {\n filename: $node[\"Globals\"].json[\"backupFolder\"] + file\n }\n });\n}\nreturn newItems;" }, "name": "Convert filenames to objects with path", "type": "n8n-nodes-base.function", "position": [ 920, 440 ], "typeVersion": 1, "id": "b907d698-869c-42ca-8594-7793f2014f97" }, { "parameters": { "batchSize": 1, "options": {} }, "name": "One at a time", "type": "n8n-nodes-base.splitInBatches", "position": [ 1100, 440 ], "typeVersion": 1, "id": "d274d5c4-f2e6-4deb-b74c-ba2c547e23fd" }, { "parameters": { "resource": "file", "operation": "get", "owner": "={{$node[\"Globals\"].json[\"repo\"][\"owner\"]}}", "repository": "={{$node[\"Globals\"].json[\"repo\"][\"name\"]}}", "filePath": "={{$node[\"Globals\"].json[\"repo\"][\"path\"]}}{{JSON.parse($json[\"data\"]).name}}.json", "asBinaryProperty": false, "additionalParameters": {} }, "name": "GitHub", "type": "n8n-nodes-base.github", "position": [ 1700, 240 ], "typeVersion": 1, "alwaysOutputData": true, "id": "31a8177b-2cdb-4b41-821f-484d103c6900", "credentials": { "githubApi": { "id": "3", "name": "GitHub account" } }, "continueOnFail": true } ], "connections": { "new": { "main": [ [ { "node": "GitHub Create", "type": "main", "index": 0 } ] ] }, "same": { "main": [ [ { "node": "One at a time", "type": "main", "index": 0 } ] ] }, "Merge": { "main": [ [ { "node": "isDiffOrNew", "type": "main", "index": 0 } ] ] }, "Start": { "main": [ [ { "node": "Globals", "type": "main", "index": 0 } ] ] }, "GitHub": { "main": [ [ { "node": "Merge", "type": "main", "index": 0 } ] ] }, "Globals": { "main": [ [ { "node": "Run CLI to export Workflows", "type": "main", "index": 0 } ] ] }, "different": { "main": [ [ { "node": "GitHub Edit", "type": "main", "index": 0 } ] ] }, "GitHub Edit": { "main": [ [ { "node": "One at a time", "type": "main", "index": 0 } ] ] }, "isDiffOrNew": { "main": [ [ { "node": "github_status", "type": "main", "index": 0 } ] ] }, "Every 2 hours": { "main": [ [ { "node": "Globals", "type": "main", "index": 0 } ] ] }, "GitHub Create": { "main": [ [ { "node": "One at a time", "type": "main", "index": 0 } ] ] }, "One at a time": { "main": [ [ { "node": "Read Binary File", "type": "main", "index": 0 } ] ] }, "github_status": { "main": [ [ { "node": "same", "type": "main", "index": 0 } ], [ { "node": "different", "type": "main", "index": 0 } ], [ { "node": "new", "type": "main", "index": 0 } ] ] }, "List filenames": { "main": [ [ { "node": "Convert filenames to objects with path", "type": "main", "index": 0 } ] ] }, "Move Binary Data": { "main": [ [ { "node": "GitHub", "type": "main", "index": 0 }, { "node": "Merge", "type": "main", "index": 1 } ] ] }, "Read Binary File": { "main": [ [ { "node": "Move Binary Data", "type": "main", "index": 0 } ] ] }, "Run CLI to export Workflows": { "main": [ [ { "node": "List filenames", "type": "main", "index": 0 } ] ] }, "Convert filenames to objects with path": { "main": [ [ { "node": "One at a time", "type": "main", "index": 0 } ] ] } }, "createdAt": "2022-09-01T06:42:50.621Z", "updatedAt": "2022-09-01T06:47:02.287Z", "settings": {}, "staticData": null, "pinData": {}, "tags": [] }