Create AMI, duplicate, backup & restore in AWS

Understand how AWS manage your instance

Binh Nguyen Xuan

Today when trying to fix some issue relevant with wkhtmltopdf on my AWS server, I uninstalled postgresql accidentally. That is the dumbest thing I've ever done. Uninstall PSQL mean there is no db on my server anymore. WTF??? You know, when yourself in a hole, you can not think about anything, just copy and paste :((

Fortunately, I’ve just created an AMI (Amazon Machine Image) last night. Thank God, everything seems easy from now.

So, How did I do to fix my stupid?


Backup (Create an AMI)

Right click on your running instance: Image > Create Image












Please pay attendant on Snapshot and Size.












When you create an AMI, it will be create a snapshot of your virtual disk as well. You can choose size >= current virtual disk. That's all, we finish the backup. Very simple, right?

AWS is very awesome. It support 2 ways to restore instance. Duplicating and Mounting.


Duplicating

Instances > Launch Instance > Select My AMIs > Click “Select”









All next steps, you can follow this tut, for simple, when aws ask you to create new key pairs, you should reuse the old one which was used for current instance.

Mounting

ELASTIC BLOCK STORE > Snapshots

You can see the snapshot which was created in “Create AMI” step above. Right Click > Create Volume

You must select same Availability Zone with current instance (important)













Click on create, you will see new volume at ELASTIC BLOCK STORE > Volumes.

Now you have many choices:

  • Replace old virtual disk by new one (new volume just created).

  • Mount virtual disk and copy files.

Replace old virtual disk

Detach old one

  • Stop current instance

  • ELASTIC BLOCK STORE > Volumes > Right Click > Detach

Attach new one

  • ELASTIC BLOCK STORE > Volumes > Right Click > Attach












Instance: Select instance you detach disk in prev step.

Device: must be “/dev/sda1

  • Start instance. DONE.

  • Now you can access your old instance with new volume.


Mount Virtual Disk

Attach new one to current instance

ELASTIC BLOCK STORE > Volumes > Right Click > Attach










Instance: Select instance you detach disk in prev step.

Device: should be “/dev/sdg

Mount

# lsblk
# mkdir /mnt/recovery
# mount /dev/xvdg /mnt/recovery

Copy any files you want to the old volume.


Unmount

# cd /
# umount /mnt/recovery/


Hope this help.