It is essential to have AWS ec2 backup using EBS volumes for data recovery and protection. AWS EBS snapshots play an important role in the backup of your ec2 instance data (root volumes & additional volumes).
Imagine an application which requires 24x7 availability with maximum 15 minutes of downtime allowed. How would you ensure the database hosted on your Elastic Block Store (EBS) volume is backed up to meet the required SLAs?
An automated backup is the key process here. Automated backups work in the background and don’t require manual intervention. When you want to backup the data, Amazon Web Service Application Program Interface (AWS API) and Command Line Interface (AWS CLI) both play major roles in the automation process, letting you write automated scripts.
In this article, we will discuss why timely snapshots are so important for data recovery. Also, we will show you how to automate the backup of EBS volumes using AWS Console & AWS CLI.
Automation EBS Snapshot with Life Cycle manager
AWS EC2 lifecycle manage is a native AWS functionality to manage the lifecycle of EBS volumes and snapshots.
It is the quickest and easiest way to automate EBS snapshots. It works on the concept of AWS tags. Based on the instance or volume tags, you can group EBS volumes and perform snapshot operations in bulk or for a single instance.
Follow the steps given below to setup a EBS snapshot lifecycle policy.
Step 1: Tag your ec2 instance and volumes
EC2 EBS snapshots with the life cycle manager work with the instance & volume tags. It requires instances and volumes to be tagged to identify the snapshot candidate.
You can use the following tag in the instances and volumes that needs automated snapshots.
Tagged instance:
Tagged Volume:
Use same tag in Lifecycle Manager
Step 2: Go to the EBS life cycle manager to create a snapshot lifecycle policy.
Go to the EC2 dashboard and select “Lifecycle Manager
” option under ELASTIC BLOCK STORE
category as shown below.
Step 3: Add EBS snapshot life cycle policy rules
Enter the EBS snapshot policy details as shown below. Ensure that you select the right tags for the volumes you need the snapshot.
Note: You can add multiple tags to target specific Volumes
Enter EBS snapshot schedule details based on your requirements. You can choose retention type for both count & age.
Also apply proper tags to identify the snapshots.
Here we can show 2 snapshots created using this backup policy:
How to Take Snapshots of EBS Volumes using AWS CLI
Here is an example of how to use AWS CLI to take a snapshot of an EBS volume.
The backup is taken by filtering for any volume tagged “ebs-backup.” In this example, we will find the volumes with that tag, the instance associated with those volumes, and the block of the volume that is attached to the instance. Then we will create a snapshot of the volume with the description and the instance name.
1. Tag Your EBS Instances
The EBS backups will be taken using AWS CLI. The scripts can be written in any programming language (such as like Bash or Python) to take the backup of the instance.
The key is to identify which instances are going to be backed up. For that, tagging is used.
2. Creating Snapshot Permissions
To take EC2 Snapshots, the EC2 instance should be provided access to communicate with the AWS resources. The best way to do this is to create a small EC2 instance on which the script will run in the background.
This EC2 instance should have enough access to take the snapshots. An identity and access management (IAM) role must be created and the policy to create snapshots must be attached to this role: an EC2 full-access role is sufficient to create the snapshots.
This script assumes that you have the AWS CLI installed and configured with the necessary IAM permissions.
#!/bin/bash
# Replace these values with your own
AWS_REGION="ap-south-1"
VOLUME_ID="vol-0ed0d774813160251"
DESCRIPTION="Daily Backup $(date +%Y-%m-%d)"
# Create EBS snapshot
SNAPSHOT_ID=$(aws ec2 create-snapshot --region $AWS_REGION --volume-id $VOLUME_ID --description "$DESCRIPTION" --query SnapshotId --output text)
# Print the snapshot ID
echo "Snapshot created with ID: $SNAPSHOT_ID"
Save the script with a .sh
extension, for example, ebs_backup.sh. Don't forget to make it executable:
chmod 700 ebs_backup.sh
For Taking Auto snapshot backup:
Open the crontab file for editing:
crontab -e
Add a line to execute your script in every every two minutes:
*/2 * * * * /path/to/your/backup_script.sh
We can see the output:
In every 2 minute new snapshot created using this script: