Module: Rds::S3::Backup

Defined in:
lib/rds-s3-backup/mys3.rb,
lib/rds-s3-backup.rb,
lib/rds-s3-backup/myrds.rb,
lib/rds-s3-backup/datadog.rb,
lib/rds-s3-backup/version.rb,
lib/rds-s3-backup/mysqlcmds.rb

Overview

mys3.rb

Copyright

© 2013 by Novu, LLC

*Author(s)*

Tamara Temple <[email protected]>

Since

2013-02-26

License

GPLv3

Version

0.0.1

Description

Defined Under Namespace

Classes: DataDog, DataDogException, MyRDS, MyRDSException, MyS3, MyS3Exception, MySqlCmds, MySqlCmdsException

Constant Summary collapse

VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

.cleanup(rds, s3, unlink_files = []) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/rds-s3-backup.rb', line 114

def cleanup(rds, s3, unlink_files=[])
  rds.destroy() unless rds.nil?
  s3.destroy() unless s3.nil?
  
  unlink_files = [ unlink_files ] unless unlink_files.is_a?(Array)
  unlink_files = unlink_files.flatten
  unlink_files.each {|f| File.unlink(f) if !f.nil? && File.exists?(f) }

end

.process_options(thor_options, thor_defaults) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rds-s3-backup.rb', line 13

def process_options(thor_options,thor_defaults)

  options = thor_defaults

  if thor_options["config_file"] && File.exists?(thor_options["config_file"])
    begin
      options = options.merge(YAML.load(File.read(thor_options["config_file"])))
    rescue Exception => e
      raise "Unable to read and parse #{thor_options["config_file"]}: #{e.class}: #{e}"
    end
  end

  options.merge!(thor_options)

  # Check for required options
  missing_options = %w{rds_instance_id s3_bucket aws_access_key_id aws_secret_access_key mysql_database mysql_username mysql_password}.select {|o| o unless options.has_key?(o)}

  raise "Missing required options #{missing_options.inspect} in either configuration or command line" if missing_options.count > 0
  
  options['timestamp'] = Time.new.strftime('%Y-%m-%d-%H-%M-%S-%Z') #  => "2013-02-25-19-28-55-CST" 

  options
end

.run(thor_options, thor_defaults) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rds-s3-backup.rb', line 37

def run(thor_options,thor_defaults)

  $logger = Logger.new(STDOUT)
  @options = process_options(thor_options,thor_defaults)
  $logger.debug "Running with Options: #{@options.to_yaml}"

  $logger.level = set_logger_level(@options["log_level"])

  $dogger = DataDog.new(@options['data_dog_api_key'])
  
  "Starting RDS-S3-Backup at #{@options['timestamp']}".tap do |t|
    $logger.info t
    $dogger.send t, :alert_type => 'Info', :priority => 'low'
  end


  $logger.debug "#{File.basename(__FILE__)}:#{__LINE__}: Running with options:"
  debug_opts = @options.dup

  $logger.debug @options.merge(
  'aws_access_key_id' => 'X'*10,
  'aws_secret_access_key' => 'Y'*15,
  'mysql_password' => "ZZY"*5,
  'data_dog_api_key' => 'XYZZY'*3                                     
                               ).to_yaml

  begin

    $logger.info "Creating RDS and S3 Connections"
    rds = MyRDS.new(@options)
    $logger.debug "rds: #{rds.to_yaml}"

    $logger.info "Restoring Database"  
    rds.restore_db()

    $logger.info "Dumping and saving original database contents"
    real_data_file = "#{rds.server.id}-mysqldump-#{@options['timestamp']}.sql.gz".
      tap{|t| $logger.debug "real_data_file: #{t}"}
 
    real_dump_file = rds.dump(real_data_file).tap{|t| $logger.debug "rds.dump returns: #{t}"}
              s3 = MyS3.new(@options)
    $logger.debug "s3: #{s3.to_yaml}"
    s3.save_production(real_dump_file)

    if @options['dump_ttl'] > 0
      $logger.info "Pruning old dumps"
      s3.prune_files(:prefix => "#{rds.server.id}-mysqldump-",
                     :keep => @options['dump_ttl'])
    end

    $logger.info "Obfuscating database"
    rds.obfuscate()

    $logger.info "Dumping and saving obfuscated database contents"
    clean_data_file = "clean-mysqldump.sql.gz" 
    s3.save_clean(rds.dump(clean_data_file))
    
  rescue Exception => e
    msg = "ERROR in #{File.basename(__FILE__)}: #{e.class}: #{e}\n"
    msg << e.backtrace.join("\n")
    $logger.error msg
    $dogger.send msg unless e.is_a?(DataDogException) # don't make a loop!
    raise e
  ensure
    cleanup(rds, s3, [ real_data_file, clean_data_file ])
  end
  
  "RDS-S3-Backup Completed Successfully".tap do |t|
    $logger.info t
    $dogger.send t, :alert_type => 'Info', :priority => 'low'
  end

  0                           # exit code 0


end

.set_logger_level(ll) ⇒ Object



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

def set_logger_level(ll)
  case ll.downcase
  when 'debug' ; Logger::DEBUG
  when 'info' ; Logger::INFO
  when 'warn' ; Logger::WARN
  when 'error' ; Logger::ERROR
  when 'fatal' ; Logger::FATAL
  else
    Logger::INFO
  end
end