class PreserveRdsSnapshot::CLI
Constants
- PRESERVE_TAG_NAME
Public Instance Methods
copy()
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 109 def copy if options[:target_db_snapshot_identifier] target = options[:target_db_snapshot_identifier] else target = preserve_snapshot_name(options[:source_db_snapshot_identifier]) end source = options[:source_db_snapshot_identifier] begin resp = rds.client.copy_db_snapshot( source_db_snapshot_identifier: source, target_db_snapshot_identifier: target, tags: [key: PRESERVE_TAG_NAME, value: 'true'] ) s = resp.db_snapshot puts "#{s.db_snapshot_identifier}\t#{s.snapshot_create_time}" rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end end
init()
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 47 def init begin rds.db_instances.each do |instance| if options[:dry_run] puts "init(dry run)\t#{instance.db_instance_identifier}\t#{options[:generations]}" else if enable_preserve(instance.db_instance_identifier, options[:generations]) puts "init\t#{instance.db_instance_identifier}\t#{options[:generations]}" end end end fix_tags rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end end
latest()
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 132 def latest instances = db_instances(options[:instance]) instances.each do |instance| s = latest_auto_snapshot(instance.db_instance_identifier) puts "#{s.db_snapshot_identifier}\t#{s.snapshot_create_time}" if s end end
list()
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 26 def list begin resp = rds.client.describe_db_snapshots( snapshot_type: options[:snapshot_type], db_instance_identifier: options[:instance] ) resp.db_snapshots.each do |s| puts "#{s.db_snapshot_identifier}\t#{s.snapshot_create_time}" end rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end end
preserve()
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 66 def preserve begin instances = db_instances(options[:instance]) instances.each do |i| instance = i.db_instance_identifier latest = latest_auto_snapshot(instance) if latest if options[:dry_run] puts "#{latest.db_snapshot_identifier}\t-\t-" else s = copy_snapshot(latest.db_snapshot_identifier) puts "copy\t#{latest.db_snapshot_identifier}\t#{s.db_snapshot_identifier}\t#{s.snapshot_create_time}" if s end end tag = preserve_tag(instance, 'db') expireds = expired_snapshots(instance, tag[:value].to_i) dry_run_msg = '(dry run)' if options[:dry_run] expireds.each do |expired| unless options[:dry_run] rds.client.delete_db_snapshot( db_snapshot_identifier: expired.db_snapshot_identifier ) end puts "delete#{dry_run_msg}\t#{expired.db_snapshot_identifier}" end end rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end end
Private Instance Methods
aws_account_number()
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 178 def aws_account_number if options[:aws_account_number] return options[:aws_account_number] else begin return ec2.security_groups(group_names: ['default']).first.owner_id rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end end end
copy_snapshot(db_snapshot_identifier)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 254 def copy_snapshot(db_snapshot_identifier) begin resp = rds.client.copy_db_snapshot( source_db_snapshot_identifier: db_snapshot_identifier, target_db_snapshot_identifier: preserve_snapshot_name(db_snapshot_identifier), tags: [key: PRESERVE_TAG_NAME, value: 'true'] ) return resp.db_snapshot rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end end
db_instances(db_instance_identifier = nil)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 157 def db_instances(db_instance_identifier = nil) list = [] begin if db_instance_identifier list << rds.db_instance(db_instance_identifier) else rds.db_instances.each do |i| list << i if preserve_tag(i.db_instance_identifier, 'db') end end rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end list end
enable_preserve(resource_id, generations)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 209 def enable_preserve(resource_id, generations) return false unless generations.kind_of? Integer begin tag = preserve_tag(resource_id, 'db') unless tag resp = rds.client.add_tags_to_resource( resource_name: rds_arn(resource_id, 'db'), tags: [{key: PRESERVE_TAG_NAME, value: generations.to_s}] ) return resp.successful? end rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end end
expired_snapshots(db_instance_identifier, generations)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 268 def expired_snapshots(db_instance_identifier, generations) expired_snapshots = [] begin resp = rds.client.describe_db_snapshots( snapshot_type: 'manual', db_instance_identifier: db_instance_identifier ) snapshots = resp.db_snapshots.select {|s| preserve_tag(s.db_snapshot_identifier, 'snapshot') }.sort_by(&:snapshot_create_time).reverse expired_snapshots = snapshots[generations..-1] if snapshots.size > generations rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end expired_snapshots end
latest_auto_snapshot(db_instance_identifier = nil)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 142 def latest_auto_snapshot(db_instance_identifier = nil) latest = nil begin resp = rds.client.describe_db_snapshots( snapshot_type: 'automated', db_instance_identifier: db_instance_identifier ) latest = resp.db_snapshots.sort_by(&:snapshot_create_time).last rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end latest end
preserve_snapshot_name(db_snapshot_identifier)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 174 def preserve_snapshot_name(db_snapshot_identifier) 'preserve-' + db_snapshot_identifier.gsub(/^rds:/, '') end
preserve_tag(resource_id, type)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 195 def preserve_tag(resource_id, type) tag = nil begin resp = rds.client.list_tags_for_resource( resource_name: rds_arn(resource_id, type) ) tag = resp.tag_list.find {|t| t[:key] == PRESERVE_TAG_NAME} rescue ::Aws::Errors::ServiceError => e $stderr.puts e exit 1 end tag end
rds_arn(resource_id, type)
click to toggle source
# File lib/preserve-rds-snapshot/cli.rb, line 191 def rds_arn(resource_id, type) "arn:aws:rds:#{options[:region]}:#{aws_account_number}:#{type}:#{resource_id}" end