Subscriber Count


Subscribe to Posts by Email


AWS EBS Snapshot Cleanup Script


Today it's another script, we have got piles of EBS snapshots (around 50,000) over period of 2 years.To give a bit background the EBS volume snapshots will be taken every 4 hours along with our rds snapshots to maintain the recover-ability for some of the lob objects. So each volume having 6 per day and we have around 7 volumes of same kind.

So it's time to cleanup them. Rather deleting every snapshot or by retention of particular days snapshots, we want to retain one per day (which was the last one of the day) and delete the rest of snapshots for each EBS Volume.

And here is the script: (Note we have Description of Volume filtered, you can use Tag or Name of Snapshot Instead, Use wildcharacter * if you want so)

#!/usr/bin/env python

import boto.ec2, os
import datetime
from itertools import groupby
from operator import itemgetter
import time

MAX_SNAPSHOTS = 1   # Number of snapshots to keep

# Connect to EC2 in this region
connection = boto.ec2.connect_to_region('ap-southeast-2')

# Get a list of all volumes which having description filtered
snapshots = connection.get_all_snapshots(filters={'description':'*Production WINFS /dev/xvdk shared storage snapshot*'})
# Get a list of all volumes named *winfs-/dev/xvdk*
#snapshots = connection.get_all_snapshots(filters={'Name':'*winfs-/dev/xvdk*'})
snap_sort = sorted([(, datetime.datetime.strptime(s.start_time, '%Y-%m-%dT%H:%M:%S.%fZ').date()) for s in snapshots], key=lambda k: k[1])
snaps = len(snap_sort)
for i in range(0,snaps):
    s = snap_sort[i]
    print "Checking snapshot", s[0], s[1]
    if i < snaps-1 and s[1] == snap_sort[i+1][1]:
        print "Delete snapshot", s[0], s[1]

Run as below and the output shown as, And snaps will be deleted.

root@wash-i-16ca26c8-prod ~ $ python
Checking snapshot snap-188a4188 2016-10-27
Delete snapshot snap-188a4188 2016-10-27
Checking snapshot snap-51c6a8c1 2016-10-27
Checking snapshot snap-d4c8d7d8 2016-10-28
Delete snapshot snap-d4c8d7d8 2016-10-28
Checking snapshot snap-a331d1ac 2016-10-28
Delete snapshot snap-a331d1ac 2016-10-28
Checking snapshot snap-460af849 2016-10-28
Delete snapshot snap-460af849 2016-10-28
Checking snapshot snap-35dac2c6 2016-10-28
Delete snapshot snap-35dac2c6 2016-10-28
Checking snapshot snap-7f8e958c 2016-10-28
Delete snapshot snap-7f8e958c 2016-10-28
Checking snapshot snap-f88e7a0a 2016-10-28
Delete snapshot snap-f88e7a0a 2016-10-28
Checking snapshot snap-2c5213de 2016-10-28
Delete snapshot snap-2c5213de 2016-10-28
Checking snapshot snap-a802215a 2016-10-28
Delete snapshot snap-a802215a 2016-10-28
Checking snapshot snap-1eb2d780 2016-10-28

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>