module Cloner::MySQL

Public Instance Methods

clone_my() click to toggle source
# File lib/cloner/mysql.rb, line 71
def clone_my
  my_dump_remote()
  my_dump_copy()
  my_dump_restore()
end
my_bin_path(util) click to toggle source
# File lib/cloner/mysql.rb, line 26
def my_bin_path(util)
  util
end
my_dump_copy() click to toggle source
# File lib/cloner/mysql.rb, line 65
def my_dump_copy
  FileUtils.mkdir_p(my_path)
  `mkdir -p #{e my_path}`
  rsync(remote_dump_path, my_path)
end
my_dump_param() click to toggle source
# File lib/cloner/mysql.rb, line 18
def my_dump_param
  "--add-drop-table"
end
my_dump_remote() click to toggle source
# File lib/cloner/mysql.rb, line 30
def my_dump_remote
  puts "backup remote DB via ssh"
  do_ssh do |ssh|
    ssh.exec!("rm -R #{e remote_dump_path}")
    ret = ssh_exec!(ssh, "mkdir -p #{e remote_dump_path}")
    check_ssh_err(ret)
    host = ar_r_conf['host'].present? ? " --host #{e ar_r_conf['host']}" : ""
    port = ar_r_conf['port'].present? ? " --port #{e ar_r_conf['port']}" : ""
    dump = "#{my_bin_path 'mysqldump'} #{my_dump_param} --user #{e ar_r_conf['username']} #{my_remote_auth}#{host}#{port} #{e ar_r_conf['database']} > #{e(remote_dump_path + '/'+db_file_name+'.sql')}"
    puts dump if verbose?
    ret = ssh_exec!(ssh, dump)
    check_ssh_err(ret)
  end
end
my_dump_restore() click to toggle source
# File lib/cloner/mysql.rb, line 45
def my_dump_restore
  puts "restoring DB"
  host = ar_conf['host'].present? ? " --host #{e ar_conf['host']}" : ""
  port = ar_conf['port'].present? ? " --port #{e ar_conf['port']}" : ""
  restore = "#{my_bin_path 'mysql'} #{my_restore_param} --user #{e ar_conf['username']} #{my_local_auth}#{host}#{port} #{e ar_to} < #{e(my_path + '/'+db_file_name+'.sql')}"
  puts restore if verbose?
  pipe = IO.popen(restore)
  while (line = pipe.gets)
    print line if verbose?
  end
  ret = $?.to_i
  if ret != 0
    puts "Error: local command exited with #{ret}"
  end
end
my_local_auth() click to toggle source
# File lib/cloner/mysql.rb, line 2
def my_local_auth
  if ar_conf['password'].blank?
    ""
  else
    "--password='#{ar_conf['password']}'"
  end
end
my_path() click to toggle source
# File lib/cloner/mysql.rb, line 61
def my_path
  Rails.root.join("tmp", "dump").to_s
end
my_remote_auth() click to toggle source
# File lib/cloner/mysql.rb, line 10
def my_remote_auth
  if ar_r_conf['password'].blank?
    ""
  else
    "--password='#{ar_r_conf['password']}'"
  end
end
my_restore_param() click to toggle source
# File lib/cloner/mysql.rb, line 22
def my_restore_param
  ""
end