class BackupFoundation::Job

Public Class Methods

new(params) click to toggle source
# File lib/backup_foundation/job.rb, line 6
def initialize(params)
  @params = params
end

Public Instance Methods

backup!() click to toggle source
# File lib/backup_foundation/job.rb, line 10
def backup!
  for_each_db do |db, tmpdir|
    (dumper = BackupFoundation::Item::DATABASES[db[:type].to_sym].new(db, tmpdir, @params[:encryption_key])).save_dump tmpdir
    if File.exist? dumper.outfile_path
      send_db_dump dumper.outfile_path, db
      FileUtils.remove_entry_secure dumper.outfile_path
    end
  end
end
download_db_dump(tmpdir, db, limit = 10) click to toggle source
# File lib/backup_foundation/job.rb, line 68
def download_db_dump(tmpdir, db, limit = 10)
  file_path = nil
  url = URI.parse case db
    when Hash
      "#{BackupFoundation::HOST}/api/file?key=#{@params[:key]}&name=#{db[:name] || db[:type]}"
    when String
      db
  end
  http_download_request(url).request_get(url.request_uri) do |response|
    case response
      when Net::HTTPSuccess
        file_path = "#{tmpdir}/#{(response['Content-Disposition'] || "\"#{url.path.split('/').last}\"")[/"(.*)"/, 1]}"
        File.open file_path, 'wb' do |file|
          response.read_body do |str|
            file.write str
          end
        end
      when Net::HTTPRedirection
        file_path = download_db_dump(tmpdir, response['Location'], limit - 1)
      else
        response.error!
    end
  end
  file_path
end
for_each_db() { |db, tmpdir| ... } click to toggle source
# File lib/backup_foundation/job.rb, line 30
def for_each_db(&block)
  tmpdir = Dir.mktmpdir
  @params[:dbs].each do |db|
    yield db, tmpdir
  end
  FileUtils.remove_entry_secure(tmpdir) if tmpdir and File.exist?(tmpdir)
end
http_download_request(url) click to toggle source
# File lib/backup_foundation/job.rb, line 94
def http_download_request(url)
  Net::HTTP.new(url.host, url.port).tap do |http|
    if url.is_a? URI::HTTPS
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
end
restore!() click to toggle source
# File lib/backup_foundation/job.rb, line 20
def restore!
  for_each_db do |db, tmpdir|
    file_path = download_db_dump tmpdir, db
    if File.exist? file_path
      BackupFoundation::Item::DATABASES[db[:type].to_sym].new(db, tmpdir, @params[:encryption_key]).load_dump file_path
      FileUtils.remove_entry_secure file_path
    end
  end
end
send_db_dump(file, db) click to toggle source
# File lib/backup_foundation/job.rb, line 38
def send_db_dump(file, db)
  url = URI.parse "#{BackupFoundation::HOST}/api/file"
  upload_file = UploadIO.new(file, 'application/octet-stream', (db[:name] || db[:type]).to_s)
  request = Net::HTTP::Post::Multipart.new url.path,
    'file' => upload_file,
    'key'  => @params[:key],
    'gz'   => 'on',
    'gpg'  => @params[:encryption_key] ? 'on' : 'off',
    'md5'  => Digest::MD5.hexdigest(File.read(file))

  http = Net::HTTP.new url.host, url.port
  if url.is_a? URI::HTTPS
    http.use_ssl = true
    # for debug
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  retry_count = 0
  begin
    http.request request
  rescue Exception => e
    if retry_count < 8
      retry_count += 1
      upload_file.rewind
      sleep 2 ** retry_count
      retry
    end
  end
end