EC2 On-Demand Backup & Restore
Tags: aws, cloud, devops
Published: 2022-09-21 00:34:38

Video Version: YouTube [link](https://youtu.be/D3hAcW908Mg)

In this article we will explore two AWS solutions to backup & restore an instance:
1. EC2 snapshots and AMIs
2. AWS Backup (and its drawback)

## Why do I need to do an on-demand backup?
It is always a good idea to take a backup of your instance before making any changes like windows updates, drivers updates or any other system update, so you have backup to fall back on if changes don't go as planned.

## Solution1: Snapshots and AMIs
This solution creates a snapshot of a root volume. During restore, you create a volume out of the snapshot. Once new volume is created, it get swapped with an existing root volume of the instance. Below are the steps how it works.

### Backup
1. *Go to EC2 service --> In the left pane, select instances*
2. *Select the desired instance --> Click Actions*
3. *From drop-down, click on Image and templates --> create Image*
4. *Enter in Image name ```
my_instance_image
``` --> Click Create image*
5. Make note of snapshot id that prompted at the top.

### Restore

#### Create a Volume
1. *Go to EC2 service --> In the left pane, select instances*
2. *Select the desired instance --> Stop the instance*
3. *In the detail(bottom) pane, switch to Storage*
4. *Make note of Root device details i.e. /dev/sda1 (different for windows)*
5. *In the left pane, click snapshots under Elastic Block store -->  Select the snapshot created in the Backup process above*
6. *Click Actions --> Click create volume* 
7. *Verify default options i.e. AZ zone should be same as instance --> Add tag `Name:RestoreVol`* 
8. *Click create volume*

#### Detach an existing volume

1. *In EC2 console --> In the left pane, select instances*
2. *Select the desired instance --> Stop the instance*
3. *In the detail(bottom) pane, switch to Storage --> Click root volume (device name = Root device name) under Block devices*
4. *Select volume --> Click Actions & from drop-down, click Detach volume --> click Detach*

#### Attach a new Volume

1. *In EC2 console --> In the left pane, select volumes*
2. *Select ```
RestoreVol
``` volume --> Click Actions*
3. *From dropdown, click Attach volume -->Select the instance*
4. *Device name /dev/sda1 --> Click attach volume*

The Instance HAS BEEN RESTORED. Start the instance and access as you usually would. 

## Solution2 : AWS BACKUP
AWS Backup is a convent way to take backups with a couple of clicks and restore the backup to a new machine.

### Prerequisite: Backup Vault 
Before you could backup an instance using AWS Backup. Backup Vault needs to be in place. Backup Vault is a storage location where resources get backed up

#### Create Backup Vault
1. *Go to AWS Backup --> In the left pane, select Backup Vault*
2. *Click Create backup vault  at the right --> Enter in Backup Vault name `
my_vault
`*
3. *Select default(aws/backup) Encryption Key --> Add tags if required*
4. *Click Create backup vault*

Once the vault is created, on-demand backup of your instance can be taken.

### Backup your instance
1. *Go to AWS Backup --> In the left pane, select Backup Vault*
2. *Select the vault you just created --> Under Recovery points, click on Create on-demand backup* 
3. *Select EC2 Resource type and your instance for instance ID*
4. *Backup window, select Create backup now --> Retention period, select `Days` and leave `1 day`*
5. *Backup Vault, select `my_vault`--> IAM role, Keep Default role selected*
6. *Click Create on-demand backup*

### Restore
1. *In AWS Backup --> In the left pane, select Protected resources*
2. *Select the instance --> Under Recovery points, select Recovery point ID*
3. *Leave everything default or update the values as desired --> Restore backup*

Go to EC2 console, a new restored instance would have been created. 

### Drawback of this solution:
New instance get created with new instance ID, IP address, interface ID etc. and old instance needs to terminated. In Enterprise environments, this could result in issues as old Instance IP or ID could have been serving other purposes.

To avoid creation of a new instance. Use AWS backup to backup EBS volume of the instance and then detach/attach as suggested in snapshot and AMI backup and restore process.
 
In addition to on-demand backup, scheduled backups can be enabled for your instances like daily, weekly or monthly and how long you would like to retain them. I'll cover more of those in future articles 

#### Cleanup
If you followed the steps suggested above, please remove all those resource to avoid getting billed for them.