前置知识: 运维与中间件

跨云数据迁移与备份实操

3 min入门

rclone 跨云同步、数据校验与备份策略、Velero 跨云 K8s 迁移、镜像与数据库迁移、跨云 IaC 工具与监控告警的命令手册。

知识点地图

  • 知识类别:跨云数据迁移与备份的实操命令手册——rclone 同步、数据校验、备份策略、K8s/镜像/数据库迁移工具与跨云 IaC、监控告警。
  • 解决什么问题:数据真的要搬(或要备份)时,每一步用什么命令、参数怎么给、校验怎么做过才敢切换。
  • 什么时候用到:多云/混合云的数据面建设与迁移演练。架构层面的「为什么与怎么选」——互联方式、Terraform 多 Provider、统一身份——见多云与混合云架构,本文默认你已读过它。

全篇按「安装 → 配置远程 → 同步 → 校验 → 迁移」组织,示例凭证均为占位符, 生产环境请优先使用各云 IAM 角色(env_auth = true)而非明文密钥。

多云工具安装

基本写法:安装 rclone curl https://rclone.org/install.sh | sudo bash

bash
# 安装 rclone 跨云同步工具
curl https://rclone.org/install.sh | sudo bash

基本写法:Windows 安装 rclone winget install Rclone.Rclone

bash
# Windows 通过 winget 安装
winget install Rclone.Rclone

基本写法:查看版本 rclone version

bash
# 查看 rclone 版本
rclone version

基本写法:交互式配置 rclone config

bash
# 进入交互式配置新增远程存储
rclone config

基本写法:查看已配置远程 rclone listremotes

bash
# 列出所有已配置的远程存储
rclone listremotes

远程存储配置

基本写法:配置 AWS S3

ini
# ~/.config/rclone/rclone.conf 配置 S3
[mys3]
type = s3
provider = AWS
env_auth = false
access_key_id = AKIAIOSFODNN7EXAMPLE
secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
region = us-east-1
endpoint =
location_constraint = us-east-1

基本写法:配置 Azure Blob

ini
# 配置 Azure Blob Storage
[myazure]
type = azureblob
account = mystorageaccount
key = MyStorageKey1234567890ABCDEF==
endpoint =

基本写法:配置 GCS

ini
# 配置 Google Cloud Storage
[mygcs]
type = google cloud storage
client_id = your-client-id
client_secret = your-client-secret
project_number = 123456789012
service_account_file = /path/to/key.json
object_acl = private
bucket_acl = private

基本写法:配置阿里云 OSS

ini
# 配置阿里云 OSS
[myoss]
type = s3
provider = Alibaba
env_auth = false
access_key_id = LTAI4your-access-key
secret_access_key = your-secret-key
endpoint = oss-cn-hangzhou.aliyuncs.com
acl = private

基本写法:配置腾讯云 COS

ini
# 配置腾讯云 COS
[mycos]
type = s3
provider = TencentCOS
env_auth = false
access_key_id = AKIDyour-access-key
secret_access_key = your-secret-key
endpoint = cos.ap-guangzhou.myqcloud.com

数据同步

基本写法:同步目录 rclone sync <源> <目标> [--progress]

bash
# 从 S3 同步到 Azure Blob
rclone sync mys3:my-bucket myazure:my-container --progress

基本写法:复制文件 rclone copy <源> <目标>

bash
# 复制 S3 文件到 GCS(保留原文件)
rclone copy mys3:my-bucket/data mygcs:my-bucket/data --progress

基本写法:移动文件 rclone move <源> <目标>

bash
# 移动文件并删除源(用于迁移)
rclone move mys3:old-bucket myazure:new-container --progress

基本写法:增量同步 rclone sync <源> <目标> --update --verbose

bash
# 仅同步修改过的文件
rclone sync mys3:my-bucket mygcs:my-bucket --update --verbose

基本写法:带过滤同步 rclone sync <源> <目标> --include <模式> --exclude <模式>

bash
# 仅同步 images 目录下的 jpg 文件
rclone sync mys3:my-bucket myazure:my-container \
  --include "images/*.jpg" \
  --exclude "*"

数据查看与校验

基本写法:列出文件 rclone ls <远程>:<路径>

bash
# 列出 S3 桶内所有文件
rclone ls mys3:my-bucket

基本写法:列出大小 rclone lsl <远程>:<路径>

bash
# 列出文件含大小和修改时间
rclone lsl mygcs:my-bucket/data

基本写法:树形显示 rclone tree <远程>:<路径>

bash
# 树形结构展示目录
rclone tree mys3:my-bucket

基本写法:计算大小 rclone size <远程>:<路径>

bash
# 计算目录总大小与文件数
rclone size mys3:my-bucket

基本写法:校验数据完整性 rclone check <源> <目标>

bash
# 校验源和目标文件是否一致
rclone check mys3:my-bucket myazure:my-container --download

基本写法:对比差异 rclone check <源> <目标> --one-way

bash
# 仅检查源比目标多的文件
rclone check mys3:my-bucket myazure:my-container --one-way

跨云迁移实战

