Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'aws-sdk'
- require 'date'
- now = Date.today
- days_ago_7 = now - 7
- days_ago_30 = now - 30
- years_ago_3 = now - ( 365 * 3 )
- rds = Aws::RDS::Client.new(region: 'us-west-1')
- config_array = { db_instance_identifier: "bn-mysql-read-1",}
- snapshots = rds.describe_db_snapshots(config_array).db_snapshots
- #protect all snapshots less than 7 days old
- snapshots = snapshots.reject {|snapshot| snapshot['snapshot_create_time'].to_date > days_ago_7 }
- #protect 1 snapshot a day that is older than 1 week but less than a month old
- datehash = Hash.new
- snapshots = snapshots.reject do |snapshot|
- if snapshot['snapshot_create_time'].to_date < days_ago_7
- if snapshot['snapshot_create_time'].to_date > days_ago_30 and datehash.has_key? snapshot['snapshot_create_time'].to_date.strftime("%x")
- false
- else
- datehash[snapshot['snapshot_create_time'].to_date.strftime("%x")] = 0
- true
- end
- else
- false
- end
- end
- #protect 1 snapshot a month that is older than 1 month but less than 3 years old
- datehash = Hash.new
- snapshots = snapshots.reject do |snapshot|
- if snapshot['snapshot_create_time'].to_date < days_ago_30
- if snapshot['snapshot_create_time'].to_date > years_ago_3 and
- datehash.has_key? snapshot['snapshot_create_time'].to_date.strftime("%y/%m")
- false
- else
- datehash[snapshot['snapshot_create_time'].to_date.strftime("%y/%m")] = 0
- true
- end
- else
- false
- end
- end
- p snapshots = snapshots.collect { |item| item['db_snapshot_identifier']}
- snapshots.each do |snapshot|
- rds.delete_db_snapshot(
- {
- db_snapshot_identifier: snapshot, # required
- })
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement