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 "Options: #{options}"
  @options = options
end

Instance Method Details

#backup_server_idObject

Return the backup server id



131
132
133
# File 'lib/new_backup/myrds.rb', line 131

def backup_server_id
  "#{@options[:rds][:instance_id]}-s3-dump-server-#{@options[:timestamp]}".tap{|t| debug "Backup Server ID: #{t}"}
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
70
# File 'lib/new_backup/myrds.rb', line 64

def get_rds(connection)
  debug "rds instance_id: #{@options[:rds][:instance_id]}"
  debug "Connection 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



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
121
# File 'lib/new_backup/myrds.rb', line 93

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 "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



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

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 "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



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

def snap_name
  "s3-dump-snap-#{@options[:timestamp]}".tap{|t| debug "Snap Name: #{t}"}
end