基本写法:AWS 到 GCP 迁移 rclone sync mys3:source-bucket mygcs:target-bucket --transfers <并发> --checkers <并发>

bash
# 高并发迁移大量文件
rclone sync mys3:source-bucket mygcs:target-bucket \
  --transfers 32 \
  --checkers 16 \
  --progress \
  --stats 30s

基本写法:Azure 到 AWS 迁移 rclone sync myazure:container mys3:bucket --retries <次数>

bash
# 带重试机制的迁移
rclone sync myazure:my-container mys3:my-bucket \
  --retries 5 \
  --low-level-retries 10 \
  --progress

基本写法:迁移带带宽限制 rclone sync <源> <目标> --bwlimit <带宽>

bash
# 限制带宽 10MB/s 避免影响业务
rclone sync mys3:my-bucket myazure:my-container \
  --bwlimit 10M \
  --progress

基本写法:迁移大型数据集 rclone sync <源> <目标> --s3-chunk-size <大小> --s3-upload-concurrency <并发>

bash
# 优化大文件迁移
rclone sync mys3:source mygcs:target \
  --s3-chunk-size 256M \
  --s3-upload-concurrency 8 \
  --transfers 16 \
  --progress

基本写法:迁移并保留元数据 rclone sync <源> <目标> --metadata

bash
# 保留所有元数据(ACL、时间戳)
rclone sync mys3:my-bucket myazure:my-container \
  --metadata \
  --progress

数据备份策略

基本写法:定时备份脚本

bash
#!/bin/bash
# daily-backup.sh 每日备份脚本
set -e

DATE=$(date +%Y%m%d)
SOURCE="mys3:production-data"
DEST="myazure:backup/$DATE"

# 执行同步备份
rclone sync $SOURCE $DEST \
  --progress \
  --log-file /var/log/rclone-backup.log \
  --transfers 16

# 删除 30 天前的备份
rclone delete myazure:backup/ --min-age 30d
echo "Backup completed: $DATE"

基本写法:使用 systemd timer 调度

ini
# /etc/systemd/system/rclone-backup.timer
[Unit]
Description=Daily rclone backup

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

基本写法:服务定义

ini
# /etc/systemd/system/rclone-backup.service
[Unit]
Description=Run rclone backup
After=network-online.target

[Service]
Type=oneshot
ExecStart=/opt/scripts/daily-backup.sh
User=backup

基本写法:加密备份 rclone sync <源> <加密目标> --crypt-remote <远程> --crypt-directory-name <目录>

ini
# rclone.conf 配置加密远程
[backup-encrypted]
type = crypt
remote = myazure:encrypted-backup
filename_encryption = standard
directory_name_encryption = true
password = MyEncryptedPassword123
password2 = MySaltForEncryption123

基本写法:解密恢复 rclone copy <加密远程>:<路径> <本地路径>

bash
# 从加密备份恢复数据
rclone copy backup-encrypted:2026-07-31 /tmp/restored --progress

Velero 跨云 K8s 迁移

基本写法:安装 Velero velero install --provider <提供者> --bucket <桶> --secret-file <凭证文件>

bash
# 安装 Velero 备份工具
velero install \
  --provider aws \
  --bucket velero-backups \
  --backup-location-config region=us-east-1 \
  --snapshot-location-config region=us-east-1 \
  --secret-file credentials-velero

基本写法:创建备份 velero backup create <备份名> [--include-namespaces <命名空间>]

bash
# 备份指定命名空间
velero backup create my-backup --include-namespaces production

基本写法:查看备份状态 velero backup describe <备份名>

bash
# 查看备份详情
velero backup describe my-backup --details

基本写法:从备份恢复 velero restore create --from-backup <备份名>

bash
# 在目标集群恢复备份
velero restore create --from-backup my-backup

基本写法:跨集群迁移

bash
# 源集群:创建备份到对象存储
velero backup create cluster-migration --include-cluster-resources=true

# 目标集群:配置相同的备份位置后恢复
velero restore create --from-backup cluster-migration

跨云镜像迁移

基本写法:拉取镜像 docker pull <源镜像>

bash
# 拉取 Docker Hub 镜像
docker pull nginx:1.25

基本写法:打标签到目标仓库 docker tag <源镜像> <目标仓库>/<镜像>:<标签>

bash
# 为推送到 ECR 准备标签
docker tag nginx:1.25 123456789012.dkr.ecr.us-east-1.amazonaws.com/nginx:1.25

基本写法:推送镜像 docker push <目标仓库>/<镜像>:<标签>

bash
# 推送到 AWS ECR
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/nginx:1.25

基本写法:使用 skopeo 跨仓库复制 skopeo copy docker://<源> docker://<目标>

bash
# 直接在仓库间复制镜像(无需本地拉取)
skopeo copy \
  docker://docker.io/nginx:1.25 \
  docker://123456789012.dkr.ecr.us-east-1.amazonaws.com/nginx:1.25

基本写法:跨云批量迁移镜像

