Class: EY::Backup::MysqlEngine
- Inherits:
-
Engine
show all
- Defined in:
- lib/ey_backup/engines/mysql_engine.rb
Constant Summary
Constants included
from Spawner
Spawner::CHUNK_SIZE
Instance Attribute Summary
Attributes inherited from Engine
#force, #host, #key_id, #password, #username
Instance Method Summary
collapse
Methods inherited from Engine
descendants, #gpg?, inherited, #initialize, label, lookup, register
Methods included from Spawner
#ioify, #run, #runs?, #spawn
Methods inherited from Base
#logger
Instance Method Details
#check_if_replica ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 42
def check_if_replica
err_msg="ERROR: Target host: '#{host}' is currently: "
err_msg=err_msg + "a replica based on 'show slave status', " if db_replicating?
err_msg=err_msg + "in read_only mode, " if read_only_on?
err_msg=err_msg + "restores should be processed against the master."
EY::Backup.logger.fatal(%Q{#{err_msg}}) if db_replicating? or read_only_on?
end
|
#cycle_database(database_name) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 99
def cycle_database(database_name)
query = "show create database #{database_name} \\G"
create_cmd = %x(mysql #{username_option} #{host_option} -NB -e "#{query}"|tail -n 1)
create_cmd="Create database #{database_name}" if create_cmd==""
%x(mysql #{username_option} #{host_option} -e 'DROP DATABASE IF EXISTS #{database_name}')
%x(mysql #{username_option} #{host_option} -e '#{create_cmd}')
end
|
#databases_option(db) ⇒ Object
59
60
61
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 59
def databases_option(db)
"--add-drop-database --databases #{db}"
end
|
#db_has_myisam?(database_name) ⇒ Boolean
75
76
77
78
79
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 75
def db_has_myisam?(database_name)
query, stdout = "SELECT 1 FROM information_schema.tables WHERE table_schema='#{database_name}' AND engine='MyISAM' LIMIT 1;", StringIO.new
spawn(%Q{mysql #{username_option} #{host_option} -N -e"#{query}"}, stdout)
stdout.string.strip == '1'
end
|
#db_replicating? ⇒ Boolean
81
82
83
84
85
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 81
def db_replicating?
stdout = StringIO.new
spawn(%Q{mysql #{username_option} #{host_option} -BN -e"show slave status"|wc -l}, stdout)
stdout.string.to_i > 0
end
|
#dump(database_name, basename) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 6
def dump(database_name, basename)
file = basename + '.sql'
command = "mysqldump #{username_option} #{host_option} #{single_transaction_option(database_name)} #{routines_option} #{master_data_option} #{databases_option(database_name)} 2> /tmp/eybackup.$$.dumperr"
if gpg?
command << " | " << GPGEncryptor.command_for(key_id)
file << GPGEncryptor.extension
else
command << " | " << GZipper.gzip
file << GZipper.extension
end
command << " > #{file}"
run(command, database_name)
file
end
|
#host_option ⇒ Object
63
64
65
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 63
def host_option
"-h#{host}"
end
|
#load(database_name, file) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 26
def load(database_name, file)
command = "cat #{file}"
if file =~ /.gpz$/
abort "Cannot restore a GPG backup directly; decrypt the file (#{file}) using your key and then load using the mysql client."
else
command << " | " << GZipper.gunzip
end
cycle_database(database_name)
command << " | mysql #{username_option} #{host_option} #{database_name} 2> /tmp/eybackup.$$.dumperr"
run(command, database_name)
end
|
#log_bin_on? ⇒ Boolean
93
94
95
96
97
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 93
def log_bin_on?
stdout = StringIO.new
spawn(%Q{mysql #{username_option} #{host_option} -BN -e"select @@global.log_bin"}, stdout)
stdout.string.to_i == 1
end
|
#master_data_option ⇒ Object
55
56
57
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 55
def master_data_option
"--master-data=2" if log_bin_on?
end
|
#read_only_on? ⇒ Boolean
87
88
89
90
91
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 87
def read_only_on?
stdout = StringIO.new
spawn(%Q{mysql #{username_option} #{host_option} -BN -e"select @@global.read_only"}, stdout)
stdout.string.to_i == 1
end
|
#routines_option ⇒ Object
51
52
53
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 51
def routines_option
"--routines"
end
|
#single_transaction_option(database_name) ⇒ Object
71
72
73
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 71
def single_transaction_option(database_name)
'--single-transaction' unless db_has_myisam?(database_name)
end
|
#suffix ⇒ Object
109
110
111
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 109
def suffix
/sql\.(gz|gpz)$/
end
|
#username_option ⇒ Object
67
68
69
|
# File 'lib/ey_backup/engines/mysql_engine.rb', line 67
def username_option
"-u#{username}"
end
|