Class: Mys3ql::Mysql

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/mys3ql/mysql.rb

Instance Method Summary collapse

Methods included from Shell

#log, #run

Constructor Details

#initialize(config) ⇒ Mysql

Returns a new instance of Mysql.



7
8
9
# File 'lib/mys3ql/mysql.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#apply_bin_logs(*files) ⇒ Object



64
65
66
67
68
69
# File 'lib/mys3ql/mysql.rb', line 64

def apply_bin_logs(*files)
  cmd  = "#{@config.bin_path}mysqlbinlog --database=#{@config.database} #{files.join ' '}"
  cmd += " | #{@config.bin_path}mysql -u'#{@config.user}'"
  cmd += " -p'#{@config.password}'" if @config.password
  run cmd
end

#delete_dumpObject



30
31
32
33
# File 'lib/mys3ql/mysql.rb', line 30

def delete_dump
  File.delete dump_file
  log "mysql: deleted #{dump_file}"
end

#dumpObject

dump



15
16
17
18
19
20
21
22
23
24
# File 'lib/mys3ql/mysql.rb', line 15

def dump
  # --master-data=2         include the current binary log coordinates in the log file
  # --delete-master-logs    delete binary log files
  cmd  = "#{@config.bin_path}mysqldump"
  cmd += ' --quick --single-transaction --create-options --no-tablespaces'
  cmd += ' --flush-logs --master-data=2 --delete-master-logs' if binary_logging?
  cmd += cli_options
  cmd += " | gzip > #{dump_file}"
  run cmd
end

#dump_fileObject



26
27
28
# File 'lib/mys3ql/mysql.rb', line 26

def dump_file
  @dump_file ||= "#{timestamp}.sql.gz"
end

#each_bin_log(&block) ⇒ Object

flushes logs, yields each bar the last to the block



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mys3ql/mysql.rb', line 40

def each_bin_log(&block)
  # FLUSH LOGS    Closes and reopens any log file, including binary logs,
  #               to which the server is writing.  For binary logs, the sequence
  #               number of the binary log file is incremented by one relative to
  #               the previous file.
  #               https://dev.mysql.com/doc/refman/5.7/en/flush.html#flush-logs
  #               https://dev.mysql.com/doc/refman/5.7/en/flush.html#flush-binary-logs
  execute 'flush logs'
  Dir.glob("#{@config.bin_log}.[0-9]*")
     .sort_by { |f| f[/\d+/].to_i }
     .slice(0..-2)  # all logs except the last, which is newly created
     .each do |log_file|
    yield log_file
  end
end

#restore(file) ⇒ Object

restore



60
61
62
# File 'lib/mys3ql/mysql.rb', line 60

def restore(file)
  run "gunzip -c #{file} | #{@config.bin_path}mysql #{cli_options}"
end