Once I got my Amazon EC2 instance up and running, I was stoked. However, then next worry I had was how to back up all of the work I had done to set it up. Here, I’ll show you how to back up your EBS-based Amazon EC2 instance and how to restore it.
Is It an EBS-Based Instance?
These instructions apply to Amazon EBS (Elastic Block Store) based instances only. To see if your instance is EBS-based, log into the EC2 dashboard, and click Instances. Find the instance that you want to back up and select it by checking its checkbox. Look in the Description tab for “Root Device Type”. If it says “ebs”, then you’re all set with these instructions. Note that micro instances are all EBS-based.
Creating the Snapshot
To create the snapshot, click “Volumes” in the “ELASTIC BLOCK STORE” section of the left menu bar. If you have several, note the volume ID of the one that you want to back up. If you have only one, no need to do anything here.
Next, click “Snapshots” in the left menu bar and then hit the “Create Snapshot” button. Select the volume that you want to back up (if you only have one, choose that one), then enter a name and description for the snapshot. Hit “Yes, Create”. After a few minutes, your snapshot will be done! Your site will not be affected during the backup.
Here is one caveat though. The Amazon documentation says that if your volume is serving as the root device (i.e., you boot from that volume), or if there could be pending disk write activity going on, you should unmount the volume, issue the snapshot command, then remount the volume (it is okay to remount while the snapshot is pending).
You can unmount the volume by issuing these instructions from the command line:
sync umount /dev/sda1
Where “/dev/sda1” is your device path (also sometimes “/dev/sdf”).
Restoring Your Snapshot
Okay, let’s say that you want to restore one of your snapshots. You’ll have to create a new Volume from your snapshot. Click “Snapshots” in the left menu and check the box next to the snapshot that you want to restore. Click the “Create Volume” button on the top (or right click on the desired snapshot and pull down this option).
In the popup, under Volume Type, select “Standard”. Select volume size you want (usually the minimum), then the availability zone. Very important, if you pick the wrong availability zone, you won’t be able to attach the volume to your instance! Make sure you know which availability zone your instance is in and use that one! Click “Yes, Create” to start the process, then wait a few minutes until the status shows that it’s completed.
Disconnecting the Current Volume and Connecting the Backup
Now your website is going to experience a bit of downtime. Go back to the console and click “Instances” in the left menu and check the box next to your instance. Click the “Actions” button and choose “Stop”. At this point, your site will go down. Wait a few minutes until the instance is completely stopped. Don’t panic if it doesn’t happen immediately. It could take a few minutes.
Next, click on “Volumes” in the left menu and find the one that is currently in use (not the one you just created, but the one being used right now) and check the box next to that one. Click the “Actions” button and choose “Detach Volume”. When the popup asks “Are you sure?”, select “yes”.
When detached, uncheck that volume and check the volume that you just created. Click the “Actions” button and choose “Attach Volume”. Fill out the popup that comes up. For the Device path, the default value of “/dev/sdf” didn’t work for me. I had to use a value of “/dev/sda1”. If you’re not sure what your Device path is, go to the left menu under Images and select AMIs -> Details. Under “Root Device Name”, you’ll see your Device path to fill out in the form below:
Restarting Your Instance
After this is completed, go back to your instance and start it (the same way you stopped it). If you get an error, check the Device path to make sure it matches what is in the error message.
When done, your site won’t be live immediately, it takes a few minutes to boot up, so don’t worry if you don’t see it right away. Just wait, and your site should be restored to the snapshot!
Remember to get rid of the old volume if you don’t want to pay for it.
Also, if you are using the URL generated by Amazon to view your site, it will change after the restore, so take note of the new URL. If you are using an Elastic IP address, you will have to re-assign that as well.
Please leave comments or corrections below! – Brian