Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring a volume from a snapshot

Tags:

amazon-ec2

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?

like image 831
Dave Avatar asked Mar 11 '11 20:03

Dave


People also ask

How do I restore AWS backup volume?

To restore a resource, in the Backups pane, choose the radio button next to the recovery point ID of the resource. In the upper-right corner of the pane, choose Restore. Specify the restore parameters for your resource. The restore parameters you enter are specific to the resource type that you selected.

Can I delete a volume after taking a snapshot?

After you no longer need an Amazon EBS snapshot of a volume, you can delete it. Deleting a snapshot has no effect on the volume.

Can you recover deleted EBS volumes?

Recycle Bin is a data recovery feature that enables you to restore accidentally deleted Amazon EBS snapshots and EBS-backed AMIs.

What is a snapshot of a volume?

A Volume Snapshot represents a Cloud storage volume at a particular point in time. Don't confuse this with a database snapshot, this is a snapshot of the entire volume, regardless of whether it contains a database or not. You can create a snapshot regardless of whether or not a volume is attached to an Instance.


2 Answers

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.

like image 190
Jason Haley Avatar answered Nov 12 '22 13:11

Jason Haley


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
like image 30
algal Avatar answered Nov 12 '22 13:11

algal