从快照恢复卷

发布于 2024-10-21 23:08:31 字数 95 浏览 1 评论 0原文

假设我有一个带有附加 EBS 卷的 AMI。

我还有一张快照。

我想将 EBS 卷“恢复”到快照。

执行此操作的最佳流程是什么?

Let's say I have an AMI with an attached EBS Volume.

I also have a snapshot.

I want to "restore" the EBS Volume to the snapshot.

What's the best process to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清风挽心 2024-10-28 23:08:31

我不知道有什么方法可以“恢复”附加卷,但我的方法是从快照创建一个卷,然后分离原始卷并附加新卷。

I don't know of a way that you can 'restore' an attached volume, but the way i would do it is to create a volume from the snapshot, then detach the original and attach the new one.

尐偏执 2024-10-28 23:08:31

如果您有一个正在运行的 EC2 实例,并且想要将其恢复到先前快照中捕获的状态,那么您需要停止该实例,分离其当前卷,从快照创建一个新卷,将新卷附加到您的实例。实例,然后重新启动您的实例。此外,在指定新卷的可用区域以及分离/重新附加卷时的设备名称方面存在一些微妙之处。

如果您从命令行而不是从 AWS Web UI 执行此操作,则可能更容易查看逻辑。

以下 bash 脚本不适合生产使用,因为它缺乏任何错误检查,并且仅使用 sleep 而不是轮询来确保 AWS 命令​​已完成。但它确实成功执行了所有这些步骤:

#!/bin/bash

set -e

# IN PARAMS
INSTANCE_ID=<YOUR_INSTANCE_ID_HERE>
SNAPSHOT_ID=<YOUR_SNAPSHOT_ID_HERE>
# OUT PARAMS
VOLUME_ID=

# begin execution

echo "Gathering information about the instance"
DEVICE_NAME=`ec2-describe-instance-attribute ${INSTANCE_ID} --block-device-mapping | awk '{print $2}'`
OLD_VOLUME_ID=`ec2-describe-instance-attribute ${INSTANCE_ID} --block-device-mapping | awk '{print $3}'`
echo "Found instance ${INSTANCE_ID} has volume ${OLD_VOLUME_ID} on device ${DEVICE_NAME}"

echo "Creating new volume from snapshot"
AVAILABILITY_ZONE=`ec2-describe-availability-zones --filter state=available | head -n 1 | awk '{print $2}'`
VOLUME_ID=`ec2-create-volume --availability-zone ${AVAILABILITY_ZONE} --snapshot ${SNAPSHOT_ID} | awk '{print $2}'`
echo "Created new volume: ${VOLUME_ID}"

sleep 20

echo "Stopping the instance"
ec2-stop-instances $INSTANCE_ID

sleep 20

echo "Detaching current volume"
ec2-detach-volume $OLD_VOLUME_ID --instance $INSTANCE_ID --device $DEVICE_NAME

sleep 20

echo "Attaching new volume"
ec2-attach-volume $VOLUME_ID  --instance $INSTANCE_ID --device $DEVICE_NAME

sleep 20

echo "Starting the instance"
ec2-start-instances $INSTANCE_ID

If you have a running EC2 instance, and you want to restore it to the state captured in an earlier snapshot, then you need to stop the instance, detach its current volume, create a new volume from the snapshot, attach the new volume to your instance, and restart your instance. Furthermore, there are a couple subtleties around specifying the availability zone of the new volume, and the device name when detaching/re-attaching the volume.

The logic might easier to see if you do it from the command line, instead of from the AWS web UI.

The following bash script is not fit for production use, since it lacks any error-checking and it just uses sleep instead of polling to ensure AWS commands have completed. But it does perform all these steps successfully:

#!/bin/bash

set -e

# IN PARAMS
INSTANCE_ID=<YOUR_INSTANCE_ID_HERE>
SNAPSHOT_ID=<YOUR_SNAPSHOT_ID_HERE>
# OUT PARAMS
VOLUME_ID=

# begin execution

echo "Gathering information about the instance"
DEVICE_NAME=`ec2-describe-instance-attribute ${INSTANCE_ID} --block-device-mapping | awk '{print $2}'`
OLD_VOLUME_ID=`ec2-describe-instance-attribute ${INSTANCE_ID} --block-device-mapping | awk '{print $3}'`
echo "Found instance ${INSTANCE_ID} has volume ${OLD_VOLUME_ID} on device ${DEVICE_NAME}"

echo "Creating new volume from snapshot"
AVAILABILITY_ZONE=`ec2-describe-availability-zones --filter state=available | head -n 1 | awk '{print $2}'`
VOLUME_ID=`ec2-create-volume --availability-zone ${AVAILABILITY_ZONE} --snapshot ${SNAPSHOT_ID} | awk '{print $2}'`
echo "Created new volume: ${VOLUME_ID}"

sleep 20

echo "Stopping the instance"
ec2-stop-instances $INSTANCE_ID

sleep 20

echo "Detaching current volume"
ec2-detach-volume $OLD_VOLUME_ID --instance $INSTANCE_ID --device $DEVICE_NAME

sleep 20

echo "Attaching new volume"
ec2-attach-volume $VOLUME_ID  --instance $INSTANCE_ID --device $DEVICE_NAME

sleep 20

echo "Starting the instance"
ec2-start-instances $INSTANCE_ID
时光沙漏 2024-10-28 23:08:31

我已经修改了 @algal 提供的脚本,以使用 aws cli 和轮询而不是睡眠。它还将查找给定卷的最新快照。

#!/bin/bash

set -e

# IN PARAMS
RECOVERY_INSTANCE_ID=
SNAPSHOT_VOLUME_ID=

echo "Gathering information about the instance"
BLOCK_DEVICE_MAPPING=`aws ec2 describe-instance-attribute --instance-id ${RECOVERY_INSTANCE_ID} --attribute blockDeviceMapping`
DEVICE_NAME=`echo ${BLOCK_DEVICE_MAPPING} | jq '.BlockDeviceMappings[0].DeviceName' | tr -d '"'`
OLD_VOLUME_ID=`echo ${BLOCK_DEVICE_MAPPING} | jq '.BlockDeviceMappings[0].Ebs.VolumeId' | tr -d '"'`
AVAILABILITY_ZONE=`aws ec2 describe-instances --filters "Name=instance-id,Values='${RECOVERY_INSTANCE_ID}'" | jq '.Reservations[0].Instances[0].Placement.AvailabilityZone' | tr -d '"'`
LATEST_SNAPSHOT_ID=`aws ec2 describe-snapshots --filter "Name=volume-id,Values='${SNAPSHOT_VOLUME_ID}'" | jq '.[]|max_by(.StartTime)|.SnapshotId' | tr -d '"'`
echo "Found instance ${RECOVERY_INSTANCE_ID} in ${AVAILABILITY_ZONE} has volume ${OLD_VOLUME_ID} on device ${DEVICE_NAME}"

echo "Creating new volume from snapshot ${LATEST_SNAPSHOT_ID}"
NEW_VOLUME_ID=`aws ec2 create-volume --region eu-west-1 --availability-zone ${AVAILABILITY_ZONE} --snapshot-id ${LATEST_SNAPSHOT_ID} | jq '.VolumeId' | tr -d '"'`
echo "Created new volume ${NEW_VOLUME_ID}"

aws ec2 wait volume-available --volume-ids $NEW_VOLUME_ID

echo "Stopping the instance"
aws ec2 stop-instances --instance-ids $RECOVERY_INSTANCE_ID

aws ec2 wait instance-stopped --instance-ids $RECOVERY_INSTANCE_ID

echo "Detaching current volume"
aws ec2 detach-volume --volume-id $OLD_VOLUME_ID --instance-id $RECOVERY_INSTANCE_ID

aws ec2 wait volume-available --volume-ids $OLD_VOLUME_ID

echo "Attaching new volume"
aws ec2 attach-volume --volume-id $NEW_VOLUME_ID --instance-id $RECOVERY_INSTANCE_ID --device $DEVICE_NAME

aws ec2 wait volume-in-use --volume-ids $NEW_VOLUME_ID

echo "Starting the instance"
aws ec2 start-instances --instance-ids $RECOVERY_INSTANCE_ID

如果您想了解此脚本的最新情况或做出贡献:

https://github.com/karimtabet/ ebs_snapshot_recovery

I have touched up the script provided by @algal to use the aws cli and polling instead of sleep. It will also look for the latest snapshot of the given volume.

#!/bin/bash

set -e

# IN PARAMS
RECOVERY_INSTANCE_ID=
SNAPSHOT_VOLUME_ID=

echo "Gathering information about the instance"
BLOCK_DEVICE_MAPPING=`aws ec2 describe-instance-attribute --instance-id ${RECOVERY_INSTANCE_ID} --attribute blockDeviceMapping`
DEVICE_NAME=`echo ${BLOCK_DEVICE_MAPPING} | jq '.BlockDeviceMappings[0].DeviceName' | tr -d '"'`
OLD_VOLUME_ID=`echo ${BLOCK_DEVICE_MAPPING} | jq '.BlockDeviceMappings[0].Ebs.VolumeId' | tr -d '"'`
AVAILABILITY_ZONE=`aws ec2 describe-instances --filters "Name=instance-id,Values='${RECOVERY_INSTANCE_ID}'" | jq '.Reservations[0].Instances[0].Placement.AvailabilityZone' | tr -d '"'`
LATEST_SNAPSHOT_ID=`aws ec2 describe-snapshots --filter "Name=volume-id,Values='${SNAPSHOT_VOLUME_ID}'" | jq '.[]|max_by(.StartTime)|.SnapshotId' | tr -d '"'`
echo "Found instance ${RECOVERY_INSTANCE_ID} in ${AVAILABILITY_ZONE} has volume ${OLD_VOLUME_ID} on device ${DEVICE_NAME}"

