Class: Rds::S3::Backup::MyRDS

Inherits:
Object
  • Object
show all
Defined in:
lib/rds-s3-backup/myrds.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ MyRDS

Returns a new instance of MyRDS.



23
24
25
# File 'lib/rds-s3-backup/myrds.rb', line 23

def initialize(opts)
  @opts = opts
end

Instance Method Details

#backup_file_path(backup_file_name) ⇒ Object



76
77
78
# File 'lib/rds-s3-backup/myrds.rb', line 76

def backup_file_path(backup_file_name)
  File.join(@opts['dump_directory'], backup_file_name)    
end

#backup_serverObject



96
97
98
99
100
101
102
# File 'lib/rds-s3-backup/myrds.rb', line 96

def backup_server
  @backup_server ||= get_rds_server(backup_server_id)

  (0..1).each {|i|  $logger.debug "#{__FILE__}:#{__LINE__}: Waiting for backup_server to be ready: #{i}" ;  @backup_server.wait_for {ready? } } # won't get fooled again!
  @backup_server

end

#backup_server_idObject



80
81
82
# File 'lib/rds-s3-backup/myrds.rb', line 80

def backup_server_id
  @backup_server_id ||= "#{self.server.id}-s3-dump-server-#{@opts['timestamp']}"
end

#destroyObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rds-s3-backup/myrds.rb', line 138

def destroy
  
  unless @new_snap.nil?
    (0..1).each {|i| $logger.debug "#{__FILE__}:#{__LINE__}: Waiting for new_snap server to be ready in #{self.class}#destroy: #{i}"; @new_snap.wait_for { ready? } }
    @new_snap.destroy
  end
  
  unless @backup_server.nil?
    (0..1).each {|i|  $logger.debug "#{__FILE__}:#{__LINE__}: Waiting for backup server to be ready in #{self.class}#destroy: #{i}"; @backup_server.wait_for {ready? } }
    @backup_server.destroy(nil)
  end

end

#dump(backup_file_name) ⇒ Object

Dump the database to the backup file name



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rds-s3-backup/myrds.rb', line 54

def dump(backup_file_name)
  @mysqlcmds ||= ::Rds::S3::Backup::MySqlCmds.new(backup_server.endpoint['Address'],
                                                  @opts['mysql_username'],
                                                  @opts['mysql_password'],
                                                  @opts['mysql_database'])




  @mysqlcmds.dump(backup_file_path(backup_file_name)) # returns the dump file path
end

#get_rds_connectionObject

Raises:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rds-s3-backup/myrds.rb', line 108

def get_rds_connection()
  options = {
    :aws_access_key_id => @opts['aws_access_key_id'],
    :aws_secret_access_key => @opts['aws_secret_access_key'],
    :region => @opts['aws_region']}
  Fog.timeout=@opts['fog_timeout']
  begin
    connection = Fog::AWS::RDS.new(options)
  rescue Exception => e
    raise MyRDSException.new("Error in #{self.class}#get_rds_connection: #{e.class}: #{e}")
  end
  
  raise MyRDSException.new("Unable to make RDS connection") if connection.nil?

  connection

end

#get_rds_server(id) ⇒ Object

Raises:



126
127
128
129
130
131
132
133
134
135
# File 'lib/rds-s3-backup/myrds.rb', line 126

def get_rds_server(id)

   begin
    server = self.rds.servers.get(id)
  rescue Exception => e
    raise MyRDSException.new("Error getting server in #{self.class}#get_rds_server: #{e.class}: #{e}")
  end
  raise MyRDSException.new("Server is nil for #{id}") if server.nil?
  server
end

#new_snapObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rds-s3-backup/myrds.rb', line 84

def new_snap
  unless @new_snap
    self.server.snapshots.new(:id => snap_name).save
    @new_snap = self.server.snapshots.get(snap_name)

    (0..1).each {|i|  $logger.debug "#{__FILE__}:#{__LINE__}: Waiting for new_snap server to be ready: #{i}"; @new_snap.wait_for { ready? } } # ready? sometimes lies

  end

  @new_snap
end

#obfuscateObject

Convert personal data in the production data into random generic data



67
68
69
70
71
72
73
74
# File 'lib/rds-s3-backup/myrds.rb', line 67

def obfuscate
  @mysqlcmds ||= ::Rds::S3::Backup::MySqlCmds.new(backup_server.endpoint['Address'],
                                                  @opts['mysql_username'],
                                                  @opts['mysql_password'],
                                                  @opts['mysql_database'])

  @mysqlcmds.exec(@opts['obfuscate_sql'])
end

#rdsObject



27
28
29
30
# File 'lib/rds-s3-backup/myrds.rb', line 27

def rds
  # Memoize @rds
  @rds ||= get_rds_connection()
end

#restore_dbObject

Restore the production database from the most recent snapshot



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rds-s3-backup/myrds.rb', line 39

def restore_db

  begin
    self.rds.restore_db_instance_from_db_snapshot(new_snap.id,
                                              backup_server_id,
                                              {"DBSubnetGroupName" => @opts['db_subnet_group_name'],
                                                "DBInstanceClass" => @opts['db_instance_type'] } )
  rescue Exception => e
    raise MyRDSException.new("Error in #{self.class}:restore_db: #{e.class}: #{e}")
  end

end

#serverObject



32
33
34
35
# File 'lib/rds-s3-backup/myrds.rb', line 32

def server
  # Memoize @server
  @server ||= get_rds_server(@opts['rds_instance_id'])
end

#snap_nameObject



104
105
106
# File 'lib/rds-s3-backup/myrds.rb', line 104

def snap_name
  @snap_name ||= "s3-dump-snap-#{@opts['timestamp']}"
end