Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.63 KB | None | 0 0
  1. require 'aws-sdk'
  2. require 'date'
  3. now = Date.today
  4. days_ago_7 = now - 7
  5. days_ago_30  = now - 30
  6. years_ago_3 = now - ( 365 * 3 )
  7.  
  8. rds = Aws::RDS::Client.new(region: 'us-west-1')
  9. config_array = { db_instance_identifier: "bn-mysql-read-1",}
  10. snapshots = rds.describe_db_snapshots(config_array).db_snapshots
  11.  
  12. #protect all snapshots less than 7 days old
  13. snapshots = snapshots.reject {|snapshot| snapshot['snapshot_create_time'].to_date > days_ago_7 }
  14.  
  15. #protect 1 snapshot a day that is older than 1 week but less than a month old
  16. datehash = Hash.new
  17. snapshots = snapshots.reject do |snapshot|
  18.   if snapshot['snapshot_create_time'].to_date < days_ago_7
  19.     if snapshot['snapshot_create_time'].to_date > days_ago_30 and datehash.has_key? snapshot['snapshot_create_time'].to_date.strftime("%x")
  20.       false
  21.     else
  22.       datehash[snapshot['snapshot_create_time'].to_date.strftime("%x")] = 0
  23.       true
  24.     end
  25.   else
  26.     false
  27.   end
  28. end
  29. #protect 1 snapshot a month that is older than 1 month but less than 3 years old
  30. datehash = Hash.new
  31. snapshots = snapshots.reject do |snapshot|
  32.   if snapshot['snapshot_create_time'].to_date < days_ago_30
  33.     if snapshot['snapshot_create_time'].to_date > years_ago_3 and
  34.       datehash.has_key? snapshot['snapshot_create_time'].to_date.strftime("%y/%m")
  35.       false
  36.     else
  37.       datehash[snapshot['snapshot_create_time'].to_date.strftime("%y/%m")] = 0
  38.       true
  39.     end
  40.   else
  41.     false
  42.   end
  43. end
  44. p snapshots = snapshots.collect { |item| item['db_snapshot_identifier']}
  45.  
  46. snapshots.each do |snapshot|
  47.   rds.delete_db_snapshot(
  48.     {
  49.       db_snapshot_identifier: snapshot, # required
  50.   })
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement