Subscribe to Posts by Email

Subscriber Count

    701

Disclaimer

All information is offered in good faith and in the hope that it may be of use for educational purpose and for Database community purpose, but is not guaranteed to be correct, up to date or suitable for any particular purpose. db.geeksinsight.com accepts no liability in respect of this information or its use. This site is independent of and does not represent Oracle Corporation in any way. Oracle does not officially sponsor, approve, or endorse this site or its content and if notify any such I am happy to remove. Product and company names mentioned in this website may be the trademarks of their respective owners and published here for informational purpose only. This is my personal blog. The views expressed on these pages are mine and learnt from other blogs and bloggers and to enhance and support the DBA community and this web blog does not represent the thoughts, intentions, plans or strategies of my current employer nor the Oracle and its affiliates or any other companies. And this website does not offer or take profit for providing these content and this is purely non-profit and for educational purpose only. If you see any issues with Content and copy write issues, I am happy to remove if you notify me. Contact Geek DBA Team, via geeksinsights@gmail.com

Pages

AWS EBS Snapshot Cleanup Script

Hello,

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([(s.id, 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]
        connection.delete_snapshot(s[0])
    time.sleep(1)

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

root@wash-i-16ca26c8-prod ~ $ python deletesnapshot.py
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

Comments are closed.