mongo-db-utils 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/mongo-db-utils/cmd.rb +6 -4
- data/lib/mongo-db-utils/models/db.rb +47 -0
- data/lib/mongo-db-utils/tools/commands.rb +64 -31
- data/lib/mongo-db-utils/version.rb +1 -1
- data/spec/models/db_spec.rb +26 -0
- data/spec/mongo_tools_cmd_spec.rb +54 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ebe472c1c71790c06618838dac17a6da13db206
|
4
|
+
data.tar.gz: e8a5fa1960f3a5c988818fdab24657f167adeaf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 825c9dd5b70b4729afaea862888d5cd308c36ed9edf004dd60aa5e66e4b8f31d17fc3b1cffcfe2dc4761151dd60aa770208c104c0cd53996fea9c25d24338dd5
|
7
|
+
data.tar.gz: ac3a00ea228736521b54495d38ad0bae15b1bda24993cf93bc94c08d8fc44e8ae0c7d9d65030e77d44de3f08095a88598df0d9766deabceb2851e9cc1e016878
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[](https://travis-ci.org/edeustace/mongo-db-utils)
|
4
4
|
|
5
5
|
|
6
|
-
### !Current version 0.
|
6
|
+
### !Current version 0.1.1 is in Beta - for a safer version use 0.0.9
|
7
7
|
|
8
8
|
A little gem that simplifies backing up and copying your mongo dbs.
|
9
9
|
|
@@ -55,6 +55,12 @@ When it does backups it stores them in ````~/.mongo-db-utils/backups/````. The n
|
|
55
55
|
|
56
56
|
|
57
57
|
## Release Notes
|
58
|
+
* 0.1.2 - BETA
|
59
|
+
- Added 'host' and 'port' getters to Db AND 'hosts' getter to ReplicaSetDb
|
60
|
+
|
61
|
+
* 0.1.1 - BETA
|
62
|
+
- Tidy up Tools - add Import and Restore to tool set
|
63
|
+
|
58
64
|
* 0.1.0 - BETA
|
59
65
|
- Fixed CLI backup and backup_s3 not using the config-loader correctly.
|
60
66
|
|
data/lib/mongo-db-utils/cmd.rb
CHANGED
@@ -4,6 +4,8 @@ require 'mongo'
|
|
4
4
|
module MongoDbUtils
|
5
5
|
class Cmd
|
6
6
|
|
7
|
+
include MongoDbUtils::Tools
|
8
|
+
|
7
9
|
def self.backup(db, folder, final_path = nil, tar_it = true)
|
8
10
|
puts ">> Backing up: #{db}, #{folder}, #{final_path}"
|
9
11
|
unless( db_exists?(db) )
|
@@ -21,12 +23,12 @@ module MongoDbUtils
|
|
21
23
|
puts ">> final backup path: #{full_path}"
|
22
24
|
|
23
25
|
FileUtils.mkdir_p(full_path)
|
24
|
-
|
26
|
+
Dump.new(
|
25
27
|
db.to_host_s,
|
26
28
|
db.name,
|
27
29
|
full_path,
|
28
30
|
db.username,
|
29
|
-
db.password)
|
31
|
+
db.password).run
|
30
32
|
|
31
33
|
if( tar_it )
|
32
34
|
Dir.chdir(full_path)
|
@@ -66,12 +68,12 @@ module MongoDbUtils
|
|
66
68
|
password = ""
|
67
69
|
end
|
68
70
|
|
69
|
-
|
71
|
+
Restore.new(
|
70
72
|
destination.to_host_s,
|
71
73
|
destination.name,
|
72
74
|
"#{tmp_dump_path}",
|
73
75
|
username,
|
74
|
-
password)
|
76
|
+
password).run
|
75
77
|
|
76
78
|
`rm -fr #{tmp_path}`
|
77
79
|
end
|
@@ -1,6 +1,23 @@
|
|
1
1
|
module MongoDbUtils
|
2
2
|
module Model
|
3
3
|
|
4
|
+
|
5
|
+
# This method accepts 2 possible uri formats
|
6
|
+
# 1. the conventional mongo_uri format: mongodb://xxxxxxxx
|
7
|
+
# 2. the non standard way of representing a replicaset uri:
|
8
|
+
# --> replica_set|mongo_uri
|
9
|
+
# --> Eg: my-set|mongodb://xxxxxxx
|
10
|
+
# This is useful because many mongo commands require the set name
|
11
|
+
# when invoking them and this bundles the 2 things together
|
12
|
+
def self.db_from_uri(uri)
|
13
|
+
if(uri.include? "|")
|
14
|
+
split = uri.split("|")
|
15
|
+
ReplicaSetDb.new(split[1], split[0])
|
16
|
+
else
|
17
|
+
Db.new(uri)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
4
21
|
# A Db stored in the config
|
5
22
|
class Db
|
6
23
|
|
@@ -33,6 +50,14 @@ module MongoDbUtils
|
|
33
50
|
has?(self.username) && has?(self.password)
|
34
51
|
end
|
35
52
|
|
53
|
+
def host
|
54
|
+
host_and_port[:host]
|
55
|
+
end
|
56
|
+
|
57
|
+
def port
|
58
|
+
host_and_port[:port]
|
59
|
+
end
|
60
|
+
|
36
61
|
# Return the host string in a format that is compatable with mongo binary tools
|
37
62
|
# See: http://docs.mongodb.org/manual/reference/program/mongodump/#cmdoption-mongodump--host
|
38
63
|
def to_host_s
|
@@ -55,6 +80,12 @@ module MongoDbUtils
|
|
55
80
|
def has?(s)
|
56
81
|
!s.nil? && !s.empty?
|
57
82
|
end
|
83
|
+
|
84
|
+
|
85
|
+
def host_and_port
|
86
|
+
match, host,port = *@host_port.match(/(.*):(.*)/)
|
87
|
+
{ :host => host, :port => port }
|
88
|
+
end
|
58
89
|
end
|
59
90
|
|
60
91
|
|
@@ -66,6 +97,22 @@ module MongoDbUtils
|
|
66
97
|
@set_name = name
|
67
98
|
end
|
68
99
|
|
100
|
+
|
101
|
+
# Return an array of host:port strings
|
102
|
+
def hosts
|
103
|
+
@host_port.split(",")
|
104
|
+
end
|
105
|
+
|
106
|
+
# Block usage of this method from the super
|
107
|
+
def host
|
108
|
+
raise "'host' is not a valid method for a ReplicaSetDb - use 'hosts' instead."
|
109
|
+
end
|
110
|
+
|
111
|
+
# Block usage of this method from the super
|
112
|
+
def port
|
113
|
+
raise "'port' is not a valid method for a ReplicaSetDb - use 'hosts' instead."
|
114
|
+
end
|
115
|
+
|
69
116
|
# Note: we override this to provide a replica set format
|
70
117
|
def to_host_s
|
71
118
|
"#{@set_name}/#{@host_port}"
|
@@ -13,13 +13,30 @@ module MongoDbUtils
|
|
13
13
|
end
|
14
14
|
|
15
15
|
class BaseCmd
|
16
|
+
|
17
|
+
def initialize(cmd_name, host_and_port, db, username = "", password = "")
|
18
|
+
@options = build_base_options(host_and_port, db, username, password)
|
19
|
+
@cmd_name = cmd_name
|
20
|
+
end
|
21
|
+
|
22
|
+
def run
|
23
|
+
puts "[#{self.class}] run: #{cmd}"
|
24
|
+
output = `#{cmd}`
|
25
|
+
raise ToolsException.new("#{cmd}", output) unless $?.to_i == 0
|
26
|
+
end
|
27
|
+
|
28
|
+
def cmd
|
29
|
+
"#{@cmd_name} #{options_string(@options)}"
|
30
|
+
end
|
31
|
+
|
16
32
|
private
|
17
|
-
|
33
|
+
|
34
|
+
def o(key,value)
|
18
35
|
Option.new(key,value)
|
19
36
|
end
|
20
37
|
|
21
38
|
# options common to all commands
|
22
|
-
def
|
39
|
+
def build_base_options(host_and_port,db,username="",password="")
|
23
40
|
options = []
|
24
41
|
options << o("-h", host_and_port)
|
25
42
|
options << o("-db", db)
|
@@ -29,57 +46,73 @@ module MongoDbUtils
|
|
29
46
|
end
|
30
47
|
|
31
48
|
# given an array of options build a string of those options unless the option is empty
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
out << "#{o.key} #{o.value} " unless o.empty?
|
36
|
-
end
|
37
|
-
out.strip
|
49
|
+
def options_string(opts)
|
50
|
+
opt_strings = opts.reject{ |o| o.empty? }.map { |o| o.to_s }
|
51
|
+
opt_strings.join(" ").strip
|
38
52
|
end
|
39
53
|
end
|
40
54
|
|
41
55
|
class Dump < BaseCmd
|
42
|
-
|
43
|
-
|
44
|
-
options
|
45
|
-
options << o("-o", output)
|
46
|
-
"mongodump #{options_string(options)}"
|
47
|
-
end
|
48
|
-
|
49
|
-
# run the command
|
50
|
-
def self.run(host_and_port,db,output,username="", password ="")
|
51
|
-
cmd_string = self.cmd(host_and_port,db,output,username,password)
|
52
|
-
puts "[Dump] run: #{cmd_string}"
|
53
|
-
output = `#{cmd_string}`
|
54
|
-
raise ToolsException.new("#{cmd_string}", output) unless $?.to_i == 0
|
56
|
+
def initialize(host_and_port,db,output,username = "", password = "")
|
57
|
+
super("mongodump", host_and_port, db, username, password)
|
58
|
+
@options << o("-o", output)
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
58
62
|
class Restore < BaseCmd
|
59
|
-
def
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
+
def initialize(host_and_port,db,source_folder,username = "", password = "")
|
64
|
+
super("mongorestore", host_and_port, db, username, password)
|
65
|
+
@options << "--drop"
|
66
|
+
@source_folder = source_folder
|
63
67
|
end
|
64
68
|
|
65
|
-
def
|
66
|
-
|
67
|
-
puts "[Restore] run: #{cmd_string}"
|
68
|
-
output = `#{cmd_string}`
|
69
|
-
raise ToolsException.new("#{cmd_string}", output) unless $?.to_i == 0
|
69
|
+
def cmd
|
70
|
+
"#{super} #{@source_folder}"
|
70
71
|
end
|
71
72
|
end
|
72
73
|
|
74
|
+
class Import < BaseCmd
|
75
|
+
def initialize(host_and_port, db, collection, file, username = "", password = "", opts = {})
|
76
|
+
super("mongoimport", host_and_port, db, username, password)
|
77
|
+
@options << o("-c", collection)
|
78
|
+
@options << o("--file", file)
|
79
|
+
@options << "--jsonArray" if opts[:json_array]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Export < BaseCmd
|
84
|
+
|
85
|
+
def initialize(host_and_port, db, collection, query, output, username = "", password = "", opts = {})
|
86
|
+
super("mongoexport", host_and_port, db, username, password)
|
87
|
+
@options << o("-c", collection)
|
88
|
+
@options << o("-o", output)
|
89
|
+
@options << o("--query", "'#{query}'")
|
90
|
+
@options << "--jsonArray" if opts[:json_array]
|
91
|
+
end
|
92
|
+
end
|
73
93
|
|
74
94
|
class Option
|
75
95
|
attr_accessor :key, :value
|
76
96
|
|
77
|
-
def initialize(key,value)
|
97
|
+
def initialize(key,value = nil)
|
78
98
|
@key = key
|
79
99
|
@value = value
|
80
100
|
end
|
81
101
|
|
82
102
|
def empty?
|
103
|
+
(@value.nil? || @value.empty?)
|
104
|
+
end
|
105
|
+
|
106
|
+
def to_s
|
107
|
+
if empty?
|
108
|
+
nil
|
109
|
+
else
|
110
|
+
"#{@key} #{@value}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
def value_empty?
|
83
116
|
@value.nil? || @value.empty?
|
84
117
|
end
|
85
118
|
end
|
data/spec/models/db_spec.rb
CHANGED
@@ -13,6 +13,8 @@ describe MongoDbUtils::Model::Db do
|
|
13
13
|
db.password.should == "pass"
|
14
14
|
db.uri == "mongodb://user:pass@localhost:27017/db"
|
15
15
|
db.authentication_required?.should == true
|
16
|
+
db.host.should == "localhost"
|
17
|
+
db.port.should == "27017"
|
16
18
|
end
|
17
19
|
|
18
20
|
it "should construct - no user/pass" do
|
@@ -23,6 +25,8 @@ describe MongoDbUtils::Model::Db do
|
|
23
25
|
db.password.should == ""
|
24
26
|
db.uri == "mongodb://localhost:27017/db"
|
25
27
|
db.authentication_required?.should == false
|
28
|
+
db.host.should == "localhost"
|
29
|
+
db.port.should == "27017"
|
26
30
|
end
|
27
31
|
|
28
32
|
it "should build a replicaset db" do
|
@@ -31,6 +35,28 @@ describe MongoDbUtils::Model::Db do
|
|
31
35
|
rs.to_host_s === "my-set/host:port,host2:port2"
|
32
36
|
rs.uri.should == "mongodb://user:pass@host:port,host2:port2/db"
|
33
37
|
rs.authentication_required?.should == true
|
38
|
+
|
39
|
+
expect{ rs.host }.to raise_error
|
40
|
+
expect{ rs.port }.to raise_error
|
41
|
+
|
42
|
+
rs.hosts.should == ["host:port", "host2:port2"]
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
it "should parse the uri correctly" do
|
48
|
+
MongoDbUtils::Model.db_from_uri("mongodb://localhost:27017/db").class.to_s.should == "MongoDbUtils::Model::Db"
|
49
|
+
MongoDbUtils::Model.db_from_uri("set|mongodb://s:2,s:3/db").class.to_s.should == "MongoDbUtils::Model::ReplicaSetDb"
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
it "should parse a full url" do
|
54
|
+
|
55
|
+
uri = "rs-ds063347|mongodb://user:pass@ds063347-a0.mongolab.com:63347,ds063347-a1.mongolab.com:63347/staging"
|
56
|
+
db = MongoDbUtils::Model.db_from_uri(uri)
|
57
|
+
db.username.should eql "user"
|
58
|
+
db.password.should == "pass"
|
59
|
+
db.to_host_s.should == "rs-ds063347/ds063347-a0.mongolab.com:63347,ds063347-a1.mongolab.com:63347"
|
34
60
|
end
|
35
61
|
|
36
62
|
end
|
@@ -1,38 +1,78 @@
|
|
1
1
|
require 'mongo-db-utils/tools/commands'
|
2
2
|
|
3
|
-
|
3
|
+
include MongoDbUtils::Tools
|
4
4
|
|
5
|
-
|
5
|
+
describe Dump do
|
6
6
|
|
7
7
|
it "should work with single uris" do
|
8
|
-
|
8
|
+
Dump.new("host:port", "db", "out", "user", "pass").cmd.should eql "mongodump -h host:port -db db -u user -p pass -o out"
|
9
9
|
end
|
10
|
-
|
11
10
|
it "should work with single uris - no user/pass" do
|
12
|
-
|
11
|
+
Dump.new("host:port", "db", "out").cmd.should eql "mongodump -h host:port -db db -o out"
|
13
12
|
end
|
14
13
|
|
15
14
|
it "should work with replica set uris" do
|
16
15
|
expected = "mongodump -h setname/host1:port1,host2:port2 -db db -u user -p pass -o out"
|
17
|
-
|
16
|
+
Dump.new("setname/host1:port1,host2:port2","db", "out", "user", "pass").cmd.should eql expected
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
21
|
-
|
22
|
-
describe MongoDbUtils::Tools::Restore do
|
23
|
-
|
24
|
-
restore = MongoDbUtils::Tools::Restore
|
20
|
+
describe Restore do
|
25
21
|
|
26
22
|
it "should work with single uris" do
|
27
|
-
|
23
|
+
Restore.new("host:port", "db", "source", "user", "pass").cmd.should eql "mongorestore -h host:port -db db -u user -p pass --drop source"
|
28
24
|
end
|
29
25
|
|
30
26
|
it "should work with single uris" do
|
31
|
-
|
27
|
+
Restore.new("host:port", "db", "source").cmd.should eql "mongorestore -h host:port -db db --drop source"
|
32
28
|
end
|
33
29
|
|
34
30
|
it "should work with replica set uris" do
|
35
31
|
expected = "mongorestore -h setname/host1:port1,host2:port2 -db db -u user -p pass --drop source"
|
36
|
-
|
32
|
+
Restore.new("setname/host1:port1,host2:port2","db", "source", "user", "pass").cmd.should eql expected
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe Import do
|
37
|
+
|
38
|
+
it "should work with single uris - with user/pass" do
|
39
|
+
Import.new("host:port", "db", "coll", "myfile.json", "user", "pass").cmd.should eql "mongoimport -h host:port -db db -u user -p pass -c coll --file myfile.json"
|
37
40
|
end
|
38
|
-
|
41
|
+
|
42
|
+
it "should work with single uris - no user/pass" do
|
43
|
+
Import.new("host:port", "db", "coll", "myfile.json").cmd.should eql "mongoimport -h host:port -db db -c coll --file myfile.json"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should work with replica set uris" do
|
47
|
+
expected = "mongoimport -h setname/host1:port1,host2:port2 -db db -u user -p pass -c coll --file myfile.json --jsonArray"
|
48
|
+
Import.new("setname/host1:port1,host2:port2","db", "coll", "myfile.json", "user", "pass", { :json_array => true} ).cmd.should eql expected
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe Export do
|
53
|
+
|
54
|
+
it "should work with single uris - with user/pass" do
|
55
|
+
Export.new("host:port", "db", "coll", "{query}", "myfile.json", "user", "pass").cmd.should eql "mongoexport -h host:port -db db -u user -p pass -c coll -o myfile.json --query '{query}'"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should work with single uris - no user/pass" do
|
59
|
+
Export.new("host:port", "db", "coll", "{query}", "myfile.json").cmd.should eql "mongoexport -h host:port -db db -c coll -o myfile.json --query '{query}'"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should work with replica set uris" do
|
63
|
+
expected = "mongoexport -h setname/host1:port1,host2:port2 -db db -u user -p pass -c coll -o myfile.json --query '{query}' --jsonArray"
|
64
|
+
Export.new("setname/host1:port1,host2:port2","db", "coll", "{query}", "myfile.json", "user", "pass", { :json_array => true} ).cmd.should eql expected
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe Option do
|
69
|
+
|
70
|
+
it "should return empty? correctly" do
|
71
|
+
Option.new("a", "b").empty?.should eql false
|
72
|
+
Option.new("a", "b").to_s.should eql "a b"
|
73
|
+
Option.new("a").empty?.should eql true
|
74
|
+
Option.new("a").to_s.should eql nil
|
75
|
+
Option.new("").empty?.should eql true
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo-db-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- edeustace
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|