8
votes

I have an EBS backed EC2 running instance. The EBS store holds a database. I would like to create an AMI from this. Can someone please provide guidelines?

  1. Do I have to shut down MySQL?
  2. Will my AMI attempt to connect to the same EBS store?! (disaster)
  3. Will creating the AMI automatically snapshot my EBS volume and create a copy? (hoping it's this one)

Thanks for your patience through a long question. I recognize there's similar information out there, but nothing really addresses what may go wrong.

This SO question:

http://stackoverflow.com/questions/4475532/creating-an-ec2-ami-with-an-ebs-backed-instance-is-it-possible

is specific to ElasticFox and I'm not using any tools (prefer command line). I don't believe the question is answered either.

This blog:

http://instantbadger.blogspot.com/2009/09/how-to-create-and-save-ami-image-from.html

while instructive about creating an AMI, does not mention EBS at all and I'm a little worried about all that data.

Thanks in advance!

4

4 Answers

3
votes

Amazon console gives you an option to do this STUFF.

Just, login on to your aws console and navigate the instance-ID you wish to create an AMI.

  1. Right-Click on the instance and click on "Stop".
  2. Right-Click on the instance and click on "Create AMI".
  3. Type-in name for your AMI & hit OK.

That's it. This would have created an AMI. Please see: http://docs.amazonwebservices.com/AWSEC2/2011-05-15/UserGuide/index.html?Tutorial_CreateImage.html for a detailed information.

2
votes

Before going to your questions I would suggest that you back up your MySQL database to a file which is not stored as a snapshot or in an EBS before you start creating the image. (And of course test that you can restore it to somewhere else than your production system.)

I use:

mysqldump --add-drop-table -u root -p databasename > database.sql

to backup and:

mysql -u root -p databasename < database.sql

to restore.

  1. No, you do not have to shut down MySQL, but you have to prevent writes to the database while create the AMI.
  2. No, The creation of a AMI also creates a new snapshot of your EBS volume (with the content as it was at image creation time).
  3. yes, see 2

A bit more info: I prefer to shut down the database when possible when snapshoting or creating images. That said I am not by any mean an MySQL expert, but here is some guidance from http://aws.amazon.com/articles/1663?_encoding=UTF8&jiveRedirect=1

Start a MySQL session on the instance, using the password you set above.

mysql -u root -p

In the mysql session, flush the tables to disk and acquire a lock. Flush the file system to disk and freeze it. Do not exit the MySQL session or you will lose the lock and snapshot potentially inconsistent database files!

FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
SYSTEM sudo xfs_freeze -f /vol
1
votes

Make sure you always backup you MySQL by dumping as suggested by steenhulthin.

This link will help on creating your new EBS backed AMI: http://www.capsunlock.net/2009/12/create-ebs-boot-ami.html

I also suggest to place your MySQL data to a separate EBS volume or better RAIDed volumes.

0
votes

as @steenhulthin mentioned you should do the FS Freeze and put database into a readlock mode to take a consistent backup else you will have a crash-consistent only

Types of Backups https://asvignesh.in/backup-types-inconsistent-app-consistent-crash-consistent/

You can have an SSM Command to do the FLUSH and Lock the tables of the databases which will bring you the consistency of the MySQL Data and create an Image from the console and once you trigger the AMI creation you can unlock the tables Make sure you run the Flush tables in the background script, once you close the session the lock will be released in MySQL

You can find the scripts to lock and unlock the table which you can invoke through SSM https://asvignesh.in/mysql-app-consistent-backup-using-nimesa/

Here is the script for just Filesystem freeze https://asvignesh.in/consistent-backup-of-your-ec2-instance/