8
n8n 中文网amn8n.com

自动化工作流备份至 Google Drive 并智能清理

高级

这是一个DevOps, Multimodal AI领域的自动化工作流,包含 17 个节点。主要使用 N8n, Set, Code, GoogleDrive, ConvertToFile 等节点。 自动化工作流备份至 Google Drive 并智能清理

前置要求
  • Google Drive API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "c582d54231d7262c638a00ee408faa972b8030c20ecfa02b3d445f84745245ff",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "b7a4ebea-711b-473d-b18a-7bf84a888a17",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1456,
        -72
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 3
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "c7d51e5b-0add-4c66-8d85-e98d3a09f72e",
      "name": "手动触发",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -1456,
        -264
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "73b8a7f5-d038-4366-b30b-a92195db3a35",
      "name": "搜索文件和文件夹",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -16,
        -480
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "id",
            "value": "={{ $('CONFIG - Set your variables here').item.json.parentFolderId }}"
          }
        },
        "options": {},
        "resource": "fileFolder",
        "returnAll": true,
        "queryString": "Workflows backup - "
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "ardYGrL3hsVqtoGo",
          "name": "Google Drive"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "adc967fc-746c-462c-9de8-f823100d833d",
      "name": "开始清理(运行一次)",
      "type": "n8n-nodes-base.set",
      "position": [
        -240,
        -480
      ],
      "parameters": {
        "options": {}
      },
      "executeOnce": true,
      "typeVersion": 3.4
    },
    {
      "id": "1f4ad497-fbf8-4053-80fc-f22c9200370c",
      "name": "排序并隔离旧文件夹",
      "type": "n8n-nodes-base.code",
      "position": [
        208,
        -480
      ],
      "parameters": {
        "jsCode": "// Get the number of backups to keep from the config node\nconst backupsToKeep = $json.backupsToKeep;\n\n// 'items' contains the list of folders from the previous node\nconst folders = $input.all(); \nconst sortedFolders = folders.map(item => item.json);\n\n// 1. Sort the folders from newest to oldest\nsortedFolders.sort((a, b) => new Date(b.createdTime) - new Date(a.createdTime));\n\n// 2. Get only the excess folders to delete\nconst foldersToDelete = sortedFolders.slice(backupsToKeep);\n\n// 3. Return the final list to be passed to the loop\nreturn foldersToDelete;"
      },
      "typeVersion": 2
    },
    {
      "id": "ff6c6c87-2751-4429-a196-edc65f898e43",
      "name": "循环遍历要删除的文件夹",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        432,
        -480
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "7669d1df-6225-47aa-841b-c14f48cbd2e3",
      "name": "循环遍历每个工作流",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -560,
        -144
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "c7a911aa-e8a2-42a3-a64b-5c2c4318720a",
      "name": "获取所有 n8n 工作流",
      "type": "n8n-nodes-base.n8n",
      "position": [
        -784,
        -144
      ],
      "parameters": {
        "filters": {},
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "id": "iV99kO3Ts0Yartt4",
          "name": "N8n"
        }
      },
      "executeOnce": false,
      "retryOnFail": false,
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "id": "20acb740-7157-4e30-bd4d-04b8663a8f8d",
      "name": "创建新备份文件夹",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -1008,
        -144
      ],
      "parameters": {
        "name": "=Workflows backup - {{ $now.format('yyyy-MM-dd') }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('CONFIG - Set your variables here').item.json.parentFolderId }}"
        },
        "resource": "folder"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "ardYGrL3hsVqtoGo",
          "name": "Google Drive"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "82b9a618-2765-4c53-baa7-66a025808c2d",
      "name": "将工作流转换为文件",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        -240,
        16
      ],
      "parameters": {
        "options": {
          "format": true,
          "fileName": "={{ $json.name + \".json\" }} "
        },
        "operation": "toJson"
      },
      "typeVersion": 1.1
    },
    {
      "id": "1f14d31c-9ab6-4b4f-b054-a02f1b75336f",
      "name": "将工作流上传到 Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -16,
        16
      ],
      "parameters": {
        "name": "={{ $('Loop over each workflow').item.json.name + \".json\" }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive",
          "cachedResultUrl": "https://drive.google.com/drive/my-drive",
          "cachedResultName": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Create new backup folder').item.json.id }}"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "ardYGrL3hsVqtoGo",
          "name": "Google Drive"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "db4893e5-8a63-4d3d-9067-6a6d68ed3119",
      "name": "删除旧文件夹",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        656,
        -480
      ],
      "parameters": {
        "options": {},
        "resource": "folder",
        "operation": "deleteFolder",
        "folderNoRootId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "ardYGrL3hsVqtoGo",
          "name": "Google Drive"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "d6769f88-ffc4-4c1d-b8be-082109b12dde",
      "name": "配置 - 在此设置变量",
      "type": "n8n-nodes-base.set",
      "position": [
        -1232,
        -144
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5da1b756-88b7-4ba2-bc87-0ea4298b78ce",
              "name": "parentFolderId",
              "type": "string",
              "value": "PASTE_YOUR_GOOGLE_DRIVE_PARENT_FOLDER_ID_HERE"
            },
            {
              "id": "79da4836-d768-4b7d-b64f-bc820583613a",
              "name": "backupsToKeep",
              "type": "number",
              "value": 30
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "06268a9d-854e-47ee-b6f5-38c8dd57ee20",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2112,
        -336
      ],
      "parameters": {
        "width": 560,
        "height": 432,
        "content": "## 如何找到您的 Google Drive 文件夹 ID"
      },
      "typeVersion": 1
    },
    {
      "id": "1907cd86-02d6-473a-a605-4518d83c2b1b",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        -608
      ],
      "parameters": {
        "color": 7,
        "width": 1168,
        "height": 336,
        "content": "## 清理逻辑:查找并删除旧备份文件夹"
      },
      "typeVersion": 1
    },
    {
      "id": "dcad82b1-69e9-484f-8d06-cadc311a5ac7",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 544,
        "height": 432,
        "content": "## 备份逻辑:获取所有工作流并将其上传到新的每日文件夹"
      },
      "typeVersion": 1
    },
    {
      "id": "fb976047-3811-437a-b19c-de8def3fe06c",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2736,
        -560
      ],
      "parameters": {
        "width": 560,
        "height": 848,
        "content": "## 将 n8n 工作流备份到 Google Drive 并自动清理"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Manual trigger": {
      "main": [
        [
          {
            "node": "CONFIG - Set your variables here",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "CONFIG - Set your variables here",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete old folder": {
      "main": [
        [
          {
            "node": "Loop over folders to delete",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get all n8n workflows": {
      "main": [
        [
          {
            "node": "Loop over each workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop over each workflow": {
      "main": [
        [
          {
            "node": "Start cleanup (run once)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Convert workflow to file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert workflow to file": {
      "main": [
        [
          {
            "node": "Upload workflow to Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create new backup folder": {
      "main": [
        [
          {
            "node": "Get all n8n workflows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search files and folders": {
      "main": [
        [
          {
            "node": "Sort and isolate old folders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start cleanup (run once)": {
      "main": [
        [
          {
            "node": "Search files and folders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop over folders to delete": {
      "main": [
        [],
        [
          {
            "node": "Delete old folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sort and isolate old folders": {
      "main": [
        [
          {
            "node": "Loop over folders to delete",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload workflow to Google Drive": {
      "main": [
        [
          {
            "node": "Loop over each workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CONFIG - Set your variables here": {
      "main": [
        [
          {
            "node": "Create new backup folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 开发运维, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量17
分类2
节点类型9
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

外部链接
在 n8n.io 查看

分享此工作流