Basics of Backing Up Amazon EC2 Instances with EC2 Snapshots

Subscribe banner

Amazon Web Services (AWS) is a reliable cloud platform that allows you to run virtual machines (VMs) known as EC2 instances in Elastic Compute Cloud. EC2 instances should be backed up, just as any other virtual machines, to avoid data loss caused by software failure, malware infection, accidental deletions, etc. AWS provides a built-in backup feature for EC2 instances called EBS snapshots. This blog post explains how to back up AWS EC2 instances by using EBS snapshots with detailed examples of the configuration process.

The blog post about the AWS backup and restore of EC2 Instances contains the following main sections:

  • How to create snapshots
  • Creating multi-volume snapshots
  • AWS CLI commands to create snapshots
  • Restoring an EC2 instance using EBS snapshots
Before we start
For reliable Amazon EC2 backup and recovery, you need a third-party backup solution. NAKIVO Backup & Replication allows you to store application-consistent backups in any EC2 region, in Amazon S3, or any other location (including on-premises and other custom locations). With these options, you can follow a more effective AWS backup strategy. You can download NAKIVO Backup & Replication Free Edition now and back up your EC2 instances to any supported destination.

About EBS Snapshots

AWS uses EBS snapshot to refer to an incremental backup of an Elastic Block Store (EBS) volume. The terms AWS EC2 snapshot and Amazon snapshot are also used to refer to EBS snapshots. EBS volumes are virtual disks attached to EC2 instances. When the point-in-time snapshot of an EBS volume is created, the process of EBS volume data backup is started.

Amazon EBS snapshots are stored in Amazon S3. However, you cannot access the snapshots in S3 storage manually. You can manage these snapshots in the EC2 Management Console. When you create an EBS snapshot for the first time, a full backup is created. When you create the following EBS snapshots, only the changed data is backed up.

Data is backed up on the block level for EBS at the moment when the snapshot taking begins. This means that if an instance is running, the data blocks written to EBS volumes after the initiation of the snapshot-taking are not saved to the snapshot. As a result, EBS snapshots are crash-consistent. Note that EBS snapshots are not the same as snapshots in the context of VMware VMs and Hyper-V VMs, as the latter are not considered VM backups.

How to Create Snapshots in AWS Console

AWS Management Console is the web interface used to manage AWS in a web browser, including AWS EC2 backup operations. Sign in to AWS Management Console by using an account that has permissions to create EC2 instances, EBS volumes, and EBS snapshots.

Click Services and in the Compute section, click EC2 to open a web page to manage EC2 instances, EBS volumes, and other EC2 features. Once you click EC2 in the list of all services, EC2 Management Console opens.

Using AWS Management Console to create an EC2 snapshot

I have prepared an EC2 instance to demonstrate how to create EBS snapshots. The parameters of the EC2 instance that I’m going to back up and recover using EBS snapshots are the following:

Name: blog-temp

EC2 instance ID: i-0071290000000cb84

Instance type: t2.micro

Availability zone: eu-central-1c

EBS Volume 1: vol-0617000000000763c

Volume type: gp2

Volume size: 8 GB

The guest operating system (OS) running on the EC2 instance is Ubuntu Server (Linux).

Writing files to the EBS volume

Let’s create two files on the virtual disk attached to the EC2 instance before creating a snapshot. This way we can check whether these files are recovered after restoring the instance from the EBS snapshot. I create two files in the user home directory ~.

lspci > file01.txt

echo "test EC2 snapshot 01" > snapshot01.txt

Creating files on an EC2 instance before creating an Amazon snapshot

Preparing the instance

Stop the EC2 instance to be sure that the snapshot will contain consistent data. The reason for this is that EBS volumes work on the block level. When an EBS snapshot is taken, the EBS volume state is saved at that moment. Storage blocks are saved as is. The system is not aware of operations made with files on the file system level at that time. If files are being written on an EBS volume when a snapshot is taken, the snapshot can contain inconsistent data. The effect is similar to unplugging a power cable from a physical computer when files are being written. If you want to take a snapshot of a non-system EBS volume, you can unmount the file system from the operating system to ensure that there are no file writing operations on the volume. Hibernation must be disabled for the EC2 instance to take a snapshot.

Open the page with a list of EC2 instances in EC2 Management Console. Right-click the instance name and in the context menu, click Stop instance.

Stopping the EC2 instance before creating a new EC2 snapshot

Locate the volume used by the EC2 instance. Click the Storage tab and click the volume ID of the appropriate EBS volume (vol-0617000000000763c in our case).

Selecting the EBS volume attached to the EC2 instance to take an Amazon snapshot

Taking a snapshot

