AWS: Backup Options for Your EC2 Instance

AWS: Backup Options for Your EC2 Instance

AWS: Backup Options for Your EC2 Instance

Introduction

Today cloud services are very popular, because you can rent a server in the cloud and pay only for the resources you consume. Amazon Web Services provides a website hosting which is called EC2 (Elatic Compute Cloud). A server in the cloud is called an EC2 instance.

When you run a website on an AWS AEC2 instance, one of the tasks you have to do is keeping your data in
healthy state.

You have to think of making backups of your data to avoid its lost on some software or hardware failure.

A backup is just a copy of your data at some point of time. If something bad happens to your website, you can quickly recover to a previous state.

In this article we will consider several methods of making backups: manual snapshots of the EBS volume,
backing up your data with scripts and AWS command line tool (CLI), and creating
a backup plan with the AWS Backup service.

In this article, we assume a simple setup that you have an EC2 instance: a single EBS volume, and a Linux
operating system. We also assume you run your website with the Apache HTTP server and the MySQL database.
If your setup is different, you still can use most of instructions provided in this article, but refer to AWS documentation for more information/instructions relevant to your environment.

Typical Backup Procedures for a Web Server

The simplest procedure you can use to make a backup of your website on any web hosting, not only on EC2:

  • Store all your source code in a Git repository, so you can easily check out the latest code even if
    it is lost on your server.
 
  • Use the MySQL dump tool to make a backup of your MySQL database, for example:
mysqldump -u  -p  > /home/ec2-user/backup-2021-02-24.sql

You can even schedule the dump procedure on daily basis as a cron job:

crontab -e

Add the following line to your crontab:

0 0 * * * mysqldump -u  -p  > /home/ec2-user/backup-`date +%d%m%y`.sql

Now your backup will be performed every midnight.

This backup procedure is very simple, and good for small websites. However, this method will not help if
something bad happens with your EC2 instance and it becomes inaccessible. To make your backup even more reliable, you can make a snapshot of your whole EBS volume.

Creating Manual Snapshot of EBS Volume

In EC2, data is usually stored on an EBS volume (Elastic Block Storage) attached to your instance. EBS is
very fault-tolerant, which means AWS takes care of electricity outages and hardware failures. But you still have to worry of keeping your data healthy.
Situations when your data may become unhealty include the following:

  • someone unintentionally deletes the system files (for example, if some junior developer always works as root);
  • someone unintentionally drops a database table or makes a wrong ALTER TABLE or UPDATE command;
  • there is a malicious software on your website which corrupts your data intentionally;
  • someone performs a hacker attack on your server and makes it unusable.

In these cases you may need to somehow recover from the failure and restore your data. To do this, you need to regularly
make copies of your EBS volume with all data it contains. It looks a bit complex to copy gigabytes of data at first sight, but AWS makes incremental backups.

This means only the blocks of data changed since last backup are stored, reducing the size and cost of the backup.

One moment to remember here is that you should always stop your EC2 instance before making the EBS volume snapshot. This is to ensure all data cached by your operating system are saved to the EBS volume. If you make a snapshot without stopping the instance, you may have incomplete/unusable copy of your data.

To make an EBS volume backup, you need to do the following:

  1. Log into the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the left side of the page, look for the Instances section. Click it to expand. In the list of instances,
    click the EC2 instance you would like to backup.
  3. In the Instance state dropdown, click Stop instance. This will write all data cached by OS and file system to EBS volume.
  4. In the left side of the page, look for the Elastic Block Store section and click the Snapshots link.

  1. Click the Create Snapshot button. The Create Snapshot page should appear.
 

2. In the Select resource type field, choose the EC2 Instance for which you would like to make a snapshot. Optionally enter
some meaningful description, for example “Manual Snapshot for Website 2021-02-24”.

3, Click the Create Snapshot button. This will start the snapshot creation process. You will be able to see your new snapshot in the list of snapshots.

4. Start your EC2 instance again from the Instances page.

