mongo-db-utils 0.0.2 → 0.0.3
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.
- data/README.md +6 -9
- data/images/sample.png +0 -0
- data/lib/mongo-db-utils/cmd/mongotools.rb +22 -0
- data/lib/mongo-db-utils/cmd.rb +74 -18
- data/lib/mongo-db-utils/console.rb +8 -5
- data/lib/mongo-db-utils/models.rb +8 -0
- data/lib/mongo-db-utils/version.rb +2 -1
- data/spec/mongo_db_utils_spec.rb +3 -0
- metadata +3 -2
data/README.md
CHANGED
@@ -1,17 +1,21 @@
|
|
1
1
|
# MongoDbUtils
|
2
2
|
|
3
|
+
## Warning - not safe for production use - undergoing development!
|
4
|
+
|
3
5
|
A little gem that simplifies backing up and copying your mongo dbs.
|
4
6
|
|
7
|
+

|
8
|
+
|
5
9
|
It saves your database urls so any task is just a few clicks.
|
6
10
|
|
7
11
|
* backup a database
|
8
|
-
* copy a database from one server to another (whilst backing up the target db if it
|
12
|
+
* copy a database from one server to another (whilst backing up the target db if it exists)
|
9
13
|
|
10
14
|
|
11
15
|
|
12
16
|
## Installation
|
13
17
|
|
14
|
-
You need to have mongodump on your path.
|
18
|
+
You need to have *mongodump* and *mongorestore* on your path.
|
15
19
|
|
16
20
|
gem install 'mongo-db-utils'
|
17
21
|
|
@@ -33,10 +37,3 @@ When it does backups it stores them in ````~/.mongo-db-utils/backups/````. The n
|
|
33
37
|
rake build
|
34
38
|
gem install pkg/mongo-db-utils.gem
|
35
39
|
|
36
|
-
## Contributing
|
37
|
-
|
38
|
-
1. Fork it
|
39
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
41
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
-
5. Create new Pull Request
|
data/images/sample.png
ADDED
Binary file
|
@@ -35,6 +35,28 @@ module MongoDbUtils
|
|
35
35
|
`#{cmd}`
|
36
36
|
|
37
37
|
end
|
38
|
+
|
39
|
+
|
40
|
+
def self.restore(host,port,db,source_folder,username = "", password = "")
|
41
|
+
|
42
|
+
options = []
|
43
|
+
options << Option.new("-h", "#{host}:#{port}")
|
44
|
+
options << Option.new("-db", db)
|
45
|
+
options << Option.new("-u", username)
|
46
|
+
options << Option.new("-p", password)
|
47
|
+
|
48
|
+
cmd = "mongorestore "
|
49
|
+
|
50
|
+
options.each do |o|
|
51
|
+
cmd << "#{o.key} #{o.value} " unless o.empty?
|
52
|
+
end
|
53
|
+
cmd << "#{source_folder}"
|
54
|
+
puts "cmd:"
|
55
|
+
puts cmd
|
56
|
+
`#{cmd}`
|
57
|
+
|
58
|
+
end
|
59
|
+
|
38
60
|
end
|
39
61
|
end
|
40
62
|
end
|
data/lib/mongo-db-utils/cmd.rb
CHANGED
@@ -5,58 +5,114 @@ require 'mongo/connection'
|
|
5
5
|
module MongoDbUtils
|
6
6
|
class Cmd
|
7
7
|
|
8
|
-
def self.backup(db,
|
9
|
-
|
8
|
+
def self.backup(db, folder, final_path = nil, tar_it = true)
|
9
|
+
puts "--"
|
10
|
+
puts ">> Backing up: #{db}, #{folder}, #{final_path}"
|
10
11
|
unless( db_exists?(db) )
|
11
12
|
return false
|
12
13
|
end
|
13
14
|
|
14
15
|
t = Time.new
|
15
16
|
timestamp = t.strftime("%Y.%m.%d__%H.%M")
|
16
|
-
|
17
|
+
|
18
|
+
if( final_path.nil? )
|
19
|
+
out_path = "#{folder}/#{db.host}_#{db.port}/#{db.name}/#{timestamp}"
|
20
|
+
else
|
21
|
+
puts "final path not nil out_path: #{out_path}"
|
22
|
+
end
|
23
|
+
|
17
24
|
full_path = File.expand_path(out_path)
|
18
25
|
|
26
|
+
puts ">> final backup path: #{full_path}"
|
27
|
+
|
19
28
|
FileUtils.mkdir_p(full_path)
|
20
|
-
MongoDbUtils::Commands::MongoTools.dump(
|
29
|
+
MongoDbUtils::Commands::MongoTools.dump(
|
30
|
+
db.host,
|
31
|
+
db.port,
|
32
|
+
db.name,
|
33
|
+
full_path,
|
34
|
+
db.username,
|
35
|
+
db.password)
|
36
|
+
|
37
|
+
if( tar_it )
|
38
|
+
Dir.chdir(full_path)
|
39
|
+
`tar cvf #{db.name}.tar #{db.name}`
|
40
|
+
`rm -fr #{full_path}/#{db.name}`
|
41
|
+
end
|
21
42
|
|
22
|
-
`tar cvf #{full_path}/#{db.name}.tar.gz #{full_path}/#{db.name}`
|
23
|
-
`rm -fr #{full_path}/#{db.name}`
|
24
43
|
true
|
25
44
|
end
|
26
45
|
|
46
|
+
# With remote dbs you can't do a copy_database if you're not an admin.
|
47
|
+
# so using restore instead
|
48
|
+
# connection = Mongo::Connection.from_uri(destination.to_s)
|
49
|
+
# mongo_db = connection[destination.name]
|
50
|
+
# if( destination.authentication_required? )
|
51
|
+
# login_result = mongo_db.authenticate(destination.username, destination.password)
|
52
|
+
# end
|
53
|
+
# host = "#{source.host}:#{source.port}"
|
54
|
+
# connection.copy_database(source.name, destination.name, host, source.username, source.password)
|
55
|
+
#
|
56
|
+
#
|
27
57
|
def self.copy(path, source, destination, halt_on_no_backup = true)
|
28
58
|
|
29
59
|
backup_made = self.backup(destination, path)
|
30
60
|
|
31
|
-
|
32
61
|
if( !backup_made && halt_on_no_backup)
|
33
62
|
puts "aborting - no backup was made"
|
34
63
|
return
|
35
64
|
end
|
36
65
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
66
|
+
tmp_path = "~/.mongo-db-utils/tmp"
|
67
|
+
|
68
|
+
FileUtils.mkdir_p(tmp_path)
|
69
|
+
|
70
|
+
puts "backup to: #{tmp_path}/#{source.name}"
|
71
|
+
backup(source,tmp_path, source.name, false)
|
72
|
+
|
73
|
+
MongoDbUtils::Commands::MongoTools.restore(
|
74
|
+
destination.host,
|
75
|
+
destination.port,
|
76
|
+
destination.name,
|
77
|
+
"#{tmp_path}/#{source.name}/#{source.name}",
|
78
|
+
destination.username,
|
79
|
+
destination.password)
|
80
|
+
|
81
|
+
`rm -fr #{tmp_path}`
|
43
82
|
end
|
44
83
|
|
45
84
|
|
46
85
|
private
|
47
86
|
def self.db_exists?(db)
|
87
|
+
|
88
|
+
puts "DB exists? #{db.to_s}"
|
89
|
+
|
48
90
|
connection = Mongo::Connection.from_uri(db.to_s)
|
49
|
-
|
91
|
+
mongo_db = connection[db.name]
|
92
|
+
|
93
|
+
exists = !mongo_db.nil?
|
94
|
+
|
95
|
+
puts "mongo_db: #{mongo_db}"
|
96
|
+
if( db.authentication_required? && exists )
|
97
|
+
login_result = mongo_db.authenticate(db.username, db.password)
|
98
|
+
exists = !login_result.nil?
|
99
|
+
else
|
100
|
+
end
|
50
101
|
connection.close
|
51
|
-
|
102
|
+
exists
|
52
103
|
end
|
53
104
|
|
54
105
|
def self.remove_db(db)
|
55
|
-
|
56
|
-
|
106
|
+
if( db_exists?(db))
|
107
|
+
connection = Mongo::Connection.from_uri(db.to_s)
|
108
|
+
if( db.authentication_required? )
|
109
|
+
mongo_db = connection[db.name]
|
110
|
+
mongo_db.authenticate(db.username, db.password)
|
111
|
+
end
|
112
|
+
|
57
113
|
connection.drop_database(db.name)
|
114
|
+
connection.close
|
58
115
|
end
|
59
|
-
connection.close
|
60
116
|
end
|
61
117
|
end
|
62
118
|
end
|
@@ -10,6 +10,7 @@ module MongoDbUtils
|
|
10
10
|
Mongo Db Utils - Version: #{MongoDbUtils::VERSION}
|
11
11
|
===================================
|
12
12
|
eos
|
13
|
+
|
13
14
|
|
14
15
|
def initialize(config, cmd)
|
15
16
|
@config = config
|
@@ -18,6 +19,7 @@ eos
|
|
18
19
|
|
19
20
|
def run
|
20
21
|
say(HEADER)
|
22
|
+
say(MongoDbUtils::READY_FOR_USE)
|
21
23
|
main_menu
|
22
24
|
end
|
23
25
|
|
@@ -77,7 +79,8 @@ eos
|
|
77
79
|
def add_config
|
78
80
|
entry = Hash.new
|
79
81
|
entry[:mongo_uri] = ask("Mongo uri (eg: 'mongodb://user:pass@locahost:27017/db')")
|
80
|
-
|
82
|
+
new_uri = entry[:mongo_uri].gsub(" ", "")
|
83
|
+
successful = @config.add_db_from_uri(new_uri)
|
81
84
|
|
82
85
|
if successful
|
83
86
|
say("added server")
|
@@ -99,7 +102,7 @@ eos
|
|
99
102
|
choose do |menu|
|
100
103
|
prep_menu(menu)
|
101
104
|
@config.dbs.each do |db|
|
102
|
-
menu.choice "#{db.
|
105
|
+
menu.choice "#{db.to_s}" do backup(db) end
|
103
106
|
end
|
104
107
|
menu.choice "back" do main_menu end
|
105
108
|
end
|
@@ -114,7 +117,7 @@ eos
|
|
114
117
|
choose do |menu|
|
115
118
|
prep_menu(menu)
|
116
119
|
@config.dbs.sort.each do |db|
|
117
|
-
menu.choice "#{db.
|
120
|
+
menu.choice "#{db.to_s}" do
|
118
121
|
copy_plan[:source] = db
|
119
122
|
end
|
120
123
|
|
@@ -130,7 +133,7 @@ eos
|
|
130
133
|
choose do |menu|
|
131
134
|
prep_menu(menu)
|
132
135
|
@config.dbs.sort.each do |db|
|
133
|
-
menu.choice "#{db.
|
136
|
+
menu.choice "#{db.to_s}" do
|
134
137
|
copy_plan[:destination] = db
|
135
138
|
end unless db == copy_plan[:source]
|
136
139
|
end
|
@@ -141,7 +144,7 @@ eos
|
|
141
144
|
|
142
145
|
def show_copy_plan(plan)
|
143
146
|
say("Copy: (we'll backup the destination before we copy)")
|
144
|
-
say("#{plan[:source].
|
147
|
+
say("#{plan[:source].to_s} --> #{plan[:destination].to_s}")
|
145
148
|
|
146
149
|
choose do |menu|
|
147
150
|
prep_menu(menu)
|
data/spec/mongo_db_utils_spec.rb
CHANGED
@@ -13,6 +13,8 @@ describe MongoDbUtils::Model do
|
|
13
13
|
db.name.should eql("ed-backup")
|
14
14
|
db.username.should eql("")
|
15
15
|
|
16
|
+
db.authentication_required?.should eql(false)
|
17
|
+
|
16
18
|
end
|
17
19
|
|
18
20
|
it "should parse mongo uris" do
|
@@ -25,6 +27,7 @@ describe MongoDbUtils::Model do
|
|
25
27
|
db.name.should eql("ed-backup")
|
26
28
|
db.username.should eql("ed")
|
27
29
|
db.password.should eql("password")
|
30
|
+
db.authentication_required?.should eql(true)
|
28
31
|
|
29
32
|
end
|
30
33
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo-db-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -156,6 +156,7 @@ files:
|
|
156
156
|
- bin/mongo-db-utils
|
157
157
|
- features/mongo-db-utils.feature
|
158
158
|
- features/support/setup.rb
|
159
|
+
- images/sample.png
|
159
160
|
- install_rdebug.sh
|
160
161
|
- lib/mongo-db-utils/cli.rb
|
161
162
|
- lib/mongo-db-utils/cmd.rb
|