Select the EBS volume (click the checkbox on the left side of the line), then click Actions > Create Snapshot.

Selecting the Create Snapshot option in the Actions menu

Enter a description, set encryption settings, and add a tag. I use the following parameters.

Description: EBS snapshot blog 01

Encryption: Not encrypted

Click Create Snapshot.

Note: I use the unencrypted volume and create an unencrypted snapshot. In this case, encryption settings for the snapshot can be set manually. If you take a snapshot of an encrypted volume, the snapshot is encrypted automatically.

Setting description, encryption, and tags in the Create Snapshot window

You should see the message and the snapshot ID:

Create Snapshot Request Succeeded.

snap-0778000000000ab40

Click the snapshot ID to open a page with the snapshot options and status.

EC2 Create snapshot request succeeded

The snapshot status pending means that data from the EBS volume is being transferred now to Amazon S3. Wait until the process of snapshot creation is complete. The time depends on the size of data saved to the snapshot. When the Amazon snapshot creation is finished, the status is changed to completed.

Amazon EC2 backup is in progress

How to Take Multi-Volume Snapshots

It is common to find an EC2 instance using multiple EBS volumes. In this case, you can create a multi-volume snapshot for all EBS volumes attached to the EC2 instance in order to perform the Amazon EC2 backup.

Creating a second EBS volume for the EC2 instance

Let’s add a second EBS volume to my EC2 instance used for demonstration purposes. Adding the second EBS volume to the EC2 instance is straightforward. Go to Elastic Block Store > Volumes in the navigation pane of EC2 Management Console. Click Create Volume.

Adding a second EBS volume to demonstrate AWS backup and restore options

The region must be the same as the region of the instance (eu-central-1c in our case). Enter the volume type/size, set encryption options, and enter tags. Tags help find the volume in the list of volumes. I use tags in this example and the volume size is 1 GB.

Key: blog

Value: temp volume 2

Click Create Volume.

Options to create an EBS volume

Remember (or write down) the id of the volume (vol-0b4a0000000003d01 in our case). Click the volume ID to open the volume properties.

The volume has been created

Attach the volume to the EC2 instance. Right-click the volume or click Actions and in the menu that opens, click Attach Volume.

Attaching the EBS volume to the EC2 instance

Select the instance to which you need to attach the EBS volume. You must select the EC2 instance ID. In my case, the instance ID is i-0071290000000cb84. You can select a device in a guest operating system used to access the attached volume. In my case /dev/sdf is used.

Click Attach to attach the new volume to the EC2 instance.

Useful information about renaming disk devices is displayed in the note. In my case /dev/sdf is renamed to /def/xvdf in a guest OS.

Options to attach the EBS volume to the EC2 instance

Two volumes are now attached to my EC2 instance (blog-temp):

vol-0617000000000763c

vol-0b4a0000000003d01

Two EBS volumes are attached to the EC2 instance that should be backed up using Amazon snapshots

Writing data on the second EBS volume

Next, I will create a file on the second EBS volume, take a snapshot, and restore the instance by restoring two volumes attached to the instance using snapshots. The file is used to check whether the data is has been recovered after restoring a volume from the EBS snapshot. For this reason, I will create a partition, create a file system, and mount the file system to Linux running on the EC2 instance as a guest operating system.

Use lsblk to display details about block devices in Linux; use df -h to check storage space on mounted disks; use mount to check mounted disks. In this case, I am focused on operations with EBS snapshots rather than operations in a guest Linux operating system running on the EC2 instance. However, I will list the commands used to better understand operations with volumes and data restoring processes.

Creating and formatting a partition on the second EBS volume in parted:

sudo parted -l

parted /dev/xvdf

(parted) mklabel msdos

(parted) print

(parted) mkpart primary ext4 1MB 955MB

(parted) print

(parted) quit

Creating a file system on the prepared partition:

mkfs.ext4 /dev/xvdf1

Creating the directory to use as the mount point:

mkdir disk2

Mounting the partition with the file system to the created directory:

mount /dev/xvdf1 /disk2/

Going to the directory of the mounted partition:

cd /disk2

Create the file on the partition located on the second EBS volume attached to the EC2 instance running Linux. This file is created to check whether the needed data has been preserved after restoring the EBS volume from the snapshot.

echo "EBS snapshot for disk 2" > /disk2/d2-file01.txt

Creating a file to be restored on the EBS volume by using the Amazon snapshot

Make changes on the first disk to create a new Amazon snapshot of the first EBS volume used by the EC2 instance. Both volumes should contain changes from the state when the previous snapshot was taken. I create the snapshot02.txt file on the first volume to more conveniently detect changes saved to the new snapshot of the first volume.

Adding a file to the home directory stored on the first EBS volume

