{"id":18799,"date":"2015-03-31T23:02:43","date_gmt":"2015-03-31T17:32:43","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18799"},"modified":"2015-03-31T23:02:43","modified_gmt":"2015-03-31T17:32:43","slug":"continuing-with-boto-delete-ebs-snapshot-which-is-30-days-older","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/continuing-with-boto-delete-ebs-snapshot-which-is-30-days-older\/","title":{"rendered":"Continuing with Boto : Delete EBS snapshot which is 30 days older"},"content":{"rendered":"<p>In continuation with my previous blog <a href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-boto-python-interface-for-aws\/\">&#8220;Getting Started with Boto ( python Interface for AWS )&#8221;<\/a>,\u00a0 today we are going to discuss one of the use case that we used to do daily.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Deleting EBS Snapshot which is N days older.<\/h3>\n<p><\/br>We&#8217;ll be writing python script using Boto library to delete EBS snapshots which is 30 days older. For this we will be using three different python modules.<\/p>\n<p>1) boto<br \/>\n2) datetime<br \/>\n3) dateutil<\/p>\n<p>We have already discussed boto module in our <a href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-boto-python-interface-for-aws\/\">previous article<\/a>.<br \/>\n<strong><a href=\"https:\/\/docs.python.org\/2\/library\/datetime.html\">datetime<\/a><\/strong><br \/>\ndatetime module contains classes and methods for manipulating dates and times. e.g<br \/>\n[Python]<\/p>\n<p>[js]<br \/>\n  &gt;&gt;&gt; import datetime<br \/>\n  &gt;&gt;&gt;datetime.datetime.now()<br \/>\n   datetime.datetime(2015, 3, 31, 22, 11, 22, 512228)<\/p>\n<p>Output : (Year,Month,Day,Minutes,Hour,Seconds)<br \/>\n[\/js]<\/p>\n<p>Now we have to find the date which is 30 days old with respect to current date. For that we will use timedelta objects comes with datetime class.<\/p>\n<p>[js]<\/p>\n<p>  &gt;&gt;&gt;import datetime<br \/>\n  &gt;&gt;&gt;datetime.datetime.now() &#8211; datetime.timedelta(days=30)<br \/>\n  datetime.datetime(2015, 3, 1, 22, 21, 49, 741196)<\/p>\n<p>[\/js]<\/p>\n<p>As you can see we got the day <strong> 1 March 2015 <\/strong> which is 30 days behind from Current Date.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">dateutil<\/h3>\n<p>Now, we can compare EBS Snapshot Creation time with this time, If ebsSnapshotId.start_time &lt; 30 days we&#8217;ll delete the snapshots. For that we have to use another python module <strong>dateutil.parser <\/strong> which helps us in extracting date from EBS Start time.<\/p>\n<p>EBS start_time uses <font style=\"text-align: justify;color: #ff9900\">2012-10-09T12:27:30.000Z\u00a0<\/font> format to describe Creation Time for snaphots. We have to extract the date from this. We can achieve this by following snippets.<\/p>\n<p>[js]<br \/>\n&gt;&gt;&gt; import dateutil<br \/>\n&gt;&gt;&gt; from dateutil import parser<br \/>\n&gt;&gt;&gt; parser.parse(snapshotID.start_time).date()<br \/>\ndatetime.date(2012, 10, 9)<br \/>\n[\/js]<\/p>\n<p>Now we have the current date , 30 days old date and EBS Snapshot Creation Time. So our complete scripts &#8221; To delete snapshot which is 30 days Old &#8221;<\/p>\n<p>[js]<\/p>\n<p>import boto<br \/>\nimport datetime<br \/>\nimport dateutil<br \/>\nfrom dateutil import parser<br \/>\nfrom boto import ec2<\/p>\n<p>connection=ec2.connect_to_region(&quot;REGION-NAME&quot;)<\/p>\n<p>ebsAllSnapshots=connection.get_all_snapshots(owner=&#8217;16-DIGIT-AWS-ACCOUNT-NUMBER&#8217;)<\/p>\n<p>#Get the 30 days old date<br \/>\ntimeLimit=datetime.datetime.now() &#8211; datetime.timedelta(days=30)  <\/p>\n<p>for snapshot in ebsAllSnapshots:<\/p>\n<p>    if parser.parse(snapshot.start_time).date() &lt;= timeLimit.date():<br \/>\n        print &quot; Deleting Snapshot %s  %s &quot;  %(snapshot.id,snapshot.tags)<br \/>\n        connection.delete_snapshot(snapshot.id)<br \/>\n    else:<br \/>\n        # this section will have all snapshots which is created before 30 days<br \/>\n        print &quot;Only Deleting Snapshots which is 30 days old&quot; <\/p>\n<p>[\/js]<br \/>\n<strong> <\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Note : Please make sure you perform testing before executing this script.<\/h3>\n<p><\/strong><br \/>\nLeave a comment if you have any questions regarding this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In continuation with my previous blog &#8220;Getting Started with Boto ( python Interface for AWS )&#8221;,\u00a0 today we are going to discuss one of the use case that we used to do daily. Deleting EBS Snapshot which is N days older. We&#8217;ll be writing python script using Boto library to delete EBS snapshots which is [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":12},"categories":[1174,1],"tags":[248,1611,1358],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18799"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18799"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18799\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}