Class: Backupper

Inherits:
Object
  • Object
show all
Includes:
SSHKit::DSL
Defined in:
lib/backupper/version.rb,
lib/backupper/backupper.rb

Constant Summary collapse

VERSION =
'0.6.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(conf_file_path) ⇒ Backupper

Returns a new instance of Backupper.



22
23
24
25
26
27
28
29
# File 'lib/backupper/backupper.rb', line 22

def initialize(conf_file_path)
  conf = YAML.load_file(conf_file_path)
  @default = conf['default'] || {}
  @mailer = conf['mailer'] || {}
  @conf = conf.select { |k, v| !%w[default mailer].include?(k) && (v['disabled'].nil? || v['disabled'] == false) }
  @report = {}
  @logger = SSHKit::Formatter::Pretty.new($stdout)
end

Instance Method Details

#backup!(only_these_keys = nil) ⇒ Object



31
32
33
34
35
36
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
# File 'lib/backupper/backupper.rb', line 31

def backup!(only_these_keys = nil)
  filtered_conf = @conf
  if only_these_keys
    filtered_conf = @conf.slice(*only_these_keys)
    (only_these_keys - @conf.keys).each do |k|
      err = "Configuration key '#{k}' not found or disabled!"
      error(k, err)
      @logger.error err
    end
  end
  filtered_conf.each do |k, options|
    @logger.info "[#{Time.now}] ⬇️  backing up #{k}..."
    o, err = setup_options(options)
    if err
      error(k, err)
      @logger.error err
      next
    end
    begin
      do_and_download_dump(
        key:          k,
        adapter:      o['adapter'],
        url:          o['url'],
        password:     o['password'],
        database:     o['database'],
        db_username:  o['db_username'],
        db_password:  o['db_password'],
        dump_options: o['dump_options'],
        sudo:         o['sudo'],
        outdir:       o['outdir'],
        extra_copy:   o['extra_copy']
      )
    rescue SSHKit::Runner::ExecuteError => e
      error(k, e.to_s)
      @logger.error e
    end
  end
  send_report_email!
end