Class: SimpleBackup::Utils::MySQL
- Inherits:
-
Object
- Object
- SimpleBackup::Utils::MySQL
- Includes:
- Singleton
- Defined in:
- lib/simple_backup/utils/mysql.rb
Constant Summary collapse
- @@logger =
Logger.instance
Instance Method Summary collapse
- #close ⇒ Object
- #dump(db, tables, dumpfile) ⇒ Object
- #host(value) ⇒ Object
-
#initialize ⇒ MySQL
constructor
A new instance of MySQL.
- #open ⇒ Object
- #pass(value) ⇒ Object
- #port(value) ⇒ Object
- #scan_tables(db) ⇒ Object
- #user(value) ⇒ Object
Constructor Details
#initialize ⇒ MySQL
Returns a new instance of MySQL.
11 12 13 14 15 16 |
# File 'lib/simple_backup/utils/mysql.rb', line 11 def initialize @host = 'localhost' @port = 3306 @user = nil @pass = nil end |
Instance Method Details
#close ⇒ Object
28 29 30 |
# File 'lib/simple_backup/utils/mysql.rb', line 28 def close @conn.close unless @conn.nil? end |
#dump(db, tables, dumpfile) ⇒ Object
42 43 44 45 46 |
# File 'lib/simple_backup/utils/mysql.rb', line 42 def dump(db, tables, dumpfile) cmd = "mysqldump --flush-logs --flush-privileges --order-by-primary --complete-insert -C -h #{@host} -u #{@user} -p#{@pass} #{db} #{tables.join(' ')} > #{dumpfile}" @@logger.debug "Running command: #{cmd}" `#{cmd}` end |
#host(value) ⇒ Object
48 49 50 |
# File 'lib/simple_backup/utils/mysql.rb', line 48 def host(value) @host = value end |
#open ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/simple_backup/utils/mysql.rb', line 18 def open return nil unless @conn.nil? @conn = Mysql2::Client.new(host: @host, port: @port, username: @user, password: @pass) @existing_dbs = [] @conn.query("SHOW DATABASES").each do |row| @existing_dbs << row['Database'] end end |
#pass(value) ⇒ Object
60 61 62 |
# File 'lib/simple_backup/utils/mysql.rb', line 60 def pass(value) @pass = value end |
#port(value) ⇒ Object
52 53 54 |
# File 'lib/simple_backup/utils/mysql.rb', line 52 def port(value) @port = value end |
#scan_tables(db) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/simple_backup/utils/mysql.rb', line 32 def scan_tables(db) return nil unless @existing_dbs.include?(db) tables = [] @conn.query("SHOW TABLES FROM `#{db}`").each do |row| tables << row["Tables_in_#{db}"] end tables end |
#user(value) ⇒ Object
56 57 58 |
# File 'lib/simple_backup/utils/mysql.rb', line 56 def user(value) @user = value end |