Class: Rds::S3::Backup::MySqlCmds
- Inherits:
-
Object
- Object
- Rds::S3::Backup::MySqlCmds
- Defined in:
- lib/rds-s3-backup/mysqlcmds.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#dbname ⇒ Object
Returns the value of attribute dbname.
-
#host ⇒ Object
Returns the value of attribute host.
-
#passwd ⇒ Object
Returns the value of attribute passwd.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #_really_run_it(cmd) ⇒ Object
- #dump(output) ⇒ Object
- #exec(script) ⇒ Object
-
#initialize(host, user, passwd, dbname) ⇒ MySqlCmds
constructor
A new instance of MySqlCmds.
- #run(cmd) ⇒ Object
Constructor Details
#initialize(host, user, passwd, dbname) ⇒ MySqlCmds
Returns a new instance of MySqlCmds.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 23 def initialize(host, user, passwd, dbname) # Assert real parameters raise MySqlCmdsException.new("host is empty") if host.nil? || host.empty? raise MySqlCmdsException.new("user is empty") if user.nil? || user.empty? raise MySqlCmdsException.new("passwd is empty") if passwd.nil? || passwd.empty? raise MySqlCmdsException.new("dbname is empty") if dbname.nil? || dbname.empty? # Store instance vars @host = host @user = user @passwd = passwd @dbname = dbname # Find commands to execute @mysqldump = `which mysqldump`.chomp raise MySqlCmdsException.new("No mysqldump command found") if @mysqldump.empty? @mysql = `which mysql`.chomp raise MySqlCmdsException.new("No mysql command found") if @mysql.empty? @gzip = `which gzip`.chomp raise MySqlCmdsException.new("No gzip command found") if @gzip.empty? end |
Instance Attribute Details
#dbname ⇒ Object
Returns the value of attribute dbname.
21 22 23 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 21 def dbname @dbname end |
#host ⇒ Object
Returns the value of attribute host.
21 22 23 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 21 def host @host end |
#passwd ⇒ Object
Returns the value of attribute passwd.
21 22 23 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 21 def passwd @passwd end |
#user ⇒ Object
Returns the value of attribute user.
21 22 23 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 21 def user @user end |
Instance Method Details
#_really_run_it(cmd) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 105 def _really_run_it(cmd) result = Result.new result.stdout = Array.new result.stderr = Array.new Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| stdin.close until stdout.eof result.stdout << stdout.gets.chomp end stdout.close until stderr.eof result.stderr << stderr.gets.chomp end stderr.close result.code = wait_thr.value.exitstatus end result end |
#dump(output) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 47 def dump(output) raise MySqlCmdsException.new("output is not specified") if output.nil? || output.empty? dump_opts = ["--opt", "--add-drop-table", "--single-transaction", "--order-by-primary", "--host=#{@host}", "--user=#{@user}", "--password=XXPASSWORDXX", @dbname] gzip_opts = ["--fast", "--stdout", "> #{output}"] cmd = "#{@mysqldump} #{dump_opts.join(' ')} | #{@gzip} #{gzip_opts.join(' ')}" run(cmd.gsub(/XXPASSWORDXX/,@passwd)) output # return the dump file path end |
#exec(script) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 72 def exec(script) return if script.nil? || script.empty? raise MySqlCmdsException.new("#{script} is not readable!") unless File.readable?(script) obf_opts = ["--host=#{@host}", "--user=#{user}", "--password=XXPASSWORDXX", @dbname] cmd = "#{@mysql} #{obf_opts.join(' ')} < #{script}" run(cmd.gsub(/XXPASSWORDXX/,@passwd)) end |
#run(cmd) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rds-s3-backup/mysqlcmds.rb', line 89 def run(cmd) result = _really_run_it(cmd) if result.code != 0 error = "Mysql operation failed on mysql://#{@user}@#{@host}/#{@dbname}: Error code: #{result.code}.\n" error << "Command:\n#{cmd}\n" error << "stdout:\n#{result.stdout.join("\n")}\n" error << "stderr:\n#{result.stderr.join("\n")}\n" raise MySqlCmdsException.new error end end |