From d25e2d0d10b72eeac0d4643f376866a03cf31a19 Mon Sep 17 00:00:00 2001 From: Michael Alphonso Date: Tue, 24 May 2016 12:56:57 -0400 Subject: [PATCH 1/3] merged from https://github.com/alexadriaanse/aws-snapshot-tool/tree/snapshot-by-instance to get snapshot by instance type added extra information gathered from instance type to relate volume id and instance names in the outgoing message --- makesnapshots.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/makesnapshots.py b/makesnapshots.py index e689231..f9cfcc9 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -65,7 +65,6 @@ 'period': period, 'date': datetime.today().strftime('%d-%m-%Y %H:%M:%S') } -message += start_message + "\n\n" logging.info(start_message) # Get settings from config.py @@ -142,8 +141,29 @@ def set_resource_tags(resource, tags): resource.add_tag(tag_key, tag_value) # Get all the volumes that match the tag criteria -print 'Finding volumes that match the requested tag ({ "tag:%(tag_name)s": "%(tag_value)s" })' % config -vols = conn.get_all_volumes(filters={ 'tag:' + config['tag_name']: config['tag_value'] }) +instance_names = [] +tag_type = config.get('tag_type', 'volume') +if tag_type == 'volume': + print 'Finding volumes that match the requested tag ({ "tag:%(tag_name)s": "%(tag_value)s" })' % config + vols = conn.get_all_volumes(filters={ 'tag:' + config['tag_name']: config['tag_value'] }) +elif tag_type == 'instance': + print 'Finding volumes attached to running instances that match the requested tag ({ "tag:%(tag_name)s": "%(tag_value)s" })' % config + reservations = conn.get_all_instances(filters={ + 'instance-state-name': 'running', + 'tag:' + config['tag_name']: config['tag_value']}) + volume_ids = [] + for r in reservations: + for i in r.instances: + for bdt in i.block_device_mapping.itervalues(): + volume_ids.append(bdt.volume_id) + if 'Name' in i.tags: + instance_names.append(i.tags['Name'] + " - " + bdt.volume_id) + else: + instance_names.append(i.id + " - " + bdt.volume_id) + vols = conn.get_all_volumes(volume_ids=volume_ids) +else: + print "tag_type should either be 'volume' or 'instance'." + quit() for vol in vols: try: @@ -219,6 +239,10 @@ def date_compare(snap1, snap2): 'count_success': count_success, 'count_total': count_total } +message += start_message + "\n\n" +message = "These are the instances and their volumes:\n" +for instance_name in instance_names: + message += instance_name + "\n" message += result message += "\nTotal snapshots created: " + str(total_creates) From 335d083db54bc3a9e1579cbc2e0655b650b5a9d2 Mon Sep 17 00:00:00 2001 From: Michael Alphonso Date: Wed, 25 May 2016 06:19:26 -0400 Subject: [PATCH 2/3] left out start message --- makesnapshots.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makesnapshots.py b/makesnapshots.py index f9cfcc9..55fed62 100755 --- a/makesnapshots.py +++ b/makesnapshots.py @@ -240,7 +240,7 @@ def date_compare(snap1, snap2): 'count_total': count_total } message += start_message + "\n\n" -message = "These are the instances and their volumes:\n" +message += "These are the instances and their volumes:\n" for instance_name in instance_names: message += instance_name + "\n" From 897247167fd3a04bd4efa5af2d287a8c740a1e69 Mon Sep 17 00:00:00 2001 From: Michael Alphonso Date: Wed, 25 May 2016 06:28:41 -0400 Subject: [PATCH 3/3] config.sample update iam.policy.sample update, needs DescribeInstances --- config.sample | 8 +++++++- iam.policy.sample | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config.sample b/config.sample index 8f70762..bafefca 100644 --- a/config.sample +++ b/config.sample @@ -9,9 +9,15 @@ config = { 'ec2_region_endpoint': 'ec2.eu-west-1.amazonaws.com', # Tag of the EBS volume you want to take the snapshots of - 'tag_name': 'tag:MakeSnapshot', + 'tag_name': 'MakeSnapshot', 'tag_value': 'True', + # tag_type 'volume' will cause individual volumes that match the above tag + # to be snapshotted. + # tag_type 'instance' will cause volumes attached to running instances that + # match the above tag to be snapshotted. + 'tag_type': 'instance', + # Number of snapshots to keep (the older ones are going to be deleted, # since they cost money). 'keep_day': 5, diff --git a/iam.policy.sample b/iam.policy.sample index 378b405..179e15d 100644 --- a/iam.policy.sample +++ b/iam.policy.sample @@ -17,6 +17,7 @@ "ec2:CreateTags", "ec2:DeleteSnapshot", "ec2:DescribeAvailabilityZones", + "ec2:DescribeInstances", "ec2:DescribeSnapshots", "ec2:DescribeTags", "ec2:DescribeVolumeAttribute",