echo "Creating new volume from snapshot ${LATEST_SNAPSHOT_ID}"
NEW_VOLUME_ID=`aws ec2 create-volume --region eu-west-1 --availability-zone ${AVAILABILITY_ZONE} --snapshot-id ${LATEST_SNAPSHOT_ID} | jq '.VolumeId' | tr -d '"'`
echo "Created new volume ${NEW_VOLUME_ID}"

aws ec2 wait volume-available --volume-ids $NEW_VOLUME_ID

echo "Stopping the instance"
aws ec2 stop-instances --instance-ids $RECOVERY_INSTANCE_ID

aws ec2 wait instance-stopped --instance-ids $RECOVERY_INSTANCE_ID

echo "Detaching current volume"
aws ec2 detach-volume --volume-id $OLD_VOLUME_ID --instance-id $RECOVERY_INSTANCE_ID

aws ec2 wait volume-available --volume-ids $OLD_VOLUME_ID

echo "Attaching new volume"
aws ec2 attach-volume --volume-id $NEW_VOLUME_ID --instance-id $RECOVERY_INSTANCE_ID --device $DEVICE_NAME

aws ec2 wait volume-in-use --volume-ids $NEW_VOLUME_ID

echo "Starting the instance"
aws ec2 start-instances --instance-ids $RECOVERY_INSTANCE_ID

If you'd like to stay up to date with this script or contribute:

https://github.com/karimtabet/ebs_snapshot_recovery

逆光飞翔i 2024-10-28 23:08:31

要将附加到实例的卷替换为从快照创建的新卷:

  1. 在实例所在的同一可用区中从快照创建卷(右键单击快照,然后单击“从快照创建卷”)
  2. 最好停止实例以避免任何应用程序崩溃。等待实例停止。
  3. 记下原始卷的确切设备名称(它写在 AWS 控制台的实例视图或卷视图下)
  4. 分离旧卷,如果不需要,则稍后将其删除。
  5. 将新创建的卷(从快照)附加到具有相同设备名称的实例。
  6. 再次启动实例

To replace a volume attached to an instance with a new volume created from a snapshot:

  1. Create a volume from the snapshot in the same availability zone the instance is in (right click on snapshot and click "create volume from snapshot")
  2. Best to stop the instance to avoid any application from crashing. Wait until instance is stopped.
  3. Write down the exact device name of the original volume (it is written in the AWS console under instances view or volumes view)
  4. Detach the old volume, delete it afterwards if you don't need it.
  5. Attach the newly created volume (from the snapshot) to the instance with the same device name.
  6. Start the instance again
゛时过境迁 2024-10-28 23:08:31

从快照创建一个卷,以将该卷挂载到现有 EC2 计算机上并从中复制文件。

检查 EC2 机器。

  1. 选择一个实例。 EC2 选项卡 |实例 |实例。
  2. 记下 EC2 计算机的可用区域。

创建一个卷。

  1. 找到您要从中复制文件的快照并勾选该框。弹性块存储 |快照
  2. 单击创建卷按钮并填写字段。
    o 大小必须大于快照大小(免费微实例获得 8GB 卷)。
    o 可用区必须与 EC2 机器的可用区相同。
    o 快照已被选择,或多或少类似于 snap12345678 - 我的描述。
  3. 单击是,创建按钮。卷表中将出现一个新行。弹性块存储 |卷

附加卷。

  1. 单击附加卷按钮并填写字段。
  2. 音量值已经存在。
  3. 从实例下拉列表中选择您的计算机名称 i-12345678(正在运行)。
  4. 设备字段显示第一个可用的设备名称,例如 /dev/sdf。有人愿意改变这个值吗?
  5. 单击是,创建按钮。 EC2 机器上神奇地出现了一个新设备。
  6. 关闭 AWS 控制台。

Make a volume from the snapshot to mount the volume on an existing EC2 machine and copy files from it.

Check the EC2 machine.

  1. Pick an instance. EC2 tab | INSTANCES | Instances.
  2. Make a note of the EC2 machine’s availability zone.

Create a volume.

  1. Find the snapshot you want to copy files from and tick the box. ELASTIC BLOCK STORE | Snapshots
  2. Click the Create Volume button and fill in the fields.
    o The Size must be bigger than the snapshot size (free micro-instances get an 8GB volume).
    o The Availability Zone must be the same as the EC2 machine’s.
    o The Snapshot is already selected, more or less like snap12345678 - my description.
  3. Click the Yes, Create button. A new line appears in the Volumes table. ELASTIC BLOCK STORE | Volumes

Attach the volume.

  1. Click the Attach Volume button and fill in the fields.
  2. The Volume value is already there.
  3. Pick your machine name i-12345678 (running) from the drop-down list of Instances.
  4. The Devices field shows the first available device name, like /dev/sdf. Does anyone bother changing this value?
  5. Click the Yes, Create button. A new device magically appears on the EC2 machine.
  6. Close the AWS console.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文