Class: BackupFoundation::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/backup_foundation/job.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Job

Returns a new instance of Job.



6
7
8
# File 'lib/backup_foundation/job.rb', line 6

def initialize(params)
  @params = params
end

Instance Method Details

#backup!Object



10
11
12
13
14
15
16
17
18
# 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) ⇒ Object



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
# 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(&block) ⇒ Object



30
31
32
33
34
35
36
# 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) ⇒ Object



94
95
96
97
98
99
100
101
# 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!Object



20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



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
# 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