Method: NewBackup::Main#run

Defined in:
lib/new_backup/main.rb

#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
174
175
176
177
178
179
# File 'lib/new_backup/main.rb', line 111

def run
  dogger "Beginning RDS-S3-Backup"
  begin
    
    raise "#{self.class}##{__method__}:#{__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__}:#{__LINE__}: raw_file: #{raw_file}"
    clean_file = File.join(File.expand_path(@options[:dump_directory]),clean_file_name)
    debug "#{self.class}##{__method__}:#{__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|
        info "Dump raw database"
        db.dump(raw_file)
        info "Obfuscate database"
        db.obfuscate
        info "Dump clean database"
        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|
          info "Save raw db dump"
          s3.put_file bucket, raw_file
          info "Prune excess backups"
          s3.prune bucket, @options[:s3][:dump_ttl]
        end
        
        s3.connect_bucket(connection, @options[:s3][:clean_bucket]) do |bucket|
          info "Save cleaned db dump"
          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 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