Python script to take Amazon volume backup of an instance
Assumption:
- AWS access key has a privilege to create snapshot.
- Aws Tag name has to be passed as an argument.
- Replace aws access key and secret key with proper keys
#!/usr/bin/python #Author:Dhanasekaran N #Email:dhanasekaran.n16@gmail.com #Version:1.0 #Here come I....... from datetime import datetime, timedelta import datetime import time from dateutil import parser import sys,os,time,re,argparse import boto,boto.ec2,boto.utils conn=boto.ec2.connect_to_region('ap-southeast-1', aws_access_key_id='XXXXXXXXXXXX',aws_secret_access_key='XXXXXXXXXXXXXX'); reservations=conn.get_all_instances() all_volumes_info = conn.get_all_volumes() command_line_instance=sys.argv[1]; for name in reservations: for instance in name.instances: aws_inst_name=instance.tags.get("Name") #print aws_inst_name if(aws_inst_name == command_line_instance): print "perfect" + command_line_instance; for volumes in all_volumes_info: if volumes.attach_data.instance_id == instance.id: print "Taking backup of volumes"; snapshot=conn.create_snapshot(volumes.id,command_line_instance); print "Instance ID:%s" %(instance.id); print "Volume ID:%s" %(volumes.id); print "Instance Name:%s" %(aws_inst_name); print "Snapshot ID:%s" %(snapshot.id);
Usage
python aws_create_snapshot.py AWS_INSTANCE_NAME
Future post with retention period and may be with configuration 🙂