Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snapshot by instance and instance-volume information #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion config.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions iam.policy.sample
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ec2:CreateTags",
"ec2:DeleteSnapshot",
"ec2:DescribeAvailabilityZones",
"ec2:DescribeInstances",
"ec2:DescribeSnapshots",
"ec2:DescribeTags",
"ec2:DescribeVolumeAttribute",
Expand Down
30 changes: 27 additions & 3 deletions makesnapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down