Once two EBS volumes with new data are attached to the EC2 instance, we can create multi-volume snapshots.

Creating the multi-volume snapshot

Go to Elastic Block Store > Snapshots in the navigation pane of the EC2 instances management page. Click Create Snapshot.

Clicking the Create Snapshot button in the Amazon web interface to take AWS EC2 snapshots

Select resource type: Instance.

Enter the instance ID of the needed EC2 instance which volumes you want to back up by using EBS snapshots.

All volumes attached to the selected instance are selected automatically.

Enter a description to help you find the EBS snapshot in AWS when needed. In my example, the description is “EBS snapshot blog 02”.

Select the “Copy tags from volume” checkbox if you want to use tags assigned to EBS volumes.

Snapshots for all selected volumes are created after you finish this step and hit Create Snapshot.

EC2 create snapshot for multiple EBS volumes

Create Snapshot Request Succeeded. Two Amazon snapshot identifiers are displayed (snap-0f95000000000de71 and snap-0a91000000000769b in our case). You can save these ID values and click one of them to check the snapshot creation status. Right after initiating the snapshot creation, the operation status is pending. When the creation of the snapshots is finished, the status is completed.

EC2 create snapshot operation is started for multiple EBS volumes

A new snapshot for each volume has been created.

Using AWS CLI

You can use AWS CLI as an alternative to AWS Management Console if you prefer the command-line interface or you need to automate snapshot creation.

The command to create an EBS snapshot in AWS CLI is:

aws ec2 create-snapshot --volume-id vol-0b4a51e1cea333d01 --description "This is a test snapshot for the blog"

The output should look like this:

{

"Description": "This is a test snapshot for the blog",

"Tags": [],

"Encrypted": false,

"VolumeId": "vol-0b4a51e1cea333d01",

"State": "pending",

"VolumeSize": 8,

"StartTime": "2021-08-28T21:06:01.000Z",

"Progress": "",

"OwnerId": "012345678910",

"SnapshotId": "snap-0689749a39a7b822b"

}

The command to create a snapshot with tags:

aws ec2 create-snapshot --volume-id vol-0b4a51e1cea333d01 --description 'Prod backup' --tag-specifications 'ResourceType=snapshot,Tags=[{Key=Blog,Value=Test},{Key=AWS,Value=EC2-snapshot}]'

How to Restore an EC2 Instance Using EBS Snapshots

We have created snapshots of EBS volumes attached to the Amazon EC2 instance. These snapshots are a form of data backup in AWS. Now let’s find out how to restore the entire EC2 instance or particular EBS volumes in AWS.

Restoring the second EBS volume

Let’s restore the second EBS volume whose size is 1 GiB from the snapshot. This volume is used to store files, and the operating system is not installed on this volume.

Go to Elastic Block Store > Snapshots in the navigation pane of the EC2 Management Console page. I enter “blog” in the search field to display all snapshots created for EBS volumes of the EC2 instance used for writing this blog post. This is an example of why descriptions and tags are useful.

Select the snapshot of the 1-GiB volume and create a new volume from the snapshot. Right-click the snapshot and in the menu that opens, hit Create Volume.

AWS backup and restore – how to create EC2 instance from snapshot

Define the needed volume parameters on the Create Volume page – select the volume type, define the volume size, availability zone, encryption options, tags keys, and values. The availability zone must be the same as that of the EC2 instance (eu-central-1c in our case). The volume size is the same as the size of the original backed up volume (1 GiB) in our example. The tag key is blog and the value is recovered volume 1.

Hit Create Volume to finish and create the EBS volume using the EBS snapshot.

AWS backup and restore - recovering the EBS volume from the Amazon snapshot

Create Volume Request Succeeded. You can save the volume ID. The new volume id is vol-09cd000000000b502 in our case.

Amazon backup services are used to restore the volume from the Amazon snapshot

Restoring a system volume

If you need to restore an EC2 instance, you need to restore the EBS volume on which the operating system is installed. In this case, you can create an image from a snapshot of the EBS volume used by a guest operating system and launch the instance by using this image (the restored EC2 instance).

Creating the image

Go to Elastic Block Store > Snapshots in EC2 Management Console and select the snapshot needed of the EBS volume on which the guest OS is installed. I select the latest snapshot of the 8-GiB volume used by the guest OS (Ubuntu Server). This EBS volume snapshot was created when I created a multi-volume snapshot for the EC2 instance.

Right-click the snapshot and in the menu that opens, hit Create Image.

How to create EC2 instance from snapshot – creating an image

In the window that opens, enter the information needed and select the options to create an image from the EBS snapshot. I use the following parameters.

Name: blog-recovered

Description: Restored from an EBS snapshot (blog)

