如何使用 AWS CLI 从 AWS Backup 恢复点还原 Amazon EC2 实例?

上次更新时间:2021 年 1 月 19 日

我想从 AWS Backup 恢复点还原 Amazon Elastic Compute Cloud (Amazon EC2) 实例。如何使用 AWS 命令行界面 (AWS CLI) 还原该实例?

解决方法

注意:如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新版本的 AWS CLI

1.    运行 list-recovery-points-by-backup-vault 命令,列出文件库中的所有恢复点:

aws backup list-recovery-points-by-backup-vault --backup-vault-name Default --output json --query 'RecoveryPoints[].[BackupVaultName,RecoveryPointArn]'

2.    在命令输出中,复制要还原的恢复点的 RecoveryPointArn

3.    运行 get-recovery-point-restore-metadata 命令,获取要还原的 Amazon EC2 实例的元数据:

aws backup get-recovery-point-restore-metadata --backup-vault-name Default --recovery-point-arn arn:aws:ec2:eu-west-1::image/ami-012ab3456789c0123

4.    复制命令输出,该命令输出类似于以下内容:

{
    "BackupVaultArn": "arn:aws:backup:eu-west-1:123456789012:backup-vault:Default",
    "RecoveryPointArn": "arn:aws:ec2:eu-west-1::image/ami-012ab3456789c0123",
    "RestoreMetadata": {
        "CapacityReservationSpecification": "{\"CapacityReservationPreference\":\"open\"}",
        "CpuOptions": "{\"CoreCount\":1,\"ThreadsPerCore\":1}",
        "CreditSpecification": "{\"CpuCredits\":\"standard\"}",
        "DisableApiTermination": "false",
        "EbsOptimized": "false",
        "HibernationOptions": "{\"Configured\":false}",
        "IamInstanceProfileName": "ServerAccess",
        "InstanceInitiatedShutdownBehavior": "stop",
        "InstanceType": "t2.micro",
        "KeyName": "BackupTesting",
        "Monitoring": "{\"State\":\"disabled\"}",
        "NetworkInterfaces": "[{\"AssociatePublicIpAddress\":true,\"DeleteOnTermination\":true,\"Description\":\"\",\"DeviceIndex\":0,\"Groups\":[\"sg-0babcd1234567890f\"],\"Ipv6AddressCount\":0,\"Ipv6Addresses\":[],\"NetworkInterfaceId\":\"eni-12345678a9b0cd1e2\",\"PrivateIpAddress\":\"192.0.2.206\",\"PrivateIpAddresses\":[{\"Primary\":true,\"PrivateIpAddress\":\"192.0.2.206\"}],\"SecondaryPrivateIpAddressCount\":0,\"SubnetId\":\"subnet-1a2b3c4d\",\"InterfaceType\":\"interface\"}]",
        "Placement": "{\"AvailabilityZone\":\"eu-west-1b\",\"GroupName\":\"\",\"Tenancy\":\"default\"}",
        "SecurityGroupIds": "[\"sg-0babcd1234567890f\"]",
        "SubnetId": "subnet-1a2b3c4d",
        "VpcId": "vpc-1a2b3c4d",
        "aws:backup:request-id": "c1234567-ee7c-4896-beeb-ee123b456789"
    }
}

5.    编辑命令输出,仅保留以下元数据字段和值:

{
"VpcId": "vpc-1a2b3c4d",
"Monitoring": "{\"State\":\"disabled\"}",
"CapacityReservationSpecification": "{\"CapacityReservationPreference\":\"open\"}",
"InstanceInitiatedShutdownBehavior": "stop",
"DisableApiTermination": "false",
"KeyName": "BackupTesting",
"CreditSpecification": "{\"CpuCredits\":\"standard\"}",
"HibernationOptions": "{\"Configured\":false}",
"EbsOptimized": "false",
"Placement": "{\"AvailabilityZone\":\"eu-west-1b\",\"GroupName\":\"\",\"Tenancy\":\"default\"}",
"aws:backup:request-id": "c1234567-ee7c-4896-beeb-ee123b456789",
"InstanceType": "t2.micro",
"NetworkInterfaces": "[{\"DeleteOnTermination\":true,\"Description\":\"Primary network interface\",\"DeviceIndex\":0,\"Groups\":[\"sg-0babcd1234567890f\"],\"Ipv6AddressCount\":0,\"Ipv6Addresses\":[],\"PrivateIpAddresses\":[{\"Primary\":true,\"PrivateIpAddress\":\"192.0.2.210\"}],\"SecondaryPrivateIpAddressCount\":1,\"SubnetId\":\"subnet-1a2b3c4d\",\"InterfaceType\":\"interface\"}]"
}

6.    如果原始实例仍在使用中,您还必须更改元数据输出中的主要私有 IP 地址。在 NetworkInterfaces 元数据字段中,将 Primary PrivateIpAddress 更改为与 get-recovery-point-restore-metadata 命令最初返回的 IP 地址不同的 IP 地址。如果原始实例已终止,并且您想使用原始私有 IP 地址(就地还原),则无需更改元数据中的主要私有 IP 地址。

7.    将编辑后的元数据保存为 JSON 文件。可以将文件命名为 ec2-restore-metadata.json

8.    运行 start-restore-job 命令,并将 --recovery-point-arn 设置为您在步骤 2 中复制的 RecoveryPointArn,将 --metadata 设置为您在步骤 5 中创建的 JSON 文件。

aws backup start-restore-job --region eu-west-1 --recovery-point-arn "arn:aws:ec2:eu-west-1::image/ami-012ab3456789c0123" --iam-role-arn "arn:aws:iam::123456789012:role/service-role/AWSBackupDefaultServiceRole"  --metadata file://ec2-restore-metadata.json

注意:您可以将实例元数据定义为 start-restore-job 命令中的参数,而不使用 JSON 文件。为此,请按如下格式运行 start-restore-job 命令:

aws backup start-restore-job --region eu-west-1 --recovery-point-arn "arn:aws:ec2:eu-west-1::image/ami-012ab3456789c0123" --iam-role-arn "arn:aws:iam::123456789012:role/service-role/AWSBackupDefaultServiceRole"  --metadata '{"VpcId": "vpc-1a2b3c4d", "Monitoring": "{\"State\":\"disabled\"}", "CapacityReservationSpecification": "{\"CapacityReservationPreference\":\"open\"}", "InstanceInitiatedShutdownBehavior": "stop", "DisableApiTermination": "false", "KeyName": "BackupTesting", "CreditSpecification": "{\"CpuCredits\":\"standard\"}", "HibernationOptions": "{\"Configured\":false}", "EbsOptimized": "false", "Placement": "{\"AvailabilityZone\":\"eu-west-1b\",\"GroupName\":\"\",\"Tenancy\":\"default\"}", "aws:backup:request-id": "c1234567-ee7c-4896-beeb-ee123b456789", "InstanceType": "t2.micro", "NetworkInterfaces": "[{\"DeleteOnTermination\":true,\"Description\":\"Primary network interface\",\"DeviceIndex\":0,\"Groups\":[\"sg-0babcd1234567890f\"],\"Ipv6AddressCount\":0,\"Ipv6Addresses\":[],\"PrivateIpAddresses\":[{\"Primary\":true,\"PrivateIpAddress\":\"172.31.32.210\"}],\"SecondaryPrivateIpAddressCount\":1,\"SubnetId\":\"subnet-1a2b3c4d\",\"InterfaceType\":\"interface\"}]"}'

这篇文章对您有帮助吗?


您是否需要账单或技术支持?