-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlambda_ebs_backup.py
61 lines (51 loc) · 2.64 KB
/
lambda_ebs_backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# lambda_abs_backup.py: Implements the lambda function for the ebs backup feature
# Copyright (c) 2016 Gabor Maylander (gabormay@github)
import datetime
import boto3
# Main entry point for the lambda function
def backup_handler(event, context):
ec2 = boto3.resource('ec2')
print "Starting scheduled EBS volume backup", datetime.datetime.now()
i_backed_up = 0
i_deleted = 0;
# Process only volumes with the 'Backup' tag set (value is indifferent)
# For each volume:
# Create a fresh snapshot
# Cleanup earlier snapshots:
# Keep at least n_snapshots_to_keep backups
# Delete only ones older than min_snapshot_age_to_delete
n_snapshots_to_keep = 10
min_snapshot_age_to_delete = 7 # (in days)
volumes_to_backup = ec2.volumes.filter(Filters=[{'Name': 'tag-key', 'Values': ['Backup']}])
for vol in volumes_to_backup:
# convenience dict of the volume's tags
vol_tags = {t['Key']: t['Value'] for t in vol.tags}
# Use the name of the volume as the base name for the snapshot if available
# otherwise use the volume ID
vol_name = vol_tags['Name'] if 'Name' in vol_tags else vol.id
dt = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
# Submit the snapshot creation request
print 'Backing up', vol.id, vol_name
i_backed_up += 1
snapshot = vol.create_snapshot(Description='Created by ebs-backup')
# Add tags to the snapshot for later processing:
# Name: for easier identification
# Backup: to signal that this was automatically created as a backup
# Note that cleanup will only delete snapshot tagged as Backup
sn_name = '{}-{}'.format(vol_name, dt)
snapshot.create_tags(Tags=
[
{'Key': 'Name', 'Value': sn_name},
{'Key': 'Backup', 'Value': 'true'}
])
print 'Created snapshot', snapshot.id, sn_name
# Now cleanup older backups
backups = list(ec2.snapshots.filter(Filters=[{'Name': 'volume-id', 'Values': [vol.id]}]))
for snap in sorted(backups, key=lambda s: s.start_time)[0:len(backups)-n_snapshots_to_keep]:
if (datetime.date.today() - snap.start_time.date()).days >= min_snapshot_age_to_delete:
print 'Deleting old snapshot', snap.id, snap.start_time, (datetime.date.today() - snap.start_time.date()).days, "days old"
i_deleted += 1
snap.delete()
msg = "Done, created {} new backups and deleted {} old ones".format(i_backed_up, i_deleted)
print msg
return {'message': msg}