Other settings are left at their defaults.

Hit Create.

How to create EC2 instance from snapshot by using AMI

The Create Image request received dialog box opens. Click the image ID to view information about the image (ami-08ed800000000872a in our case). Wait until the image creation is finished.

AWS backup and restore – creating the image from the Amazon snapshot

Launching the image

Once the image has been created, right-click the image and hit Launch.

Launching the AMI to create EC2 instance from snapshot

The new EC2 instance creation wizard opens. You must select the instance options. You can select the options that are the same as the options selected for the original VM that was backed up. You can also restore the instance to a new more powerful EC2 instance.

Step 2. At the second step, I select t2.micro as the instance type. Click Next at each step of the wizard to continue.

AWS backup and restore – restoring the EC2 instance from Amazon snapshots

Step 3. Configure instance details to suit your requirements (network settings, IAM roles, shutdown behavior, etc.).

Step 4. Add Storage. This step is important when you recover an EC2 instance by using EBS snapshots. Click Add New Volume. A new line with volume options is added on this page. Enter the ID of the needed volume snapshot in the Snapshot field. I enter snap-0a91000000000769b, which is the ID of my snapshot of the 1-GiB volume. The 8-GiB size of the volume is selected by default in my example. I can set the volume size to 1 GiB as configured for the original EBS volume. You can also select the storage device, volume type, encryption, and delete on termination options.

Attaching the EBS volume recovered from the Amazon snapshot to the EC2 instance

Step 5. Add Tags. Remember to make the tags useful for search purposes. Tags are applied to all volumes and instances. In this example, the tag key is blog and the value is recovered VM 01.

AWS backup and restore – adding tags to a recovered EC2 instance

Step 6. Configure Security Group. Edit the parameters to control the network traffic for your EC2 instance.

Step 7. Review Instance Launch. Review your instance launch details and hit Launch to finish. You must select a key pair or create a new one to access the EC2 instance.

The Launch Status page is now displayed. Click View instances.

AWS backup services can be used to restore EC2 instances

Enter a name for the recovered instance (blog-recovered in our case). The status of the recovered E2 instance is running. Both EBS volumes recovered from the EBS snapshots are attached to the EC2 instance (see the screenshot below).

AWS backup and restore – the EC2 instance has been restored from Amazon snapshots

Checking the restored files

Let’s check whether our files created before taking the latest snapshot are recovered.

First, list the attached block devices:

lsblk

Second, list the files of the current (home) directory of the user:

ls -al

The snapshot01.txt and snapshot2.txt files created before taking the first and second snapshots of the EBS volume on which the OS is installed are present.

Mount the partition located on the second disk (EBS volume):

sudo mount /dev/xvdb1 /disk2/

The partition name has been changed to /dev/xvdb1. Keep in mind that you may need to edit /etc/fstab after restoring the instance for auto-mounting.

ls -al /disk2/

d2-file01.txt is present on the file system stored on the second EBS volume after restoring.

AWS backups were used to restore EBS volumes with files stored on them

The AWS EC2 instance, with all attached EBS volumes, has been successfully restored from EBS snapshots. All needed data is preserved. Note that additional costs are incurred when storing EBS snapshots in the Amazon cloud because additional cloud storage is used.

Conclusion

This blog post covered AWS backup and restore for EC2 using snapshots with detailed examples. EBS snapshot is a native feature to back up and restore Amazon EC2 instances by restoring storage data. You can back up a single volume or back up all volumes by using multi-volume snapshots. When the time to recover EC2 instances comes, you have to restore the EBS volume on which the operating system is installed from a snapshot, restore other EBS volumes, and attach them to the recovered EC2 instance. You can create an Amazon Machine Image (AMI) from the snapshot to restore the instance and system storage.

EBS snapshots are created on the block level, and the snapshot creation system is not aware of files that are being written at the moment the snapshot is taken. As a result, backups made with EBS snapshots are crash-consistent and not application-consistent.

NAKIVO Backup & Replication supports the creation of application-aware backups of Amazon EC2 instances. The solution supports Microsoft apps such as SQL Server, Active Directory, Exchange, and others. Backup jobs can be run automatically. You can store Amazon EC2 backups in a backup repository created in a custom location on-premises and in the cloud. The product also supports the application-consistent replication of running Amazon EC2 instances. You can recover the entire EC2 instance from a backup made with NAKIVO Backup & Replication without the need to recover EBS volumes, create an EC2 instance, and attach the volumes manually to a created EC2 instance. Download NAKIVO Backup & Replication Free edition and start protecting your AWS EC2 instances today.

Let’s Stay in Touch

Subscribe today to our monthly newsletter
so you never miss out on our offers, news and discounts.