bash
#!/bin/bash
# migrate-images.sh 批量迁移镜像
IMAGES=(
  "nginx:1.25"
  "redis:7.2"
  "postgres:16"
)
SOURCE="docker.io"
TARGET="123456789012.dkr.ecr.us-east-1.amazonaws.com"

for img in "${IMAGES[@]}"; do
  echo "Migrating $img..."
  skopeo copy \
    docker://$SOURCE/$img \
    docker://$TARGET/$img \
    --dest-creds AWS:$(aws ecr get-login-password)
done

跨云数据库迁移

基本写法:AWS DMS 创建复制实例 aws dms create-replication-instance --replication-instance-identifier <ID> --replication-instance-class <类>

bash
# 创建 DMS 复制实例
aws dms create-replication-instance \
  --replication-instance-identifier my-dms \
  --replication-instance-class dms.r5.large \
  --allocated-storage 100

基本写法:创建端点 aws dms create-endpoint --endpoint-identifier <ID> --endpoint-type <类型> --engine-name <引擎> --server-name <服务器> --port <端口>

bash
# 创建源端 PostgreSQL 端点
aws dms create-endpoint \
  --endpoint-identifier source-pg \
  --endpoint-type source \
  --engine-name postgres \
  --server-name pg.source.com \
  --port 5432 \
  --database-name mydb \
  --username admin \
  --password 'Pass123!'

基本写法:创建迁移任务 aws dms create-replication-task --replication-task-identifier <ID> --source-endpoint-arn <源> --target-endpoint-arn <目标> --replication-instance-arn <实例> --migration-type <类型>

bash
# 创建全量+CDC 迁移任务
aws dms create-replication-task \
  --replication-task-identifier my-migration \
  --source-endpoint-arn arn:aws:dms:us-east-1:123456789012:endpoint:ABC \
  --target-endpoint-arn arn:aws:dms:us-east-1:123456789012:endpoint:DEF \
  --replication-instance-arn arn:aws:dms:us-east-1:123456789012:rep:GHI \
  --migration-type full-load-and-cdc \
  --table-mappings file://mappings.json

基本写法:启动迁移任务 aws dms start-replication-task --replication-task-arn <ARN> --start-replication-task-type start-replication

bash
# 启动数据库迁移任务
aws dms start-replication-task \
  --replication-task-arn arn:aws:dms:us-east-1:123456789012:task:XYZ \
  --start-replication-task-type start-replication

基本写法:查看任务状态 aws dms describe-replication-tasks

bash
# 查看所有迁移任务
aws dms describe-replication-tasks

跨云 IaC 工具

基本写法:Terraform 多云 provider 配置

hcl
# 多云部署的 Terraform 配置
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }
    google = {
      source  = "hashicorp/google"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

provider "azurerm" {
  features {}
}

provider "google" {
  project = "my-project-123"
  region  = "us-central1"
}

基本写法:跨云相同资源定义

hcl
# 在三云创建相同规格的虚拟机
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"
  tags = { Name = "web-server" }
}

resource "azurerm_linux_virtual_machine" "web" {
  name                = "web-server"
  resource_group_name = azurerm_resource_group.main.name
  location            = "East US"
  size                = "Standard_B1s"
  admin_username      = "adminuser"
}

resource "google_compute_instance" "web" {
  name         = "web-server"
  machine_type = "e2-medium"
  zone         = "us-central1-a"
}

基本写法:使用 Terragrunt 多环境管理

hcl
# env/prod/terragrunt.hcl
terraform {
  source = "../../modules/web-server"
}

inputs = {
  instance_count = 5
  instance_type  = "t3.large"
  environment    = "production"
}

基本写法:跨云状态后端

hcl
# 使用 HCP Terraform Cloud 作为统一后端
terraform {
  cloud {
    organization = "my-org"
    workspaces {
      name = "multi-cloud-prod"
    }
  }
}

监控与告警

基本写法:rclone 同步状态检查脚本

bash
#!/bin/bash
# check-sync.sh 检查同步状态
LOG_FILE="/var/log/rclone-backup.log"
ERROR_COUNT=$(grep -c "ERROR" $LOG_FILE)
SUCCESS_COUNT=$(grep -c "Sync successful" $LOG_FILE)

if [ $ERROR_COUNT -gt 0 ]; then
  echo "WARNING: $ERROR_COUNT errors found in last sync"
  exit 1
fi
echo "OK: Last sync completed successfully"

基本写法:跨云成本对比

bash
# 使用 Infracost 估算多云成本
infracost breakdown --path . --format json > costs.json
# 查看各云资源成本
jq '.projects[].breakdown.resources[] | {address, monthlyCost}' costs.json

基本写法:Cloud Custodian 多云策略

yaml
# custodian.yml 多云资源策略
policies:
  - name: aws-unused-eips
    resource: aws.elastic-ip
    filters:
      - AssociationId: absent
    actions:
      - delete
  - name: azure-unattached-disks
    resource: azure.disk
    filters:
      - type: value
        key: managedBy
        value: null
    actions:
      - type: delete

基本写法:运行 Custodian custodian run -s <输出> <策略文件>

bash
# 执行多云合规策略
custodian run -s output custodian.yml