Class: Backupper
- Inherits:
-
Object
- Object
- Backupper
- Defined in:
- lib/backupper/version.rb,
lib/backupper/backupper.rb
Constant Summary collapse
- VERSION =
'0.2.1'
Instance Method Summary collapse
- #backup! ⇒ Object
- #download_dump(key:, adapter: 'mysql', host:, password: nil, database:, db_username: 'root', db_password: nil, dump_options: nil, outdir:, extra_copy: nil) ⇒ Object
-
#initialize(conf_file_path) ⇒ Backupper
constructor
A new instance of Backupper.
- #mysql_dump_command(database:, username: 'root', password: nil, dump_options: nil, outfile:) ⇒ Object
- #postgresql_dump_command(database:, username: 'root', password: nil, dump_options: nil, outfile:) ⇒ Object
Constructor Details
#initialize(conf_file_path) ⇒ Backupper
Returns a new instance of Backupper.
16 17 18 19 20 21 22 |
# File 'lib/backupper/backupper.rb', line 16 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 = {} end |
Instance Method Details
#backup! ⇒ Object
24 25 26 27 28 29 30 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 70 71 72 73 74 75 76 |
# File 'lib/backupper/backupper.rb', line 24 def backup! @conf.each do |k, | o = @default.merge() puts "⬇️ backing up #{k}..." outdir = check_dir(o['dump'].to_s) unless outdir err = 'Invalid directory where to save database dump' error(k, err) puts err next end host = o['host'] host = "#{o['username']}@#{host}" if o['username'] host = "#{host}:#{o['port']}" if o['port'] adapter = o['adapter'] || 'mysql' unless self.respond_to?("#{adapter}_dump_command") err = "Cannot handle adapter '#{adapter}'" error(k, err) puts err next end unless o['database'] err = 'Please specify database name!' error(k, err) puts err next end begin download_dump( key: k, adapter: adapter, host: host, password: o['password'], database: o['database'], db_username: o['db_username'], db_password: o['db_password'], dump_options: o['dump_options'], outdir: outdir, extra_copy: o['extra_copy'] ) rescue SSHKit::Runner::ExecuteError => e error(k, e.to_s) puts e end end if @report.any? && @mailer['from'] && @mailer['to'] && @mailer['password'] begin Mailer.send(from: @mailer['from'], to: @mailer['to'], password: @mailer['password'], report: @report) rescue Net::SMTPAuthenticationError => e puts e end end end |
#download_dump(key:, adapter: 'mysql', host:, password: nil, database:, db_username: 'root', db_password: nil, dump_options: nil, outdir:, extra_copy: nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/backupper/backupper.rb', line 95 def download_dump(key:, adapter: 'mysql', host:, password: nil, database:, db_username: 'root', db_password: nil, dump_options: nil, outdir:, extra_copy: nil) if self.respond_to?("#{adapter}_dump_command") t = Time.now path = nil filename = "#{key}__#{database}.sql.bz2" tempfile = File.join('/tmp', filename) dumpname = "#{Time.now.strftime('%Y-%M-%d_%H-%M-%S')}__#{filename}" path = File.join(outdir, dumpname) backupper = self on(host) do |client| client.password = password execute 'set -o pipefail; ' + backupper.send("#{adapter}_dump_command", database: database, username: db_username, password: db_password, dump_options: , outfile: tempfile) download! tempfile, path execute :rm, tempfile end extra_copy = check_dir(extra_copy) FileUtils.cp(path, extra_copy) if extra_copy @report[key] = { path: File.absolute_path(path), size: (File.size(path).to_f / 2**20).round(2), time: (Time.now - t).round(2) } @report[key].merge!({extra_copy: File.absolute_path(File.join(extra_copy, dumpname))}) if extra_copy end end |
#mysql_dump_command(database:, username: 'root', password: nil, dump_options: nil, outfile:) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/backupper/backupper.rb', line 78 def mysql_dump_command(database:, username: 'root', password: nil, dump_options: nil, outfile:) params = [] params << "--databases '#{database}'" params << "-u#{username}" params << "-p#{password}" if password params << if return "mysqldump #{params.join(' ')} | bzip2 > '#{outfile}'" end |
#postgresql_dump_command(database:, username: 'root', password: nil, dump_options: nil, outfile:) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/backupper/backupper.rb', line 87 def postgresql_dump_command(database:, username: 'root', password: nil, dump_options: nil, outfile:) params = [] params << "-U #{username}" params << "'#{database}'" params << if return "PGPASSWORD=#{password} pg_dump #{params.join(' ')} | bzip2 > '#{outfile}'" end |