Wednesday, May 25, 2016

Taking Backup of VM instances on AWS

If you are familiar with saving the states of your VM instances, you will know that you can take snapshots.  From VMWare documentation:
“On vSphere, backups are usually done by taking a snapshot, to efficiency obtain a static image of the virtual machine. Snapshots are a view of a virtual machine at a certain point in time, and enable quick and clean backup operation. Snapshots also provide an incremental backup mechanism called changed block tracking. ”
Well the equivalent for AWS is taking AMI which stands for Amazon Machine Image. With AMI, you can launch instances and specify as many instances as you need.  It specifies  a template for the root volume for the instance, launch permissions that control which AWS Accounts can use the AMI to launch instances and a block device mapping that specifies the volumes to attach to the instance. If the AMI is EBS backed, it is automatically registered. We can also take snapshots of an EBS volume that the VM is using. EBS volume snapshots also help to launch instances. We can do both for making sure no information is lost on recovery.

Here’s the sample code to do it:
        import boto3
        ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=accesskey,
                          aws_secret_access_key=accesssecret)
        instances = ec2.instances.filter(
        Filters=[{'Name': 'tag:Name', 'Values': [name]}])
        for instance in instances:
            print('instance_id='+instance.id)
            import random
            response = instance.create_image(Name=name+"Backup"+str(random.randint(1,99999)), InstanceId=instance.id, Description='Full Backup', NoReboot=False, DryRun=False)
            print(response.id)

The image created by the steps above is EBS-backed. If the instance was customized with instance store volumes or EBS volumes in addition to the root device volumes, the new AMI contains the block device mapping information for those volumes. Because the volumes are independent and their mapping only uses their reference, when the instance is launched from this AMI, all those additional volumes are also available.
Instance.volumes.all() gives the iterator for all volumes attached the instance.
An AMI can also be created from a snapshot of the root device volume. However, some AWS OS flavors have billingProduct code associated with an AMI to verify subscription status for package updates and this is not maintained when creating an AMI from an EBS Snapshot. Therefore instances cannot be launched successfully from such AMI.


Alternatively, if the root device volume is snapshot, then instances can be launched from the EBS Snapshot.

#codingexercise
Get the intersection point of two linked lists
Find the count of one linked list as c1
Find the count of another linked list as c2
Traverse the longer list by abs(c1-c2) nodes
Then traverse them together one node each until a match.

Find all connected components in a graph
int getCount(int[,] adj)
{
int count = 0
for (int i =0; i < row; i++)
   for(int j =0; j < col; j++)
    if (adj[i,j] && !visited[i,j]){
          DFS(adj, i, j, visited);
          ++count;
    }
}
#coding exercise
Add two numbers where the digits are represented by nodes in a linked list 
Find the  lengths of both the linked list.
If they are of equal size recursively go down both lists until the last and then add and carry forward to stack unwind
If they are different lengths, then walk down the longer until the size are same.

1 comment:

  1. Yes, it is helpful... thanks for sharing the valuable coding for AWS EC2 automated backup, given coding is perfect. thank you for sharing.

    ReplyDelete