8
n8n 中文网amn8n.com

基于密码认证和警报系统的自动化Rsync备份

高级

这是一个自动化工作流,包含 21 个节点。主要使用 If, Set, ManualTrigger, ExecuteCommand, ScheduleTrigger 等节点。 密码认证与警报系统的自动化Rsync备份

前置要求
  • 无特殊前置要求,导入即可使用

分类

-
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "JxrInAO8OLCjxVC5",
  "meta": {
    "instanceId": "5c7ce220523e8664f49208a8be668a8dc6fab5f747ce4de865fa1309727919f1",
    "templateCredsSetupCompleted": true
  },
  "name": "基于密码认证和警报系统的自动化 Rsync 备份",
  "tags": [],
  "nodes": [
    {
      "id": "5c38117c-4f04-4033-a570-cc3d55e78f17",
      "name": "手动触发器",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -2800,
        224
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "f4d96789-441a-4152-b4d8-6922581dfe5b",
      "name": "服务器参数",
      "type": "n8n-nodes-base.set",
      "position": [
        -2576,
        224
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "source_host",
              "value": "source_host_ip"
            },
            {
              "name": "source_port",
              "value": "source_host_ssh_port"
            },
            {
              "name": "source_user",
              "value": "source_user_username"
            },
            {
              "name": "source_password",
              "value": "=source_user_password"
            },
            {
              "name": "source_folder",
              "value": "source_path_on_the_source_host"
            },
            {
              "name": "target_host",
              "value": "target_host_ip"
            },
            {
              "name": "target_port",
              "value": "target_host_ssh_port"
            },
            {
              "name": "target_user",
              "value": "target_user_username"
            },
            {
              "name": "target_password",
              "value": "=target_user_password"
            },
            {
              "name": "target_folder",
              "value": "target_path_on_the_target_host"
            },
            {
              "name": "rsync_options",
              "value": "-avz --delete"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "2856d809-25ad-46ba-8056-84e78c7fd66c",
      "name": "检查本地 Sshpass",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        -2352,
        224
      ],
      "parameters": {
        "command": "which sshpass"
      },
      "typeVersion": 1,
      "continueOnFail": true
    },
    {
      "id": "98869a56-7d59-4055-bbf8-d437f6d7ca12",
      "name": "检查本地Sshpass是否存在",
      "type": "n8n-nodes-base.if",
      "position": [
        -2128,
        224
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.exitCode }}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5fdd117b-5e40-4f21-987c-7ca7fb5b2c8b",
      "name": "本地是否已安装?",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        -1920,
        320
      ],
      "parameters": {
        "command": "=if command -v apt-get &> /dev/null; then\n  apt-get update && apt-get install -y sshpass rsync\nelif command -v yum &> /dev/null; then\n  yum install -y epel-release && yum install -y sshpass rsync\nelif command -v dnf &> /dev/null; then\n  dnf install -y sshpass rsync\nelif command -v apk &> /dev/null; then\n  apk add sshpass rsync\nelse\n  echo \"Unsupported package manager\"\n  exit 1\nfi"
      },
      "typeVersion": 1
    },
    {
      "id": "2e5c2163-e384-4488-9fb6-68d74ce3e932",
      "name": "检查源服务器 Sshpass",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        -1696,
        224
      ],
      "parameters": {
        "command": "=sshpass -p '{{ $('Server Parameters').item.json.source_password }}' ssh -o StrictHostKeyChecking=no -p {{ $('Server Parameters').item.json.source_port }} {{ $('Server Parameters').item.json.source_user }}@{{ $('Server Parameters').item.json.source_host }} \"which sshpass\""
      },
      "typeVersion": 1,
      "continueOnFail": true
    },
    {
      "id": "5916c184-5925-4fb4-8c8e-accc74e4df29",
      "name": "源服务器是否已安装?",
      "type": "n8n-nodes-base.if",
      "position": [
        -1472,
        224
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.exitCode }}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b302372f-007c-4afa-89e1-a453e03064c8",
      "name": "在源服务器安装 Sshpass",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        -1248,
        320
      ],
      "parameters": {
        "command": "=sshpass -p '{{ $('Server Parameters').item.json.source_password }}' ssh -o StrictHostKeyChecking=no -p {{ $('Server Parameters').item.json.source_port }} {{ $('Server Parameters').item.json.source_user }}@{{ $('Server Parameters').item.json.source_host }} \"if command -v apt-get &> /dev/null; then echo '{{ $('Server Parameters').item.json.source_password }}' | sudo -S apt-get update && echo '{{ $('Server Parameters').item.json.source_password }}' | sudo -S apt-get install -y sshpass rsync; elif command -v yum &> /dev/null; then echo '{{ $('Server Parameters').item.json.source_password }}' | sudo -S yum install -y epel-release && echo '{{ $('Server Parameters').item.json.source_password }}' | sudo -S yum install -y sshpass rsync; elif command -v dnf &> /dev/null; then echo '{{ $('Server Parameters').item.json.source_password }}' | sudo -S dnf install -y sshpass rsync; else echo 'Unsupported package manager'; exit 1; fi\""
      },
      "typeVersion": 1
    },
    {
      "id": "93e5b9e9-5a31-404f-932c-ba050d10e5d5",
      "name": "执行 Rsync 备份",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        -1040,
        224
      ],
      "parameters": {
        "command": "=sshpass -p '{{ $('Server Parameters').item.json.source_password }}' ssh -o StrictHostKeyChecking=no -p {{ $('Server Parameters').item.json.source_port }} {{ $('Server Parameters').item.json.source_user }}@{{ $('Server Parameters').item.json.source_host }} \"sshpass -p '{{ $('Server Parameters').item.json.target_password }}' rsync {{ $('Server Parameters').item.json.rsync_options }} -e 'ssh -o StrictHostKeyChecking=no -p {{ $('Server Parameters').item.json.target_port }}' {{ $('Server Parameters').item.json.source_folder }}/ {{ $('Server Parameters').item.json.target_user }}@{{ $('Server Parameters').item.json.target_host }}:{{ $('Server Parameters').item.json.target_folder }}/\""
      },
      "typeVersion": 1
    },
    {
      "id": "e2b2ff1a-0d35-42ba-875d-8bb0e78dae8a",
      "name": "成功?",
      "type": "n8n-nodes-base.if",
      "position": [
        -816,
        224
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.exitCode }}",
              "operation": "equal"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ae9e2524-1b95-4473-8679-1b42809583cd",
      "name": "备份成功",
      "type": "n8n-nodes-base.set",
      "position": [
        -592,
        128
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "status",
              "value": "SUCCESS"
            },
            {
              "name": "timestamp",
              "value": "={{ $now.format('YYYY-MM-DD HH:mm:ss') }}"
            },
            {
              "name": "source",
              "value": "={{ $('Server Parameters').item.json.source_host }}:{{ $('Server Parameters').item.json.source_folder }}"
            },
            {
              "name": "target",
              "value": "={{ $('Server Parameters').item.json.target_host }}:{{ $('Server Parameters').item.json.target_folder }}"
            },
            {
              "name": "output",
              "value": "={{ $('Execute Rsync Backup').item.json.stdout }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "3d4ee816-96aa-4ac3-ab96-54f8ab220807",
      "name": "备份失败",
      "type": "n8n-nodes-base.set",
      "position": [
        -592,
        320
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "status",
              "value": "ERROR"
            },
            {
              "name": "timestamp",
              "value": "={{ $now.format('YYYY-MM-DD HH:mm:ss') }}"
            },
            {
              "name": "source",
              "value": "={{ $('Server Parameters').item.json.source_host }}"
            },
            {
              "name": "target",
              "value": "={{ $('Server Parameters').item.json.target_host }}"
            },
            {
              "name": "output",
              "value": "={{ $('Execute Rsync Backup').item.json.exitCode }} -- {{ $('Execute Rsync Backup').item.json.stderr }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "f7d833ae-0003-48f8-8d13-4f22cb70f52f",
      "name": "处理完成报告 --- Telegram 和短信",
      "type": "n8n-nodes-base.executeCommand",
      "onError": "continueRegularOutput",
      "position": [
        -272,
        224
      ],
      "parameters": {
        "command": "=apk add curl\n\nexport TOKEN=\"YOUR-TELEGRAM-BOT-TOKEN\"\nexport CHAT_ID=\"YOUR-TELEGRAM-CHANNEL-ID\"\nexport MESSAGE=\"{{ $json.timestamp }} -- {{ $json.status }} :: {{ $json.source }} -> {{ $json.target }} :: {{ $json.output }}\"\n\ncurl -s -X POST \"https://api.telegram.org/bot$TOKEN/sendMessage\" -d chat_id=\"$CHAT_ID\" -d text=\"$MESSAGE\"\n\nNUMBERS=\"+36301234567\"\n\nfor PHONE in $NUMBERS; do\n  curl -X POST https://textbelt.com/text --data-urlencode phone=\"$PHONE\" --data-urlencode message=\"$MESSAGE\" -d key=YOUR-TEXTBELT-API-KEY\ndone"
      },
      "typeVersion": 1
    },
    {
      "id": "4f541742-fac7-4d71-b236-c4bc65856f4a",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3088,
        -240
      ],
      "parameters": {
        "width": 380,
        "height": 232,
        "content": "## 自动化 Rsync 备份"
      },
      "typeVersion": 1
    },
    {
      "id": "b74c3604-78af-4a0c-a7df-b4bd0c1ecd71",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2576,
        384
      ],
      "parameters": {
        "width": 340,
        "height": 348,
        "content": "## 需要服务器配置"
      },
      "typeVersion": 1
    },
    {
      "id": "b6910e2a-f654-4918-a3e1-51bddbe9bb51",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2352,
        -64
      ],
      "parameters": {
        "width": 340,
        "height": 248,
        "content": "## 依赖管理"
      },
      "typeVersion": 1
    },
    {
      "id": "720d40a5-6799-4d92-8b2d-1b4e27e7408c",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        -64
      ],
      "parameters": {
        "width": 320,
        "height": 256,
        "content": "## 备份执行"
      },
      "typeVersion": 1
    },
    {
      "id": "55adad40-e782-4687-bd28-d99414754a2e",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -592,
        464
      ],
      "parameters": {
        "width": 300,
        "height": 224,
        "content": "## 状态处理"
      },
      "typeVersion": 1
    },
    {
      "id": "db8dbab3-3671-424e-b690-d82c56bed6c5",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        -96
      ],
      "parameters": {
        "width": 340,
        "height": 296,
        "content": "## 通知配置"
      },
      "typeVersion": 1
    },
    {
      "id": "0c19273d-a6a3-43c9-9f4a-a43f8a1029b1",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3248,
        128
      ],
      "parameters": {
        "width": 388,
        "height": 112,
        "content": "## 调度"
      },
      "typeVersion": 1
    },
    {
      "id": "f5f0cb34-f5a9-4ad9-a2ee-54852328bc54",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -2800,
        32
      ],
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "44bd3bc1-2f84-4948-b794-6a8b23b50258",
  "connections": {
    "Success?": {
      "main": [
        [
          {
            "node": "Backup Successful",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Backup Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Backup Failed": {
      "main": [
        [
          {
            "node": "Process Finish Report --- Telegam & SMS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Server Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Server Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Backup Successful": {
      "main": [
        [
          {
            "node": "Process Finish Report --- Telegam & SMS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Server Parameters": {
      "main": [
        [
          {
            "node": "Check Sshpass Local",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Sshpass Local": {
      "main": [
        [
          {
            "node": "Is Installed Local?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Installed Local?": {
      "main": [
        [
          {
            "node": "Check Sshpass on Source",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Install Sshpass Local",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Rsync Backup": {
      "main": [
        [
          {
            "node": "Success?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Install Sshpass Local": {
      "main": [
        [
          {
            "node": "Check Sshpass on Source",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Sshpass on Source": {
      "main": [
        [
          {
            "node": "Is Installed on Source?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Installed on Source?": {
      "main": [
        [
          {
            "node": "Execute Rsync Backup",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Install Sshpass on Source",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Install Sshpass on Source": {
      "main": [
        [
          {
            "node": "Execute Rsync Backup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级

需要付费吗?

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

工作流信息
难度等级
高级
节点数量21
分类-
节点类型6
难度说明

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

外部链接
在 n8n.io 查看

分享此工作流