Class: NewBackup::MyRds

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging
Defined in:
lib/new_backup/myrds.rb

Constant Summary collapse

MAX_TRIES =

Maximum number of tries to wait for database servers to be ready

3

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MyRds

Initialize the class with options



27
28
29
30
# File 'lib/new_backup/myrds.rb', line 27

def initialize(options={})
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: Options: #{options}"
  @options = options
end

Instance Method Details

#backup_server_idObject

Return the backup server id



132
133
134
135
136
# File 'lib/new_backup/myrds.rb', line 132

def backup_server_id
  "#{@options[:rds][:instance_id]}-s3-dump-server-#{@options[:timestamp]}".tap do |t|
    debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: Backup Server ID: #{t}"
  end
end

#connect {|Fog::AWS::RDS.new(fog_options)| ... } ⇒ Object

Establishes a connection to AWS RDS and yeilds a block on the connection

Yields:

  • (Fog::AWS::RDS.new(fog_options))


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/new_backup/myrds.rb', line 50

def connect(&block)
  raise "no block given in #{self.class}#connect" unless block_given?
  aws = @options[:aws]
  debug "AWS Options: #{aws}"
  fog_options = {
    :aws_access_key_id 	=> aws[:access_key],
    :aws_secret_access_key 	=> aws[:secret_key],
    :region 		=> aws[:rds_region]}

  Fog.timeout = @options[:fog][:timeout]
  yield Fog::AWS::RDS.new(fog_options)
end

#get_rds(connection) {|rds_server| ... } ⇒ Object

Get the RDS server

Yields:

  • (rds_server)


64
65
66
67
68
69
# File 'lib/new_backup/myrds.rb', line 64

def get_rds(connection)
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: rds instance_id: #{@options[:rds][:instance_id]}\nConnection servers: #{connection.servers}"
  rds_server = connection.servers.get(@options[:rds][:instance_id])
  raise "No RDS server!" if rds_server.nil?
  yield rds_server
end

#restore(&block) ⇒ Object

Restore a snapshot of the target database yielding the database to the block



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/new_backup/myrds.rb', line 37

def restore(&block)
  connect do |connection|
    get_rds(connection) do |rds_server|
      retrieve_snapshot(rds_server) do |snapshot|
        restore_db(connection,snapshot) do |db|
          yield db
        end
      end
    end
  end
end

#restore_db(connection, snapshot, &block) ⇒ Object

Restore the snapshot to a database



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/new_backup/myrds.rb', line 92

def restore_db(connection, snapshot, &block)
  begin
    connection.
      restore_db_instance_from_db_snapshot(snapshot.id,
                                           backup_server_id,
                                           {"DBSubnetGroupName" => @options[:rds][:subnet_group],
                                             "DBInstanceClass" => @options[:rds][:instance_type]}
                                           )
    backup_server = connection.servers.get(backup_server_id)
    1.upto(MAX_TRIES) do |i|
      debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: waiting for backup server, try ##{i}"
      backup_server.wait_for { ready? }
    end

    
    yield MySqlCmds.new(backup_server.endpoint['Address'],
                        @options[:mysql][:username],
                        @options[:mysql][:password],
                        @options[:mysql][:database],
                        @options[:mysql][:obfuscate_script])
                          

  ensure
    unless @options[:debug]
      backup_server.destroy unless backup_server.nil?
    end
  end
  
end

#retrieve_snapshot(rds_server, &block) ⇒ Object

Retrieve a snapshot



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/new_backup/myrds.rb', line 73

def retrieve_snapshot(rds_server, &block)
  begin
    rds_server.snapshots.new(:id => snap_name).save
    new_snapshot = rds_server.snapshots.get(snap_name)
    1.upto(MAX_TRIES) do |i|
      debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: waiting for new snapshot, try ##{i}"
      new_snapshot.wait_for { ready? }
    end
    
    yield new_snapshot
  ensure
    unless @options[:debug]
      new_snapshot.destroy unless new_snapshot.nil?
    end
  end
  
end

#snap_nameObject

Return the snapshot name



125
126
127
128
129
# File 'lib/new_backup/myrds.rb', line 125

def snap_name
  "s3-dump-snap-#{@options[:timestamp]}".tap do |t|
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: Snap Name: #{t}"
  end
end