Module: Cloner::Postgres

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

Instance Method Summary collapse

Instance Method Details

#clone_pgObject



88
89
90
91
92
# File 'lib/cloner/postgres.rb', line 88

def clone_pg
  pg_dump_remote()
  pg_dump_copy()
  pg_dump_restore()
end

#pg_bin_path(util) ⇒ Object



35
36
37
# File 'lib/cloner/postgres.rb', line 35

def pg_bin_path(util)
  util
end

#pg_dump_copyObject



82
83
84
85
86
# File 'lib/cloner/postgres.rb', line 82

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

#pg_dump_extraObject



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

def pg_dump_extra
  ""
end

#pg_dump_paramObject



24
25
26
27
28
29
# File 'lib/cloner/postgres.rb', line 24

def pg_dump_param
  if pg_dump_extra != ""
    puts "WARN pg_dump_extra is deprecated, use def pg_dump_param; super + ' extra'"
  end
  "-Fc #{pg_dump_extra}"
end

#pg_dump_remoteObject



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

def pg_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? ? " -h #{e ar_r_conf['host']}" : ""
    port = ar_r_conf['port'].present? ? " -p #{e ar_r_conf['port']}" : ""
    dump = pg_remote_auth + "#{pg_remote_bin_path 'pg_dump'} #{pg_dump_param} -U #{e ar_r_conf['username']}#{host}#{port} #{e ar_r_conf['database']} > #{e(remote_dump_path + '/'+db_file_name+'.bak')}"
    puts dump if verbose?
    ret = ssh_exec!(ssh, dump)
    check_ssh_err(ret)
  end
end

#pg_dump_restoreObject



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

def pg_dump_restore
  puts "restoring DB"
  host = ar_conf['host'].present? ? " -h #{e ar_conf['host']}" : ""
  port = ar_conf['port'].present? ? " -p #{e ar_conf['port']}" : ""
  restore = pg_local_auth + "#{pg_local_bin_path 'pg_restore'} #{pg_restore_param} -U #{e ar_conf['username']}#{host}#{port} -d #{e ar_to} #{e(pg_path + '/'+db_file_name+'.bak')}"
  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

#pg_local_authObject



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

def pg_local_auth
  if ar_conf['password'].blank?
    ""
  else
    "PGPASSWORD='#{ar_conf['password']}' "
  end
end

#pg_local_bin_path(util) ⇒ Object



39
40
41
# File 'lib/cloner/postgres.rb', line 39

def pg_local_bin_path(util)
  pg_bin_path(util)
end

#pg_pathObject



78
79
80
# File 'lib/cloner/postgres.rb', line 78

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

#pg_remote_authObject



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

def pg_remote_auth
  if ar_r_conf['password'].blank?
    ""
  else
    "PGPASSWORD='#{ar_r_conf['password']}' "
  end
end

#pg_remote_bin_path(util) ⇒ Object



43
44
45
# File 'lib/cloner/postgres.rb', line 43

def pg_remote_bin_path(util)
  pg_bin_path(util)
end

#pg_restore_paramObject



31
32
33
# File 'lib/cloner/postgres.rb', line 31

def pg_restore_param
  "--no-owner -Fc -c"
end