You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量迁移多台EC2实例与RDS数据库至其他区域?

批量跨区域迁移EC2实例与RDS数据库的方案

批量处理EC2实例迁移

方法1:使用AWS CLI编写脚本批量操作

适合有脚本基础的场景,可灵活控制迁移范围:

  1. 筛选并获取源区域目标EC2实例ID
    通过标签过滤需迁移的实例(避免误操作全量实例),执行命令:
# 替换us-east-1为源区域,Migration为自定义标签键,Yes为标签值
INSTANCE_IDS=$(aws ec2 describe-instances --region us-east-1 --filters "Name=tag:Migration,Values=Yes" --query 'Reservations[].Instances[].InstanceId' --output text)
  1. 批量创建EC2根卷快照
    循环遍历实例ID,创建对应根卷的快照:
for INSTANCE in $INSTANCE_IDS; do
    # 获取根卷ID(默认/dev/sda1,根据实际环境调整)
    VOLUME_ID=$(aws ec2 describe-instances --region us-east-1 --instance-id $INSTANCE --query 'Reservations[].Instances[].BlockDeviceMappings[?DeviceName==`/dev/sda1`].Ebs.VolumeId' --output text)
    # 创建快照
    aws ec2 create-snapshot --region us-east-1 --volume-id $VOLUME_ID --description "Migration snapshot for $INSTANCE"
done
  1. 批量复制快照到目标区域
    先获取源区域的迁移快照ID,再复制到目标区域:
# 替换us-east-1为源区域,us-west-2为目标区域
SNAPSHOT_IDS=$(aws ec2 describe-snapshots --region us-east-1 --filters "Name=description,Values=Migration snapshot for *" --query 'Snapshots[].SnapshotId' --output text)
for SNAPSHOT in $SNAPSHOT_IDS; do
    aws ec2 copy-snapshot --source-region us-east-1 --source-snapshot-id $SNAPSHOT --region us-west-2 --description "Copied migration snapshot $SNAPSHOT"
done
  1. 从复制后的快照批量创建AMI
    在目标区域执行,基于复制的快照生成AMI:
# 替换us-west-2为目标区域
COPIED_SNAPSHOTS=$(aws ec2 describe-snapshots --region us-west-2 --filters "Name=description,Values=Coped migration snapshot *" --query 'Snapshots[].SnapshotId' --output text)
for SNAPSHOT in $COPIED_SNAPSHOTS; do
    aws ec2 register-image --region us-west-2 \
        --name "Migration AMI from $SNAPSHOT" \
        --description "AMI created from cross-region migrated snapshot" \
        --root-device-name "/dev/sda1" \
        --block-device-mappings "[{\"DeviceName\": \"/dev/sda1\", \"Ebs\": {\"SnapshotId\": \"$SNAPSHOT\"}}]"
done

方法2:使用AWS Systems Manager Automation

无需编写脚本,通过自动化文档批量执行:

  • 打开Systems Manager控制台,进入自动化页面,选择预定义文档AWS-CopySnapshot或AWS-CreateImage
  • 配置参数:指定源区域、目标区域、要迁移的实例(可通过标签筛选)
  • 启动自动化执行,系统会批量完成快照创建、复制、AMI生成的全流程

批量处理RDS数据库迁移

使用AWS CLI批量操作

  1. 批量创建RDS手动快照
# 替换us-east-1为源区域
DB_INSTANCES=$(aws rds describe-db-instances --region us-east-1 --query 'DBInstances[].DBInstanceIdentifier' --output text)
for DB in $DB_INSTANCES; do
    aws rds create-db-snapshot --region us-east-1 \
        --db-snapshot-identifier "migration-snapshot-$DB" \
        --db-instance-identifier $DB
done
  1. 批量复制RDS快照到目标区域
# 替换us-east-1为源区域,us-west-2为目标区域
DB_SNAPSHOTS=$(aws rds describe-db-snapshots --region us-east-1 --snapshot-type manual --query 'DBSnapshots[].DBSnapshotIdentifier' --output text | grep migration-snapshot)
for SNAPSHOT in $DB_SNAPSHOTS; do
    aws rds copy-db-snapshot --source-region us-east-1 \
        --source-db-snapshot-identifier $SNAPSHOT \
        --target-db-snapshot-identifier "copied-$SNAPSHOT" \
        --region us-west-2
done
  1. 从复制的快照批量恢复RDS实例
# 替换us-west-2为目标区域
COPIED_DB_SNAPSHOTS=$(aws rds describe-db-snapshots --region us-west-2 --snapshot-type manual --query 'DBSnapshots[].DBSnapshotIdentifier' --output text | grep copied-migration-snapshot)
for SNAPSHOT in $COPIED_DB_SNAPSHOTS; do
    # 提取原DB实例名称,作为恢复后的实例名
    DB_NAME=${SNAPSHOT#copied-migration-snapshot-}
    aws rds restore-db-instance-from-db-snapshot --region us-west-2 \
        --db-instance-identifier $DB_NAME \
        --db-snapshot-identifier $SNAPSHOT
done

关键注意事项

  • 权限验证:确保执行操作的IAM角色拥有ec2:CreateSnapshot、ec2:CopySnapshot、rds:CreateDBSnapshot、rds:CopyDBSnapshot等必要权限
  • 状态检查:快照复制耗时与数据量相关,可通过aws ec2 describe-snapshots --snapshot-id <快照ID>或aws rds describe-db-snapshots --db-snapshot-identifier <快照ID>检查状态,确认完成后再进行下一步
  • 环境一致性:迁移完成后,需同步配置EC2的安全组、子网、弹性IP,以及RDS的参数组、VPC安全组,保证服务正常运行

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 22:22:52