-
-
Save kjoconnor/7344485 to your computer and use it in GitHub Desktop.
import sys | |
from boto.ec2 import connect_to_region | |
from datetime import datetime, timedelta | |
try: | |
days = int(sys.argv[1]) | |
except IndexError: | |
days = 7 | |
delete_time = datetime.utcnow() - timedelta(days=days) | |
filters = { | |
'tag-key': 'mybackups' | |
} | |
print 'Deleting any snapshots older than {days} days'.format(days=days) | |
ec2 = connect_to_region('us-west-1') | |
snapshots = ec2.get_all_snapshots(filters=filters) | |
deletion_counter = 0 | |
size_counter = 0 | |
for snapshot in snapshots: | |
start_time = datetime.strptime( | |
snapshot.start_time, | |
'%Y-%m-%dT%H:%M:%S.000Z' | |
) | |
if start_time < delete_time: | |
print 'Deleting {id}'.format(id=snapshot.id) | |
deletion_counter = deletion_counter + 1 | |
size_counter = size_counter + snapshot.volume_size | |
# Just to make sure you're reading! | |
snapshot.delete(dry_run=True) | |
print 'Deleted {number} snapshots totalling {size} GB'.format( | |
number=deletion_counter, | |
size=size_counter | |
) |
Hello...
This script only works for snapshots of volumes or also works for snapshots of databases (rds)?
Regards
Great script thanks! Made some changes:
#!/usr/bin/python
import sys
import boto
from datetime import datetime, timedelta
try:
days = int(sys.argv[1])
except IndexError:
days = 30
delete_time = datetime.utcnow() - timedelta(days=days)
print 'Deleting any snapshots older than {days} days'.format(days=days)
ec2 = boto.connect_ec2()
snapshots = ec2.get_all_snapshots(owner='self')
deletion_counter = 0
size_counter = 0
for snapshot in snapshots:
start_time = datetime.strptime(
snapshot.start_time,
'%Y-%m-%dT%H:%M:%S.000Z'
)
if start_time < delete_time:
print 'Deleting {id}'.format(id=snapshot.id)
deletion_counter = deletion_counter + 1
size_counter = size_counter + snapshot.volume_size
# Just to make sure you're reading!
snapshot.delete(dry_run=False)
print 'Deleted {number} snapshots totalling {size} GB'.format(
number=deletion_counter,
size=size_counter
)
I'm a python newbie so excuse my ignorance, but why do you have to use (days=days) instead of just (days)?
@cwhitmore88 it's just a good practice :)
You know that when you're using .format
str method, you have to use {}
to mark where the strings will appears. You can make something like:
print 'Deleting any snapshots older than {} days'.format(days)
But you also can do in this way:
print 'Deleting any snapshots older than {days} days'.format(days=days)
That way allow me do something like that:
print 'Deleting any snapshots of the {month} older than {days} days'.format(days=days, month=month)
or even control the order, like this:
print 'Deleting any snapshots older than {days} days of the {month}'.format(month=month, days=days)
Sorry for my bad English, but I hope this can help you 😄
Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 17)
Getting error while executing in Lambda.
print 'Deleting any snapshots older than {days} days'.format(days=days)
thanks. How can I use this script delete old snapshots except last two?
This is helpful. How can we do this for RDS manual snapshots with for each loop for all regions.
This was really helpful, friend. Thank you (^: