Class: NewBackup::Main

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

Constant Summary collapse

REQUIRED_OPTIONS =
%w{rds_instance_id s3_bucket aws_access_key_id aws_secret_access_key mysql_database mysql_username mysql_password timestamp}
DEFAULT_OPTIONS =
{
  'fog_timeout' => 1800,
  'dump_directory' => '/tmp',
  'dump_ttl' => 0,
  'aws_region' => 'us-east-1',
  'db_instance_type' => 'db.m1.small',
  'timestamp_format' => '%Y-%m-%d-%H-%M-%S-%Z'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataDog

api_key, api_key=, dogger

Constructor Details

#initialize(opts = {}) ⇒ Main

Initialize the Main loop, processing options and putting them in the right form



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
# File 'lib/new_backup/main.rb', line 43

def initialize(opts={})

  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: original opts: #{opts.to_yaml}"

  if opts["config_file"] && File.exists?(opts["config_file"])
    converged_opts = DEFAULT_OPTIONS.merge(YAML.load(File.read(opts["config_file"]))).merge(opts)
  else
    converged_opts = DEFAULT_OPTIONS.merge(opts)
  end

  converged_opts["timestamp"] = Time.now.strftime(converged_opts["timestamp_format"])
  
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: converged_opts: #{converged_opts.to_yaml}"

  missing_options = REQUIRED_OPTIONS.select {|o| o unless converged_opts.has_key?(o)}
  raise "Missing required options #{missing_options.inspect} in either configuration or command line" if missing_options.count > 0
  
  @options = {
    :fog => {
      :timeout      => converged_opts["fog_timeout"]
    },
    :aws => {
      :access_key 	=> '[HIDDEN]',
      :secret_key 	=> '[HIDDEN]',
      :region 	=> converged_opts["aws_region"]
    },
    :rds => {
      :instance_id	=> converged_opts["rds_instance_id"],
      :subnet_group => converged_opts["db_subnet_group_name"],
      :instance_type => converged_opts["db_instance_type"]
    },
    :s3 => {
      :raw_bucket	=> converged_opts["s3_bucket"],
      :clean_bucket => converged_opts["backup_bucket"],
      :prefix	=> converged_opts["s3_prefix"],
      :region	=> converged_opts["aws_s3_region"] ||= converged_opts["aws_region"],
      :dump_ttl	=> converged_opts["dump_ttl"]
    },
    :mysql => {
      :username	=> converged_opts["mysql_username"],
      :password	=> '[HIDDEN]',
      :database	=> converged_opts["mysql_database"],
      :obfuscate_script => converged_opts["obfuscate_script"]
    },
    :datadog => {
      :api_key	=> '[HIDDEN]'
    },
    :dump_directory => converged_opts["dump_directory"],
    :timestamp	=> converged_opts["timestamp"],
    :debug          => converged_opts["debug"],
    :nords          => converged_opts["nords"],
    :nos3           => converged_opts["nos3"]
  }

  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: Options:\n#{@options.to_yaml}"

  # Fill in the hidden values after showing the options
  @options[:aws][:access_key]   = converged_opts["aws_access_key_id"]
  @options[:aws][:secret_key]   = converged_opts["aws_secret_access_key"]
  @options[:mysql][:password]   = converged_opts["mysql_password"]
  @options[:datadog][:api_key]  = converged_opts["datadog_apikey"]

  $dogger = DataDogger.new
  $dogger.api_key=@options[:datadog][:api_key] unless @options[:debug]
  debug "#{self.class}##{__method__}@#{File.basename(__FILE__)}:#{__LINE__}: $dogger.api_key: #{$dogger.api_key}"

end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



40
41
42
# File 'lib/new_backup/main.rb', line 40

def options
  @options
end

Instance Method Details

#runObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/new_backup/main.rb', line 111

def run
  dogger "Beginning RDS-S3-Backup"
  begin
    
    raise "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: Dump directory #{@options[:dump_directory]} does not exist!" unless File.directory?(@options[:dump_directory])


    raw_file = File.join(File.expand_path(@options[:dump_directory]),save_file_name)
    debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: raw_file: #{raw_file}"
    clean_file = File.join(File.expand_path(@options[:dump_directory]),clean_file_name)
    debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: clean_file: #{clean_file}"

    if (@options[:nords])
      info "Not running RDS"
      File.open(raw_file,'w') do |f|
        f.puts "default content when not running RDS"
      end
      File.open(clean_file,'w') do |f|
        f.puts "default content when not running RDS"
      end
    else

      NewBackup::MyRds.new(@options).restore do |db|
        db.dump(raw_file)
        db.obfuscate
        db.dump(clean_file)
      end
    end

    if (@options[:nos3])
      info "Not running S3"
    else
      
      s3 = NewBackup::MyS3.new(@options)
      s3.connect do |connection|
        s3.connect_bucket(connection, @options[:s3][:raw_bucket]) do |bucket|
          s3.put_file bucket, raw_file
          s3.prune bucket, @options[:s3][:dump_ttl]
        end
        
        s3.connect_bucket(connection, @options[:s3][:clean_bucket]) do |bucket|
          s3.put_file bucket, clean_file
        end
      end
      
    end

  rescue Exception => e
    dogger "Fatal error in #{self.class}#run: #{e.class}: #{e}" ,
    :type => :error,
    :body => "Backtrace:\n#{e.backtrace.join("\n")}"
    debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: Backtrace:\n #{e.backtrace.join("\n")}"
    raise e


  ensure
    File.unlink(raw_file) if File.exists?(raw_file) && ! @options[:debug]
    File.unlink(clean_file) if File.exists?(clean_file) && ! @options[:debug]
  end

  dogger "End RDS-S3-Backup"

end