Introduction To AWS Elastic Block Store - Part Five

In the previous article Introduction to Elastic Block Storage-Part 4, we learned about mounting and unmounting. We also saw temporary and permanent mounting.
 
In this article, we are going to learn how to backup and restore a volume using Snapshot by covering the below topics.
  • Create a snapshot
  • Delete Data from the directory mounted to a partition
  • Recover data from the snapshot.

    • Unmount Partition.
    • Create Volume from Snapshot.
    • Detach the old volume.
    • Attach the volume created from Snapshot.
    • Mount it back.
Let’s get started.
 

Backup and restore volume using Snapshot

 
In this article, we are going to create a situation where we need to restore the lost data. Amazon EBS provides the ability to save point-in-time snapshots of your volumes to Amazon S3.
 
NOTE - Amazon EBS Snapshots are stored incrementally: only the blocks that have changed after your last snapshot are saved, and you are billed only for the changed blocks.
 
If you have a device with 100 GB of data but only 5 GB has changed after your last snapshot, a subsequent snapshot consumes only 5 additional GBs and you are billed only for the additional 5 GB of snapshot storage, even though both the earlier and later snapshots appear complete.
 
Snapshots can be used to instantiate multiple new volumes, expand the size of a volume, or move volumes across Availability Zones. When a new volume is created, you may choose to create it based on an existing Amazon EBS snapshot.
 
If we want to take a backup of the volume, then EBS gives us the option of Snapshot.
 
We will delete a few files from the mount point after taking the backup and then restore the deleted data.
 

Create Snapshot

 
So, let’s take the back up of our volume EBS-websrv01-devdata which we created in the previous articles.
 
Below is the screenshot which shows how we can select the volume and take the snapshot.
 
AWS Elastic Block Store
 
We can create the snapshot by selecting the volume and from actions by choosing the "Create snapshot" option.
 
AWS Elastic Block Store
 
Here, we can see that Snapshot is not going to be encrypted because the Volume EBS-websrv01-devdata is not encrypted. Snapshots of encrypted volumes will be encrypted automatically.
 
After filling the required details, click on "Create" to complete the snapshot process. The process will take a few minutes to complete.
 
NOTE
It's always a good practice to stop your instance before taking a snapshot when you are taking a snapshot of its root volume.
 
We have kept the same name of snapshot as that of volume except that we appended the snapshot keyword in the last.
 
AWS Elastic Block Store
 
So, we can see that snapshot has been created successfully and this 5GB of storage of snapshot data will be stored in the S3 bucket. Snapshot has 5GB of data because the volume whose backup we have taken was of 5GB in size.
 
NOTE
When we are going to create a snapshot for the volume for the very first time then it is going to take a full backup and it will clone the volume to some other place but if we are going to create a second snapshot then that will be of differential data.
 

DELETE DATA FROM DIRECTORY MOUNTED TO A PARTITION

 
Now suppose you accidentally deleted the data inside your directory /opt/devdata.
 
AWS Elastic Block Store
Here we can see that all the data from the directory /opt/devdata has been removed and our directory is empty now. This directory was mounted to a partition of the hard disk which we attached using the volume in the previous articles.
 
So, we don’t need to worry as we have a backup of the volume in terms of Snapshot that we just created and named as EBS-websrv01-devdata-Snapshot.
 
Recovering data from the snapshot will cause downtime and downtime is not good for the application.
 

RECOVER DATA FROM SNAPSHOT

 
Now, if we want to recover the lost data, we have to create a new volume from snapshot and replace the old volume with the new one. We will also increase the volume size.
 
To recover the data using Snapshot, we have a series of steps.
 
AWS Elastic Block Store
 

UNMOUNT PARTITION

 
We can unmount the partition by giving the directory name which we want to unmount.
 
AWS Elastic Block Store
 
Here, by using df-h, we can see that the partition /dev/xvdf1 is mounted to a directory /opt/devdata and we have unmounted it with the below command.
  1. unmount /opt/devdata 
Now, we don’t see the partition mounted a directory.
 
You may be wondering now how can we move a volume from one availability zone to another?
 
We can take a snapshot of a volume and then can create a volume from the snapshot and while creating a volume from snapshot we can select the zone where we want that volume to be available.
 

CREATE A NEW VOLUME FROM SNAPSHOT

 
Click Snapshots in the left pane of EC2 Dashboard, select the snapshot from which a new volume has to be created, click on Actions and select Create volume. You will get a "Create volume" pop-up dialog box as shown below.
 
AWS Elastic Block Store
 
And enter the details as shown below.
 
AWS Elastic Block Store
 
We can see that the new volume has been given name as EBS-websrv01-devdata_New. We can see below that the new volume has been created and can also see the old volume EBS-websrv01-devdata still attached to the EC2 instance websrv01.
 
AWS Elastic Block Store
 
Next is detaching the existing volume attached to the EC2 instance.
 

DETACH THE OLD VOLUME

 
For this, we will select the old volume EBS-websrv01-devdata and select the option of detach volume.
 
AWS Elastic Block Store
AWS Elastic Block Store
 
And hit confirm. So below we can see that it has been detached successfully.
 
AWS Elastic Block Store 
And we can also see the previous disk /dev/xvdf and its partition /dev/xvdf1 has also gone.
 
AWS Elastic Block Store
 
As of now, we are finished with the cleanup work so, next is to attach the newly created volume.
 

ATTACH THE VOLUME CREATED FROM SNAPSHOT

 
Select your new volume EBS-websrv01-devdata_New and select the instance websrv01 to attach.
 
AWS Elastic Block Store
 
AWS Elastic Block Store
 
And we can see that it has been attached successfully. This can be verified by running the below command.
  1. fdisk -l
AWS Elastic Block Store
 
Here, we can see that we got a new disk named /dev/xvdf and its partition /dev/xvdf1.
 
Now, should I do the disk format and partition it? What do you think?
 
No, we don’t need to do that because there is already data on the disk as it has been created by attaching the new volume EBS-websrv01-devdata_New which has been created by the snapshot of the old volume EBS-websrv01-devdata which had data.
 
So, we don’t need to do that again, else we will lose our data.
 

MOUNT IT BACK

 
And we can see by running the below commands that we have restored the data successfully, as this directory /opt/devdata now again contains the same content which is used to hold.
  1. mount -a
  2. ls /opt/devdata
AWS Elastic Block Store 
And we are done with restoring data. YAY 😊
 

SUMMARY

 
In this article, we saw with an example how one can create a snapshot and how one can restore the data by following a series of steps using the snapshot. We saw partition unmounting, detaching the volume and many more things.
 
I hope you find this article helpful. Stay tuned for more … Cheers!! 


Similar Articles