Class: Backup::Database::MySQL

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/database/mysql.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#dump_path, #utility_path

Instance Method Summary collapse

Methods inherited from Base

#log!, #prepare!

Methods included from Configuration::Helpers

#clear_defaults!, #getter_methods, #load_defaults!, #setter_methods

Methods included from CLI

#mkdir, #rm, #run, #utility

Constructor Details

#initialize(&block) ⇒ MySQL

Creates a new instance of the MySQL adapter object



33
34
35
36
37
38
39
40
41
42
# File 'lib/backup/database/mysql.rb', line 33

def initialize(&block)
  load_defaults!

  @skip_tables        ||= Array.new
  @only_tables        ||= Array.new
  @additional_options ||= Array.new

  instance_eval(&block)
  prepare!
end

Instance Attribute Details

#additional_optionsObject

Additional “mysqldump” options



29
30
31
# File 'lib/backup/database/mysql.rb', line 29

def additional_options
  @additional_options
end

#hostObject

Connectivity options



17
18
19
# File 'lib/backup/database/mysql.rb', line 17

def host
  @host
end

#nameObject

Name of the database that needs to get dumped



9
10
11
# File 'lib/backup/database/mysql.rb', line 9

def name
  @name
end

#only_tablesObject

Tables to dump, tables that aren’t specified won’t get dumped



25
26
27
# File 'lib/backup/database/mysql.rb', line 25

def only_tables
  @only_tables
end

#passwordObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/mysql.rb', line 13

def password
  @password
end

#portObject

Connectivity options



17
18
19
# File 'lib/backup/database/mysql.rb', line 17

def port
  @port
end

#skip_tablesObject

Tables to skip while dumping the database



21
22
23
# File 'lib/backup/database/mysql.rb', line 21

def skip_tables
  @skip_tables
end

#socketObject

Connectivity options



17
18
19
# File 'lib/backup/database/mysql.rb', line 17

def socket
  @socket
end

#usernameObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/mysql.rb', line 13

def username
  @username
end

Instance Method Details

#connectivity_optionsObject

Builds the MySQL connectivity options syntax to connect the user to perform the database dumping process



73
74
75
76
77
78
# File 'lib/backup/database/mysql.rb', line 73

def connectivity_options
  %w[host port socket].map do |option|
    next if send(option).nil? or (send(option).respond_to?(:empty?) and send(option).empty?)
    "--#{option}='#{send(option)}'"
  end.compact.join("\s")
end

#credential_optionsObject

Builds the credentials MySQL syntax to authenticate the user to perform the database dumping process



63
64
65
66
67
68
# File 'lib/backup/database/mysql.rb', line 63

def credential_options
  %w[username password].map do |option|
    next if send(option).nil? or send(option).empty?
    "--#{option}='#{send(option)}'".gsub('--username', '--user')
  end.compact.join("\s")
end

#mysqldumpObject

Builds the full mysqldump string based on all attributes



89
90
91
92
# File 'lib/backup/database/mysql.rb', line 89

def mysqldump
  "#{ utility(:mysqldump) } #{ credential_options } #{ connectivity_options } " +
  "#{ options } #{ name } #{ tables_to_dump } #{ tables_to_skip }"
end

#optionsObject

Builds a MySQL compatible string for the additional options specified by the user



83
84
85
# File 'lib/backup/database/mysql.rb', line 83

def options
  additional_options.join("\s")
end

#perform!Object

Performs the mysqldump command and outputs the data to the specified path based on the ‘trigger’



97
98
99
100
# File 'lib/backup/database/mysql.rb', line 97

def perform!
  log!
  run("#{mysqldump} > '#{File.join(dump_path, name)}.sql'")
end

#tables_to_dumpObject

Builds the MySQL syntax for specifying which tables to dump during the dumping of the database



56
57
58
# File 'lib/backup/database/mysql.rb', line 56

def tables_to_dump
  only_tables.join("\s")
end

#tables_to_skipObject

Builds the MySQL syntax for specifying which tables to skip during the dumping of the database



47
48
49
50
51
# File 'lib/backup/database/mysql.rb', line 47

def tables_to_skip
  skip_tables.map do |table|
    "--ignore-table='#{name}.#{table}'"
  end.join("\s")
end