In my infinite wisdom, I decided to backup my Mailserver which is an Instance Store based AMI. It should have been a completely straight-forward task, but the reality is, nothing ever is. Somewhere along the EC2-Bundle-Vol process, the rsync process decided to go into uninterruptible sleep. Ahh the fun of EC2! rsync to the loopback just hung. This leaves me with one little problem. How do I go about backing up my Instance-Store AMI when the EC2-Bundle-Vol script won’t run now? The problem is I can’t specify the loopback interface the command uses without actually modifying source code and personally, I hate doing that!
This would normally leave me without a recent backup of all of my email. (Something I’m not willing to do), nor do I feel like backing up all of my email only to have to restore it, sync up the mailservers, etc. Just a bit of a pain right?
Well, there’s a really easy solution to this little situation. Simply, just roll an EBS based AMI from your current running Instance Store (ephemeral storage-based instance). Everything is there and all I have to do is just swap the elastic ip! Total Downtime? uhm.. maybe 3 seconds.
So here’s the quick and easy solution for me.
First things first. I shut down the postfix process on my primary email server (my secondary and tertiary are all relays anyway). Mail is now going to the secondary.
Now, I’m going to create an EBS volume (and add a bit more space for expansion, because I’m too lazy to migrate to a larger filesystem later). 600 GB should be enough.
I mount it on the mailserver as /dev/sdi via the amazon console. (The correct nomenclature is “attach”).
First things first, let’s format the newly attached volume.
after a few minutes, it’s done.
now we mount it somewhere. I mounted mine on /mnt/newsnap
[quote]mkdir -p /mnt/newsnap[/quote]
Yes.. I used the -p flag. It’s an old habit I’m trying to break. the flag simply creates the directory structure if it doesn’t exist.
now to mount it:
[quote]mount /dev/sdi /mnt/newsnap[/quote]
Three rsync commands need to be issued:
- one for the root filesystem and everything on it.
- the second one for my ephemeral storage (mounted as /dev/xvdf) which actually holds all of my mail. I just mounted ephemeral to /home.
- The third was the /dev directory, because I really couldn’t be bothered with waiting for the AMI to start and create a bunch of new devices (the third rsync shaves about 10 min. from the initial ami boot time). Remember to add the trailing slash to your source directory so it doesn’t create a new subdirectory structure under it.
rsync -a –delete –progress -x / /mnt/newsnap
rsync -vaz /home/ /mnt/newsnap/home
rsync -avHx /dev /mnt/newsnap
After this, the data’s now on the ebs volume, but we’re not done yet. I need to create a couple of devices that don’t exist on the new filesystem.
[quote]MAKEDEV -d /mnt/newsnap/dev -x console
MAKEDEV -d /mnt/newsnap/dev -x zero
MAKEDEV -d /mnt/newsnap/dev -x null[/quote]
This simply creates the 3 devices you’ll need to actually get your ami to run on your soon-to-be new ami.
now remember to unmount your ebs volume.
Now, let’s just run one little command. I like to do this, some don’t, but it takes 3 seconds. DO IT! Some AMIs won’t start correctly if you don’t. Bah. GOGO Amazon!
[quote]tune2fs / /dev/sdi[/quote]
Next step, follow me here people.. We’re almost done.
create a snapshot of the ebs volume you created.
[quote]ec2-create-snapshot vol-xxxxxxx [/quote] (get the volume id from aws console).
it will return a snapshot id. You’ll need to wait until this is finished. I would walk away, read a magazine article, call your girlfriend, etc. Do something constructive (maybe buy my daughter a lolipop?)
[quote]watch -n 3 ‘ec2-describe-snapshots snap-xxxxxxx’ [/quote] (whatever the snapshot id is). You’ll need to run this command and wait for it to return a “complete(d)” response. It’s going to take a while.
OK.. So you’re back and my daughter has a lollipop thanks to you. :) (snicker.. yes, she’s my everything).
Let’s go ahead and register the ami, shall we?
Log into your aws console, check the following information from your existing (running instance):
kernel ID: aki-xxxxxxx
Ramdisk ID: ari-xxxxxx
execute the following command from the command line if you don’t have an ari, just leave it out (like I did):
[quote]ec2-register –snapshot snap-xxxxxxx –description “My Shiney new Mailserver” –name “Percy’s Mailserver because I was lazy” –root-device-name /dev/sda1 –architecture x86_64 –kernel aki-825ea7eb[/quote]
or if you have a ramdisk:
[quote]ec2-register –snapshot snap-xxxxxxx –description “My Shiney new Mailserver” –name “Percy’s Mailserver because I was lazy” –ramdisk ari-xxxxxx –root-device-name /dev/sda1 –architecture x86_64 –kernel aki-825ea7eb[/quote]
Launch your new server from the ami you just created and log in and check it. All should be happy. If it’s not, you did something wrong. Go through the post again, you got sloppy and missed a step. You’re not exactly following this post, because you’re bored. You screwed something up (or Amazon broke). Switch the EIP back and test the system. I bet it’s working perfectly!
Remember to reboot it once to make sure it comes up ok