cloner 0.5.7 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +3 -0
- data/lib/cloner.rb +10 -6
- data/lib/cloner/ar.rb +50 -0
- data/lib/cloner/internal.rb +3 -1
- data/lib/cloner/mysql.rb +77 -0
- data/lib/cloner/postgres.rb +16 -34
- data/lib/cloner/rsync.rb +1 -1
- data/lib/cloner/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dedfd02ba4a012b43cb0775bd2ec3cd563691415
|
4
|
+
data.tar.gz: 0607bccaae03a55e6fe60fa80e0fedc2883606ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4beb6a1618af270122ea5205029b49ad4ebe632f7451d7bfba0c280601cf39c96b26d32aa8a7f170fac0f49928766248d37185ae34aa53ffb8a52f1a813d1eab
|
7
|
+
data.tar.gz: 6ea9aaa455d224a9e82ba80bf2a2cfd19e21f4483c12eedd9b58cda2652af71c48e4f913d27c7d0121930ac80e332c832bcb3ddd79acc1f44b987cc5066fbe27
|
data/HISTORY.md
ADDED
data/lib/cloner.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
require "cloner/version"
|
2
2
|
require 'thor'
|
3
3
|
require 'active_support/concern'
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
module Cloner
|
5
|
+
autoload :Base, "cloner/base"
|
6
|
+
autoload :Internal, "cloner/internal"
|
7
|
+
autoload :Ar, "cloner/ar"
|
8
|
+
autoload :MongoDB, "cloner/mongodb"
|
9
|
+
autoload :Postgres, "cloner/postgres"
|
10
|
+
autoload :MySQL, "cloner/mysql"
|
11
|
+
autoload :SSH, "cloner/ssh"
|
12
|
+
autoload :RSync, "cloner/rsync"
|
13
|
+
end
|
10
14
|
|
data/lib/cloner/ar.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Cloner::Ar
|
2
|
+
def ar_conf
|
3
|
+
@conf ||= begin
|
4
|
+
YAML.load_file(Rails.root.join('config', 'database.yml'))[Rails.env]
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def ar_to
|
9
|
+
ar_conf['database']
|
10
|
+
end
|
11
|
+
|
12
|
+
def ar_r_conf
|
13
|
+
@ar_r_conf ||= begin
|
14
|
+
do_ssh do |ssh|
|
15
|
+
ret = ssh_exec!(ssh, "cat #{e(remote_app_path + '/config/database.yml')}")
|
16
|
+
check_ssh_err(ret)
|
17
|
+
begin
|
18
|
+
res = YAML.load(ret[0])[env_from]
|
19
|
+
raise 'no data' if res.blank?
|
20
|
+
#res['host'] ||= '127.0.0.1'
|
21
|
+
rescue Exception => e
|
22
|
+
puts "unable to read remote database.yml for env #{env_from}."
|
23
|
+
puts "Remote file contents:"
|
24
|
+
puts ret[0]
|
25
|
+
end
|
26
|
+
res
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def clone_ar
|
32
|
+
if ar_conf["adapter"] != ar_r_conf["adapter"]
|
33
|
+
puts "Error: ActiveRecord adapter mismatch: local #{ar_conf["adapter"]}, remote #{ar_r_conf["adapter"]}"
|
34
|
+
puts "it is not possible to convert from one database to another via this tool."
|
35
|
+
exit
|
36
|
+
end
|
37
|
+
|
38
|
+
case ar_conf["adapter"]
|
39
|
+
when 'postgresql'
|
40
|
+
clone_pg
|
41
|
+
when 'mysql2'
|
42
|
+
clone_my
|
43
|
+
else
|
44
|
+
puts "unknown activerecord adapter: #{ar_conf["adapter"]}"
|
45
|
+
puts "currently supported adapters: mysql2, postgresql"
|
46
|
+
exit
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
data/lib/cloner/internal.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module Cloner::Internal
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
include Cloner::MongoDB
|
4
|
+
include Cloner::Ar
|
4
5
|
include Cloner::Postgres
|
6
|
+
include Cloner::MySQL
|
5
7
|
include Cloner::SSH
|
6
8
|
include Cloner::RSync
|
7
9
|
|
@@ -27,7 +29,7 @@ module Cloner::Internal
|
|
27
29
|
if defined?(Mongoid)
|
28
30
|
clone_mongodb
|
29
31
|
else
|
30
|
-
|
32
|
+
clone_ar
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
data/lib/cloner/mysql.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
module Cloner::MySQL
|
2
|
+
def my_local_auth
|
3
|
+
if ar_conf['password'].blank?
|
4
|
+
""
|
5
|
+
else
|
6
|
+
"--password='#{ar_conf['password']}'"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def my_remote_auth
|
11
|
+
if ar_r_conf['password'].blank?
|
12
|
+
""
|
13
|
+
else
|
14
|
+
"--password='#{ar_r_conf['password']}'"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def my_dump_param
|
19
|
+
"--add-drop-table"
|
20
|
+
end
|
21
|
+
|
22
|
+
def my_restore_param
|
23
|
+
""
|
24
|
+
end
|
25
|
+
|
26
|
+
def my_bin_path(util)
|
27
|
+
util
|
28
|
+
end
|
29
|
+
|
30
|
+
def my_dump_remote
|
31
|
+
puts "backup remote DB via ssh"
|
32
|
+
do_ssh do |ssh|
|
33
|
+
ssh.exec!("rm -R #{e remote_dump_path}")
|
34
|
+
ret = ssh_exec!(ssh, "mkdir -p #{e remote_dump_path}")
|
35
|
+
check_ssh_err(ret)
|
36
|
+
host = ar_r_conf['host'].present? ? " --host #{e ar_r_conf['host']}" : ""
|
37
|
+
port = ar_r_conf['port'].present? ? " --port #{e ar_r_conf['port']}" : ""
|
38
|
+
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 + '/cloner.sql')}"
|
39
|
+
puts dump if verbose?
|
40
|
+
ret = ssh_exec!(ssh, dump)
|
41
|
+
check_ssh_err(ret)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def my_dump_restore
|
46
|
+
puts "restoring DB"
|
47
|
+
host = ar_conf['host'].present? ? " -h #{e ar_conf['host']}" : ""
|
48
|
+
port = ar_conf['port'].present? ? " -p #{e ar_conf['port']}" : ""
|
49
|
+
restore = "#{my_bin_path 'mysql'} #{my_restore_param} --user #{e ar_conf['username']} #{my_local_auth}#{host}#{port} #{e ar_to} < #{e(my_path + '/cloner.sql')}"
|
50
|
+
puts restore if verbose?
|
51
|
+
pipe = IO.popen(restore)
|
52
|
+
while (line = pipe.gets)
|
53
|
+
print line if verbose?
|
54
|
+
end
|
55
|
+
ret = $?.to_i
|
56
|
+
if ret != 0
|
57
|
+
puts "Error: local command exited with #{ret}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def my_path
|
62
|
+
Rails.root.join("tmp", "dump").to_s
|
63
|
+
end
|
64
|
+
|
65
|
+
def my_dump_copy
|
66
|
+
FileUtils.mkdir_p(my_path)
|
67
|
+
`mkdir -p #{e my_path}`
|
68
|
+
rsync(remote_dump_path + '/', my_path)
|
69
|
+
end
|
70
|
+
|
71
|
+
def clone_my
|
72
|
+
my_dump_remote()
|
73
|
+
my_dump_copy()
|
74
|
+
my_dump_restore()
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
data/lib/cloner/postgres.rb
CHANGED
@@ -1,37 +1,8 @@
|
|
1
1
|
module Cloner::Postgres
|
2
2
|
extend ActiveSupport::Concern
|
3
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
|
-
do_ssh do |ssh|
|
17
|
-
ret = ssh_exec!(ssh, "cat #{e(remote_app_path + '/config/database.yml')}")
|
18
|
-
check_ssh_err(ret)
|
19
|
-
begin
|
20
|
-
res = YAML.load(ret[0])[env_from]
|
21
|
-
raise 'no data' if res.blank?
|
22
|
-
#res['host'] ||= '127.0.0.1'
|
23
|
-
rescue Exception => e
|
24
|
-
puts "unable to read remote database.yml for env #{env_from}."
|
25
|
-
puts "Remote file contents:"
|
26
|
-
puts ret[0]
|
27
|
-
end
|
28
|
-
res
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
4
|
def pg_local_auth
|
34
|
-
if ar_conf['password'].
|
5
|
+
if ar_conf['password'].blank?
|
35
6
|
""
|
36
7
|
else
|
37
8
|
"PGPASSWORD='#{ar_conf['password']}' "
|
@@ -39,7 +10,7 @@ module Cloner::Postgres
|
|
39
10
|
end
|
40
11
|
|
41
12
|
def pg_remote_auth
|
42
|
-
if ar_r_conf['password'].
|
13
|
+
if ar_r_conf['password'].blank?
|
43
14
|
""
|
44
15
|
else
|
45
16
|
"PGPASSWORD='#{ar_r_conf['password']}' "
|
@@ -50,6 +21,17 @@ module Cloner::Postgres
|
|
50
21
|
""
|
51
22
|
end
|
52
23
|
|
24
|
+
def pg_dump_param
|
25
|
+
if pg_dump_extra != ""
|
26
|
+
puts "WARN pg_dump_extra is deprecated, use def pg_dump_param; super + ' extra'"
|
27
|
+
end
|
28
|
+
"-Fc #{pg_dump_extra}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def pg_restore_param
|
32
|
+
"--no-owner -Fc -c"
|
33
|
+
end
|
34
|
+
|
53
35
|
def pg_bin_path(util)
|
54
36
|
util
|
55
37
|
end
|
@@ -62,7 +44,7 @@ module Cloner::Postgres
|
|
62
44
|
check_ssh_err(ret)
|
63
45
|
host = ar_r_conf['host'].present? ? " -h #{e ar_r_conf['host']}" : ""
|
64
46
|
port = ar_r_conf['port'].present? ? " -p #{e ar_r_conf['port']}" : ""
|
65
|
-
dump = pg_remote_auth + "#{pg_bin_path 'pg_dump'}
|
47
|
+
dump = pg_remote_auth + "#{pg_bin_path 'pg_dump'} #{pg_dump_param} -U #{e ar_r_conf['username']}#{host}#{port} #{e ar_r_conf['database']} > #{e(remote_dump_path + '/tmp.bak')}"
|
66
48
|
puts dump if verbose?
|
67
49
|
ret = ssh_exec!(ssh, dump)
|
68
50
|
check_ssh_err(ret)
|
@@ -73,14 +55,14 @@ module Cloner::Postgres
|
|
73
55
|
puts "restoring DB"
|
74
56
|
host = ar_conf['host'].present? ? " -h #{e ar_conf['host']}" : ""
|
75
57
|
port = ar_conf['port'].present? ? " -p #{e ar_conf['port']}" : ""
|
76
|
-
restore = pg_local_auth + "#{pg_bin_path 'pg_restore'}
|
58
|
+
restore = pg_local_auth + "#{pg_bin_path 'pg_restore'} #{pg_restore_param} -U #{e ar_conf['username']}#{host}#{port} -d #{e ar_to} #{e(pg_path + '/tmp.bak')}"
|
77
59
|
puts restore if verbose?
|
78
60
|
pipe = IO.popen(restore)
|
79
61
|
while (line = pipe.gets)
|
80
62
|
print line if verbose?
|
81
63
|
end
|
82
64
|
ret = $?.to_i
|
83
|
-
if ret != 0
|
65
|
+
if ret != 0
|
84
66
|
puts "Error: local command exited with #{ret}"
|
85
67
|
end
|
86
68
|
end
|
data/lib/cloner/rsync.rb
CHANGED
data/lib/cloner/version.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.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- glebtv
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -91,14 +91,17 @@ files:
|
|
91
91
|
- ".ruby-gemset"
|
92
92
|
- ".ruby-version"
|
93
93
|
- Gemfile
|
94
|
+
- HISTORY.md
|
94
95
|
- LICENSE.txt
|
95
96
|
- README.md
|
96
97
|
- Rakefile
|
97
98
|
- cloner.gemspec
|
98
99
|
- lib/cloner.rb
|
100
|
+
- lib/cloner/ar.rb
|
99
101
|
- lib/cloner/base.rb
|
100
102
|
- lib/cloner/internal.rb
|
101
103
|
- lib/cloner/mongodb.rb
|
104
|
+
- lib/cloner/mysql.rb
|
102
105
|
- lib/cloner/postgres.rb
|
103
106
|
- lib/cloner/rsync.rb
|
104
107
|
- lib/cloner/ssh.rb
|
@@ -123,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
126
|
version: '0'
|
124
127
|
requirements: []
|
125
128
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.6.8
|
127
130
|
signing_key:
|
128
131
|
specification_version: 4
|
129
132
|
summary: Easily clone your production Mongoid database and files for local development
|