Module: Cloner::MySQL

Extended by:
ActiveSupport::Concern
Included in:
Internal
Defined in:
lib/cloner/mysql.rb

Instance Method Summary collapse

Instance Method Details

#clone_myObject



121
122
123
124
125
# File 'lib/cloner/mysql.rb', line 121

def clone_my
  my_dump_remote()
  my_dump_copy()
  my_dump_restore()
end

#my_bin_path(util) ⇒ Object



28
29
30
# File 'lib/cloner/mysql.rb', line 28

def my_bin_path(util)
  util
end

#my_dump_copyObject



115
116
117
118
119
# File 'lib/cloner/mysql.rb', line 115

def my_dump_copy
  FileUtils.mkdir_p(my_path)
  `mkdir -p #{e my_path}`
  rsync(remote_dump_path, my_path)
end

#my_dump_paramObject



20
21
22
# File 'lib/cloner/mysql.rb', line 20

def my_dump_param
  "--add-drop-table"
end

#my_dump_remoteObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cloner/mysql.rb', line 61

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 = remote_db_config['host'].present? ? " --host #{e remote_db_config['host']}" : ""
    port = remote_db_config['port'].present? ? " --port #{e remote_db_config['port']}" : ""
    dump = "#{my_remote_bin_path 'mysqldump'} #{my_dump_param} --user #{e remote_db_config['username']} #{my_remote_auth}#{host}#{port} #{e remote_db_config['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_restoreObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cloner/mysql.rb', line 76

def my_dump_restore
  puts "restoring DB"
  
  if local_docker_compose? && local_docker_compose_service
    # Docker compose restore - pipe the SQL file to docker compose exec
    host = local_db_config['host'].present? ? " --host #{e local_db_config['host']}" : ""
    port = local_db_config['port'].present? ? " --port #{e local_db_config['port']}" : ""
    
    compose_path = local_docker_compose_path
    compose_file = local_docker_compose_file
    service = local_docker_compose_service
    
    # MySQL requires password to be passed differently in Docker
    restore = "cat #{e(my_path + '/'+db_file_name+'.sql')} | (cd #{e compose_path} && docker compose -f #{e compose_file} exec -T #{e service} mysql #{my_restore_param} --user #{e local_db_config['username']} #{my_local_auth}#{host}#{port} #{e ar_to})"
    puts restore if verbose?
    system(restore)
    ret = $?.to_i
  else
    # Standard restore
    host = local_db_config['host'].present? ? " --host #{e local_db_config['host']}" : ""
    port = local_db_config['port'].present? ? " --port #{e local_db_config['port']}" : ""
    restore = "#{my_local_bin_path 'mysql'} #{my_restore_param} --user #{e local_db_config['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
  end
  
  if ret != 0
    puts "Error: local command exited with #{ret}"
  end
end

#my_local_authObject



4
5
6
7
8
9
10
# File 'lib/cloner/mysql.rb', line 4

def my_local_auth
  if local_db_config['password'].blank?
    ""
  else
    "--password='#{local_db_config['password']}'"
  end
end

#my_local_bin_path(util) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cloner/mysql.rb', line 32

def my_local_bin_path(util)
  if local_docker_compose? && local_docker_compose_service
    # For mysql restore, we pipe data in, so we don't wrap it
    return util if util == "mysql"
    
    # Build docker compose exec command
    compose_cmd = local_docker_compose_exec(
      local_docker_compose_service, 
      util,
      no_tty: true
    )
    return compose_cmd
  end
  my_bin_path(util)
end

#my_pathObject



111
112
113
# File 'lib/cloner/mysql.rb', line 111

def my_path
  Rails.root.join("tmp", "dump").to_s
end

#my_remote_authObject



12
13
14
15
16
17
18
# File 'lib/cloner/mysql.rb', line 12

def my_remote_auth
  if remote_db_config['password'].blank?
    ""
  else
    "--password='#{remote_db_config['password']}'"
  end
end

#my_remote_bin_path(util) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cloner/mysql.rb', line 48

def my_remote_bin_path(util)
  if remote_docker_compose? && remote_docker_compose_service
    # Build docker compose exec command for remote
    compose_cmd = remote_docker_compose_exec(
      remote_docker_compose_service,
      util,
      no_tty: true
    )
    return compose_cmd
  end
  my_bin_path(util)
end

#my_restore_paramObject



24
25
26
# File 'lib/cloner/mysql.rb', line 24

def my_restore_param
  ""
end