cloner 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cloner/internal.rb +6 -1
- data/lib/cloner/mongodb.rb +1 -1
- data/lib/cloner/postgres.rb +82 -0
- data/lib/cloner/version.rb +1 -1
- data/lib/cloner.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 766c1d83045885a8d0583a7d68996e10e285f445
|
4
|
+
data.tar.gz: 30a73229c201aaee192ff864d34b986209c78530
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10ae7e947d725997f0e382107aa2db7eeeb32c9fe097b341b8ad3f8e49cf3d1d7e01a4fea20bedf94d18df74bad91cd69693bc86d009f02c5a18951f4245c981
|
7
|
+
data.tar.gz: 63d80240f8d443a070ebbf6f7a699ef1587ab1159e6bd65ac256641cf5b85e34c15e98d1c5c1eccda0a33fd31fc8e4129457b31783c99342a5af39cb4374da4e
|
data/lib/cloner/internal.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Cloner::Internal
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
include Cloner::MongoDB
|
4
|
+
include Cloner::Postgres
|
4
5
|
include Cloner::SSH
|
5
6
|
include Cloner::RSync
|
6
7
|
|
@@ -19,7 +20,11 @@ module Cloner::Internal
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def clone_db
|
22
|
-
|
23
|
+
if defined?(Mongoid)
|
24
|
+
clone_mongodb
|
25
|
+
else
|
26
|
+
clone_pg
|
27
|
+
end
|
23
28
|
end
|
24
29
|
end
|
25
30
|
|
data/lib/cloner/mongodb.rb
CHANGED
@@ -0,0 +1,82 @@
|
|
1
|
+
module Cloner::Postgres
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
def ar_conf
|
5
|
+
@conf ||= begin
|
6
|
+
YAML.load_file(Rails.root.join('config', 'database.yml'))[Rails.env]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def ar_to
|
11
|
+
ar_conf['database']
|
12
|
+
end
|
13
|
+
|
14
|
+
def ar_r_conf
|
15
|
+
@ar_r_conf ||= begin
|
16
|
+
Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
|
17
|
+
ret = ssh_exec!(ssh, "cat #{remote_app_path}/config/database.yml")
|
18
|
+
check_ssh_err(ret)
|
19
|
+
YAML.load(ret[0])[env_from]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def pg_local_auth
|
25
|
+
if ar_conf['password'].nil?
|
26
|
+
""
|
27
|
+
else
|
28
|
+
"PGPASSWORD='#{ar_conf['password']}' "
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def pg_remote_auth
|
33
|
+
if ar_r_conf['password'].nil?
|
34
|
+
""
|
35
|
+
else
|
36
|
+
"PGPASSWORD='#{ar_r_conf['password']}' "
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def pg_dump_remote
|
41
|
+
puts "backup remote DB via ssh"
|
42
|
+
Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
|
43
|
+
ssh.exec!("rm -R #{remote_dump_path}")
|
44
|
+
ret = ssh_exec!(ssh, "mkdir -p #{remote_dump_path}")
|
45
|
+
check_ssh_err(ret)
|
46
|
+
dump = pg_remote_auth + "pg_dump -Fc -U #{ar_r_conf['username']} #{ar_r_conf['database']} > #{remote_dump_path}/tmp.bak"
|
47
|
+
puts dump if verbose?
|
48
|
+
ret = ssh_exec!(ssh, dump)
|
49
|
+
check_ssh_err(ret)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def pg_dump_restore
|
54
|
+
puts "restoring DB"
|
55
|
+
restore = pg_local_auth + "pg_restore -Fc -U #{ar_conf['username']} -d #{ar_to} #{pg_path}/tmp.bak"
|
56
|
+
puts restore if verbose?
|
57
|
+
pipe = IO.popen(restore)
|
58
|
+
while (line = pipe.gets)
|
59
|
+
print line if verbose?
|
60
|
+
end
|
61
|
+
ret = $?.to_i
|
62
|
+
if ret != 0
|
63
|
+
puts "Error: local command exited with #{ret}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def pg_path
|
68
|
+
Rails.root.join("tmp", "dump").to_s
|
69
|
+
end
|
70
|
+
|
71
|
+
def pg_dump_copy
|
72
|
+
FileUtils.mkdir_p(pg_path)
|
73
|
+
`mkdir -p #{pg_path}`
|
74
|
+
rsync("#{remote_dump_path}/", pg_path)
|
75
|
+
end
|
76
|
+
|
77
|
+
def clone_pg
|
78
|
+
pg_dump_remote()
|
79
|
+
pg_dump_copy()
|
80
|
+
pg_dump_restore()
|
81
|
+
end
|
82
|
+
end
|
data/lib/cloner/version.rb
CHANGED
data/lib/cloner.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- glebtv
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- lib/cloner/base.rb
|
100
100
|
- lib/cloner/internal.rb
|
101
101
|
- lib/cloner/mongodb.rb
|
102
|
+
- lib/cloner/postgres.rb
|
102
103
|
- lib/cloner/rsync.rb
|
103
104
|
- lib/cloner/ssh.rb
|
104
105
|
- lib/cloner/version.rb
|