This method is reliable, but it has disadvantages:

  • you have to stop your instance before making a EBS volume snapshot;
  • you have to do this manually each time you want to make a backup.

Automating Backups with a Script

AWS provides a console tool (CLI) that you can use to automate your EBS volume backup creation.

First you need to download and install the AWS CLI:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Then configure the AWS CLI by typing the following command:

aws configure

AWS Access Key ID [None]: 
AWS Secret Access Key [None]: 
Default region name [None]: us-west-2
Default output format [None]: json

The configuration process will request the access key ID and the access key. You can get those credentials from your AWS IAM service.
Instead of region “us-west-2” substitute the region where your EC2 intance is located. The default output format
can be json or text, at your choice.

Next create a bash script which will make the backup:

touch ec2_backup.sh

In that file enter the following AWS CLI commands:

# Stop instance first to write cached data to EBS
aws ec2 stop-instances --instance-ids ""

# Sleep for some time to let the instance stop
sleep 10

# Start the snapshot creation
aws ec2 create-snapshot --volume-id  -description "EBS Volume Snapshot -`date +%d%m%y`"

# Start instance
aws ec2 start-instances --instance-ids ""

Then you can use cron to schedule this task as a cron job every midnight:

crontab -e

In the contab file, add the following line:

0 0 * * * bash /home/ec2-user/backup.sh

One disadvantage of this method is that you should run this script from another EC2 instance (not the one you make backup from).
Although this script is rather reliable, but it may happen that something is wrong with another instance and the backup is not run.
Because of that you can use even more reliable method called AWS Backup.

Using the AWS Backup Service

AWS Backup is a service designed to simplify backup automation for EC2 and some other AWS services. AWS Backup has the following advantages:

  • it is highly reliable since all backup operations are managed by AWS;
  • you don’t need to write any script, which reduces possible mistakes;
  • you clearly see what happens with your backups through the Dashboard page;
  • you control how often your backups are made and for what period they are stored via a backup plan;
  • you can easily assign your EC2 instances to the backup plan you created by a tag or instance ID.

To enable AWS Backup for your instance, you need to do the following:

  1. Log in to your AWS console and open the AWS Backup service: https://console.aws.amazon.com/backup/.
  2. In the left side of the window, click the Backup Plans link. The page that appears will contain the list of backup plans you will have.
  3. Click the Create Backup Plan button.
  4. On the page that appears, choose Start with a template.
  5. In the Choose template dropdown, select Daily-Weekly-Monthly-5yr-Retention.
    This plan will create backups every day, once a week, and once a month; and store them for up to 5 years.
  6. In the Backup plan name input, enter your backup plan name, for example “website_backup_plan”.
  7. Click the Create plan button.

Next you need to assign your EC2 instance to the backup plan you created.

  1. On the Backup plans page, click the backup plan you created.
  2. In the Resource assignment section, click the Assign resources button.
  3. In the Resource assignment field, enter the name for your assignment, for example “website_resources”.
  4. In the Assign resources section, choose Assign By => Resource ID. In the Resource type field choose EC2. In the Instance ID field, choose your EC2 instance.
  5. Click the Assign resources button.

Now AWS Backup will make backups of your EC2 instance according to the schedule. You can see what backups were made and when on the Dashboard page.

Conclusion

Backups are very important, since they allow to protect your data from loss or corruption. A backup is just a copy of your data
at some point of time in the past. If something bad happens to your website,
you can quickly recover to a previous stateIn this article, we covered several possible ways to make backups of your website hosted in the AWS EC2 service.

The simplest way is to create an SQL dump of your database. The more advanced ways are to make snapshots of your
EBS volume manually or automate that process with a script. The last method we considered is using AWS Backup,
which seems to be the most reliable method, and it is the method we recommend preferring to all others.

Looking for a Tech jobs? 

Discover more on Meritocracy.is! 

Leave a Reply

Your email address will not be published. Required fields are marked *