dumpman 1.3.0 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -4
- data/README.md +21 -3
- data/lib/dumpman.rb +2 -0
- data/lib/dumpman/adapters/base.rb +3 -3
- data/lib/dumpman/connection.rb +11 -1
- data/lib/dumpman/executor.rb +20 -2
- data/lib/dumpman/fetcher.rb +11 -12
- data/lib/dumpman/fetchers/direct.rb +46 -0
- data/lib/dumpman/fetchers/docker.rb +52 -0
- data/lib/dumpman/version.rb +1 -1
- data/lib/generators/dumpman/templates/dumpman.rb +14 -1
- data/lib/tasks/db.rake +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4062be5d02a9d24bdec99a061b1f1deaed5e4ee3059ccf6adbdf88c9ab168b39
|
4
|
+
data.tar.gz: 06634d8fc978f0bae92a4b0707966f2402ccb545c648cb89a4111ea977ffadd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f52bbffb820dca550dce9d7dd47ac7019395994021a77cd860fb1d1d457a7ed7fda8bd87a321add31a8e88128f442f53664acc0498f1b2ab211d1aa2e87ad66a
|
7
|
+
data.tar.gz: eedf62ad6ea423c2c99484fb74dade067ee4be6f7030c9b2e3353c000c21e03a17c0598e94d09ffb78d1168f5695cb3a2e977c95df4ae4c84a76e07868cb3f6f
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
[![Test Coverage](https://api.codeclimate.com/v1/badges/3f69b1bb862be2a7e6ce/test_coverage)](https://codeclimate.com/github/skcc321/dumpman/test_coverage)
|
5
5
|
|
6
6
|
# Dumpman
|
7
|
-
Dumpman gem is what you need if you have application somewhere and need to dump your DB download it and
|
7
|
+
Dumpman gem is what you need if you have application somewhere and need to dump your DB, download it and restore that dump localy!
|
8
8
|
|
9
9
|
### use case:
|
10
10
|
You have staging OR production server with running rails app on it.
|
@@ -12,7 +12,7 @@ You have staging OR production server with running rails app on it.
|
|
12
12
|
For some reason you need DB dump from that server.
|
13
13
|
|
14
14
|
What you can do?
|
15
|
-
- Solution 1: connect to that server, make dump via CLI, download it, extract it on your
|
15
|
+
- Solution 1: connect to that server, make dump via CLI, download it, extract it on your local machine
|
16
16
|
- Solution 2: install this gem and run ```bash rake db:prod:up``` and that's it.
|
17
17
|
|
18
18
|
![alt text](https://farm8.staticflickr.com/7347/10602023975_fd3e5b61b7_b.jpg)
|
@@ -55,6 +55,20 @@ Dumpman.setup do
|
|
55
55
|
# example:
|
56
56
|
# ssh_opts '-i ~/.ssh/sertificate.pem'
|
57
57
|
|
58
|
+
# fetch strategy
|
59
|
+
# if you are using capistrano or other deployment methods
|
60
|
+
# where you have direct access to filesystem where the application code is located
|
61
|
+
# you should use :direct strategy
|
62
|
+
fetch_strategy :direct
|
63
|
+
# if your application is running under the docker you can use :docker strategy
|
64
|
+
|
65
|
+
# if you selected :docker as fetch_strategy
|
66
|
+
# you have to set docker_image as well
|
67
|
+
# docker_image "645843940509.dkr.ecr.us-east-1.amazonaws.com/oh-snowflake"
|
68
|
+
|
69
|
+
# if you selected :direct as fetch strategy
|
70
|
+
# please set
|
71
|
+
|
58
72
|
# app path on the remote server
|
59
73
|
app_path '~/application/current'
|
60
74
|
end
|
@@ -70,7 +84,11 @@ Now you are able to use awesome commands:
|
|
70
84
|
$ rake db:prod:up # makes db dump on that server, compreses it, downloads, extracts localy
|
71
85
|
$ rake db:stage:up
|
72
86
|
$ rake db:qa:up
|
73
|
-
|
87
|
+
|
88
|
+
as well as local dump&restor
|
89
|
+
|
90
|
+
$ rake db:dump
|
91
|
+
$ rake db:restore
|
74
92
|
|
75
93
|
## Contributing
|
76
94
|
|
data/lib/dumpman.rb
CHANGED
@@ -5,6 +5,8 @@ require 'dumpman/adapters/pg'
|
|
5
5
|
require 'dumpman/adapters/mysql'
|
6
6
|
require 'dumpman/comandor'
|
7
7
|
require 'dumpman/executor'
|
8
|
+
require 'dumpman/fetchers/direct'
|
9
|
+
require 'dumpman/fetchers/docker'
|
8
10
|
require 'dumpman/fetcher'
|
9
11
|
require 'dumpman/railtie'
|
10
12
|
require 'dumpman/version'
|
@@ -46,15 +46,15 @@ module Dumpman
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def username
|
49
|
-
@username ||= db_config.fetch(:username)
|
49
|
+
@username ||= db_config.fetch(:username) { db_config.fetch(:user) }
|
50
50
|
end
|
51
51
|
|
52
52
|
def password
|
53
|
-
@password ||= db_config.fetch(:password)
|
53
|
+
@password ||= db_config.fetch(:password) { raise("you should set password in 'database.yml'") }
|
54
54
|
end
|
55
55
|
|
56
56
|
def host
|
57
|
-
@host ||= db_config.fetch(:host)
|
57
|
+
@host ||= db_config.fetch(:host) { 'localhost' }
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
data/lib/dumpman/connection.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Dumpman
|
2
2
|
class Connection
|
3
|
-
attr_accessor :name, :app_env, :ssh_cmd, :app_path, :ssh_opts
|
3
|
+
attr_accessor :name, :app_env, :ssh_cmd, :app_path, :ssh_opts, :fetch_strategy, :docker_image
|
4
4
|
|
5
5
|
def initialize(name)
|
6
6
|
@name = name
|
@@ -12,6 +12,8 @@ module Dumpman
|
|
12
12
|
ssh_cmd: @ssh_cmd,
|
13
13
|
ssh_opts: @ssh_opts,
|
14
14
|
app_path: @app_path,
|
15
|
+
fetch_strategy: @fetch_strategy,
|
16
|
+
docker_image: @docker_image,
|
15
17
|
}
|
16
18
|
end
|
17
19
|
|
@@ -30,5 +32,13 @@ module Dumpman
|
|
30
32
|
def ssh_opts(val)
|
31
33
|
self.ssh_opts = val
|
32
34
|
end
|
35
|
+
|
36
|
+
def fetch_strategy(val)
|
37
|
+
self.fetch_strategy = val
|
38
|
+
end
|
39
|
+
|
40
|
+
def docker_image(val)
|
41
|
+
self.docker_image = val
|
42
|
+
end
|
33
43
|
end
|
34
44
|
end
|
data/lib/dumpman/executor.rb
CHANGED
@@ -4,8 +4,17 @@ module Dumpman
|
|
4
4
|
|
5
5
|
def system(*commands)
|
6
6
|
cmd = commands.join(' && ')
|
7
|
-
|
8
|
-
|
7
|
+
info(cmd)
|
8
|
+
|
9
|
+
# execute & capture the result
|
10
|
+
if block_given?
|
11
|
+
# if success yield the result message
|
12
|
+
result = %x[#{cmd}].strip
|
13
|
+
yield result if $?.success?
|
14
|
+
else
|
15
|
+
# return execution t/f
|
16
|
+
result = Kernel.system(cmd)
|
17
|
+
end
|
9
18
|
end
|
10
19
|
|
11
20
|
def rake(*commands)
|
@@ -14,5 +23,14 @@ module Dumpman
|
|
14
23
|
Rake::Task["db:#{command}"].invoke
|
15
24
|
end
|
16
25
|
end
|
26
|
+
|
27
|
+
def info(cmd)
|
28
|
+
puts 'EXECUTING:'
|
29
|
+
|
30
|
+
puts cmd.gsub(/(\s{2})/, '\1')
|
31
|
+
.split(/\n/)
|
32
|
+
.map { |x| x.squeeze(' ') }
|
33
|
+
.reject(&:blank?)
|
34
|
+
end
|
17
35
|
end
|
18
36
|
end
|
data/lib/dumpman/fetcher.rb
CHANGED
@@ -5,25 +5,24 @@ module Dumpman
|
|
5
5
|
connection.name == connection_name
|
6
6
|
end
|
7
7
|
|
8
|
-
instance = self.new(connection.attrs)
|
8
|
+
instance = self.new(attrs: connection.attrs)
|
9
9
|
instance.fetch_remote_dump
|
10
10
|
end
|
11
11
|
|
12
12
|
def fetch_remote_dump
|
13
|
-
|
14
|
-
|
15
|
-
fetch_dump_to_local
|
16
|
-
)
|
13
|
+
fetcher = fetcher_class.new(attrs)
|
14
|
+
fetcher.get_dump
|
17
15
|
end
|
18
16
|
|
19
17
|
private
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
19
|
+
def fetcher_class
|
20
|
+
case attrs[:fetch_strategy]
|
21
|
+
when :docker then Dumpman::Fetchers::Docker
|
22
|
+
when :direct then Dumpman::Fetchers::Direct
|
23
|
+
else
|
24
|
+
Dumpman::Fetchers::Direct
|
25
|
+
end
|
26
|
+
end
|
28
27
|
end
|
29
28
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Dumpman
|
2
|
+
module Fetchers
|
3
|
+
# it triggers rake db:dump
|
4
|
+
# inside app_path
|
5
|
+
# and copies dump into tmp directory
|
6
|
+
# after that it gets dump from remote server
|
7
|
+
class Direct
|
8
|
+
attr_reader :ssh_opts, :ssh_cmd, :app_path, :app_env
|
9
|
+
|
10
|
+
def initialize(ssh_opts:, ssh_cmd:, app_path:, app_env:, **)
|
11
|
+
@ssh_opts = ssh_opts
|
12
|
+
@ssh_cmd = ssh_cmd
|
13
|
+
@app_path = app_path
|
14
|
+
@app_env = app_env
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_dump
|
18
|
+
Dumpman::Executor.system(make_dump_remotely) do |dump_location|
|
19
|
+
Dumpman::Executor.system(fetch_dump_to_local(dump_location))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def make_dump_remotely
|
26
|
+
<<~SSH_COMMAND
|
27
|
+
ssh #{ssh_opts} #{ssh_cmd} '\
|
28
|
+
export TEMP_DIR=$(mktemp -d)\
|
29
|
+
|
30
|
+
cd #{app_path} && \
|
31
|
+
bash --login -c "RAILS_ENV=#{app_env} bundle exec rake db:dump \
|
32
|
+
&& cp #{Dumpman.dump_file_name} $TEMP_DIR/"\
|
33
|
+
|
34
|
+
echo $TEMP_DIR'
|
35
|
+
SSH_COMMAND
|
36
|
+
end
|
37
|
+
|
38
|
+
def fetch_dump_to_local(dump_location)
|
39
|
+
<<~SSH_COMMAND
|
40
|
+
scp #{ssh_opts} #{ssh_cmd}:#{dump_location}/#{Dumpman.dump_file_name} \
|
41
|
+
#{Dumpman.dump_folder}/
|
42
|
+
SSH_COMMAND
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Dumpman
|
2
|
+
module Fetchers
|
3
|
+
# it runs docker task to make dump
|
4
|
+
# copies dump into tmp dir
|
5
|
+
# and retrieves dump to local machine
|
6
|
+
class Docker
|
7
|
+
attr_reader :ssh_opts, :ssh_cmd, :docker_image, :app_env
|
8
|
+
|
9
|
+
def initialize(ssh_opts:, ssh_cmd:, docker_image:, app_env:, **)
|
10
|
+
@ssh_opts = ssh_opts
|
11
|
+
@ssh_cmd = ssh_cmd
|
12
|
+
@docker_image = docker_image
|
13
|
+
@app_env = app_env
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_dump
|
17
|
+
Dumpman::Executor.system(make_dump_remotely) do |dump_location|
|
18
|
+
Dumpman::Executor.system(fetch_dump_to_local(dump_location))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def make_dump_remotely
|
23
|
+
<<~SSH_COMMAND
|
24
|
+
ssh #{ssh_opts} #{ssh_cmd} '
|
25
|
+
export TEMP_DIR=$(mktemp -d)
|
26
|
+
export DOCKER_IMAGE=$(docker images #{docker_image} --format "{{.ID}}" | head -1)
|
27
|
+
|
28
|
+
docker run -d \
|
29
|
+
--name pgdmp \
|
30
|
+
--rm \
|
31
|
+
-e RAILS_ENV=#{app_env} \
|
32
|
+
-v ${TEMP_DIR}:/opt \
|
33
|
+
-u root \
|
34
|
+
${DOCKER_IMAGE} /bin/bash -c \
|
35
|
+
"bundle exec rake db:dump && cp #{Dumpman.dump_file_name} /opt/ && exit" > /dev/null
|
36
|
+
|
37
|
+
docker wait pgdmp >/dev/null
|
38
|
+
|
39
|
+
echo $TEMP_DIR
|
40
|
+
'
|
41
|
+
SSH_COMMAND
|
42
|
+
end
|
43
|
+
|
44
|
+
def fetch_dump_to_local(dump_location)
|
45
|
+
<<~SSH_COMMAND
|
46
|
+
scp #{ssh_opts} #{ssh_cmd}:#{dump_location}/#{Dumpman.dump_file_name} \
|
47
|
+
#{Dumpman.dump_folder}/
|
48
|
+
SSH_COMMAND
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/dumpman/version.rb
CHANGED
@@ -12,8 +12,21 @@ Dumpman.setup do
|
|
12
12
|
|
13
13
|
# ssh options for connection to the remote server
|
14
14
|
# example:
|
15
|
-
|
15
|
+
ssh_opts '-i ~/.ssh/sertificate.pem'
|
16
16
|
|
17
|
+
# fetch strategy
|
18
|
+
# if you are using capistrano or other deployment methods
|
19
|
+
# where you have direct access to filesystem where the application code is located
|
20
|
+
# you should use :direct strategy
|
21
|
+
fetch_strategy :direct
|
22
|
+
# if your application is running under the docker you can use :docker strategy
|
23
|
+
|
24
|
+
# if you selected :docker as fetch_strategy
|
25
|
+
# you have to set docker_image as well
|
26
|
+
# docker_image "645843940509.dkr.ecr.us-east-1.amazonaws.com/oh-snowflake"
|
27
|
+
|
28
|
+
# if you selected :direct as fetch strategy
|
29
|
+
# please set
|
17
30
|
# app path on the remote server
|
18
31
|
app_path '~/application/current'
|
19
32
|
end
|
data/lib/tasks/db.rake
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dumpman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rafael
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -306,6 +306,8 @@ files:
|
|
306
306
|
- lib/dumpman/database.rb
|
307
307
|
- lib/dumpman/executor.rb
|
308
308
|
- lib/dumpman/fetcher.rb
|
309
|
+
- lib/dumpman/fetchers/direct.rb
|
310
|
+
- lib/dumpman/fetchers/docker.rb
|
309
311
|
- lib/dumpman/railtie.rb
|
310
312
|
- lib/dumpman/version.rb
|
311
313
|
- lib/generators/dumpman/dumpman_generator.rb
|