mongo-db-utils 0.0.1
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/.gitignore +17 -0
- data/.mongo-db-utils/config.yml +2 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/bin/mongo-db-utils +3 -0
- data/features/mongo-db-utils.feature +9 -0
- data/features/support/setup.rb +1 -0
- data/install_rdebug.sh +18 -0
- data/lib/mongo-db-utils/cli.rb +16 -0
- data/lib/mongo-db-utils/cmd.rb +62 -0
- data/lib/mongo-db-utils/cmd/mongotools.rb +40 -0
- data/lib/mongo-db-utils/config-loader.rb +61 -0
- data/lib/mongo-db-utils/console.rb +175 -0
- data/lib/mongo-db-utils/models.rb +95 -0
- data/lib/mongo-db-utils/version.rb +3 -0
- data/mongo-db-utils.gemspec +26 -0
- data/spec/config-loader-spec.rb +21 -0
- data/spec/mongo_db_utils_spec.rb +57 -0
- metadata +198 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
|
+
|
|
3
|
+
# Specify your gem's dependencies in mongo-db-utils.gemspec
|
|
4
|
+
gemspec
|
|
5
|
+
#RUBY_PATCHLEVEL = "194"
|
|
6
|
+
#gem 'linecache19', '0.5.13', :path => "~/.rvm/gems/ruby-1.9.3-p#{RUBY_PATCHLEVEL}/gems/linecache19-0.5.13/"
|
|
7
|
+
#gem 'ruby-debug-base19', '0.11.26', :path => "~/.rvm/gems/ruby-1.9.3-p#{RUBY_PATCHLEVEL}/gems/ruby-debug-base19-0.11.26/"
|
|
8
|
+
#gem 'ruby-debug19', :require => 'ruby-debug'
|
|
9
|
+
|
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 edeustace
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# MongoDbUtils
|
|
2
|
+
|
|
3
|
+
A little gem that simplifies backing up and copying your mongo dbs.
|
|
4
|
+
|
|
5
|
+
It saves your database urls so any task is just a few clicks.
|
|
6
|
+
|
|
7
|
+
* backup a database
|
|
8
|
+
* copy a database from one server to another (whilst backing up the target db if it exits)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
You need to have mongodump on your path.
|
|
15
|
+
|
|
16
|
+
gem install 'mongo-db-utils'
|
|
17
|
+
|
|
18
|
+
And then execute:
|
|
19
|
+
|
|
20
|
+
$ mongo-db-utils console
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
Once you launch the console it'll provide you with a set of options - pretty self explanatory.
|
|
24
|
+
When it does backups it stores them in ````~/.mongo-db-utils/backups/````. The naming convention is ````${server}_${port}/${database_name}/${timestamp}/db````
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Building source
|
|
28
|
+
|
|
29
|
+
#run console
|
|
30
|
+
bundle exec bin/mongo-db-utils console
|
|
31
|
+
|
|
32
|
+
#install the gem locally
|
|
33
|
+
rake build
|
|
34
|
+
gem install pkg/mongo-db-utils.gem
|
|
35
|
+
|
|
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/Rakefile
ADDED
data/bin/mongo-db-utils
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'aruba/cucumber'
|
data/install_rdebug.sh
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#see: http://stackoverflow.com/questions/6438116/rails-with-ruby-debugger-throw-symbol-not-found-ruby-current-thread-loaderro
|
|
2
|
+
|
|
3
|
+
export PATCH_LEVEL=`ruby -e 'puts RUBY_PATCHLEVEL'`
|
|
4
|
+
export RVM_SRC=$HOME/.rvm/rubies/ruby-1.9.3-p$PATCH_LEVEL/include/ruby-1.9.1
|
|
5
|
+
gem install archive-tar-minitar
|
|
6
|
+
gem install ruby_core_source -- --with-ruby-include=/$RVM_SRC
|
|
7
|
+
export RVM_SRC=$HOME/.rvm/rubies/ruby-1.9.3-p$PATCH_LEVEL/include/ruby-1.9.1/ruby-1.9.3-p$PATCH_LEVEL
|
|
8
|
+
|
|
9
|
+
wget http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
|
|
10
|
+
wget http://rubyforge.org/frs/download.php/63094/ruby-debug19-0.11.6.gem
|
|
11
|
+
wget http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
|
|
12
|
+
gem install linecache19-0.5.13.gem -- --with-ruby-include=/$RVM_SRC
|
|
13
|
+
# if that step failed, and you are running OSX Lion, then following this post can help you:
|
|
14
|
+
# http://stackoverflow.com/questions/8032824/cant-install-ruby-under-lion-with-rvm-gcc-issues
|
|
15
|
+
# this happens if you recently installed xcode from the app store.
|
|
16
|
+
# bizarrely, for me I had to do this: ln -s /usr/bin/gcc /usr/bin/gcc-4.2
|
|
17
|
+
gem install ruby-debug-base19-0.11.26.gem -- --with-ruby-include=/$RVM_SRC
|
|
18
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'thor'
|
|
2
|
+
require 'mongo-db-utils/config-loader'
|
|
3
|
+
require 'mongo-db-utils/cmd'
|
|
4
|
+
require 'mongo-db-utils/console'
|
|
5
|
+
|
|
6
|
+
module MongoDbUtils
|
|
7
|
+
class CLI < Thor
|
|
8
|
+
|
|
9
|
+
desc "console", "run the console"
|
|
10
|
+
def console
|
|
11
|
+
@config = MongoDbUtils::ConfigLoader.load
|
|
12
|
+
console = MongoDbUtils::Console.new(@config, MongoDbUtils::Cmd)
|
|
13
|
+
console.run
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require 'mongo-db-utils/cmd/mongotools'
|
|
2
|
+
require 'mongo'
|
|
3
|
+
require 'mongo/connection'
|
|
4
|
+
|
|
5
|
+
module MongoDbUtils
|
|
6
|
+
class Cmd
|
|
7
|
+
|
|
8
|
+
def self.backup(db, path)
|
|
9
|
+
|
|
10
|
+
unless( db_exists?(db) )
|
|
11
|
+
return false
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
t = Time.new
|
|
15
|
+
timestamp = t.strftime("%Y.%m.%d__%H.%M")
|
|
16
|
+
out_path = "#{path}/#{db.host}_#{db.port}/#{db.name}/#{timestamp}"
|
|
17
|
+
full_path = File.expand_path(out_path)
|
|
18
|
+
|
|
19
|
+
FileUtils.mkdir_p(full_path)
|
|
20
|
+
MongoDbUtils::Commands::MongoTools.dump(db.host,db.port,db.name,full_path,db.username,db.password)
|
|
21
|
+
|
|
22
|
+
`tar cvf #{full_path}/#{db.name}.tar.gz #{full_path}/#{db.name}`
|
|
23
|
+
`rm -fr #{full_path}/#{db.name}`
|
|
24
|
+
true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.copy(path, source, destination, halt_on_no_backup = true)
|
|
28
|
+
|
|
29
|
+
backup_made = self.backup(destination, path)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
if( !backup_made && halt_on_no_backup)
|
|
33
|
+
puts "aborting - no backup was made"
|
|
34
|
+
return
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
remove_db(destination)
|
|
38
|
+
puts "copying... please wait..."
|
|
39
|
+
connection = Mongo::Connection.from_uri(destination.to_s)
|
|
40
|
+
source_server = "#{source.host}:#{source.port}"
|
|
41
|
+
connection.copy_database(source.name,destination.name,source_server,source.username, source.password)
|
|
42
|
+
connection.close
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
def self.db_exists?(db)
|
|
48
|
+
connection = Mongo::Connection.from_uri(db.to_s)
|
|
49
|
+
contains_db = connection.database_names.include?(db.name)
|
|
50
|
+
connection.close
|
|
51
|
+
contains_db
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.remove_db(db)
|
|
55
|
+
connection = Mongo::Connection.from_uri(db.to_s)
|
|
56
|
+
if( connection.database_names.include?(db.name))
|
|
57
|
+
connection.drop_database(db.name)
|
|
58
|
+
end
|
|
59
|
+
connection.close
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module MongoDbUtils
|
|
2
|
+
module Commands
|
|
3
|
+
|
|
4
|
+
class Option
|
|
5
|
+
attr_accessor :key, :value
|
|
6
|
+
|
|
7
|
+
def initialize(key,value)
|
|
8
|
+
@key = key
|
|
9
|
+
@value = value
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def empty?
|
|
13
|
+
@value.nil? || @value.empty?
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class MongoTools
|
|
18
|
+
|
|
19
|
+
def self.dump(host,port,db,output,username = "", password = "")
|
|
20
|
+
|
|
21
|
+
options = []
|
|
22
|
+
options << Option.new("-h", "#{host}:#{port}")
|
|
23
|
+
options << Option.new("-db", db)
|
|
24
|
+
options << Option.new("-o", output)
|
|
25
|
+
options << Option.new("-u", username)
|
|
26
|
+
options << Option.new("-p", password)
|
|
27
|
+
|
|
28
|
+
cmd = "mongodump "
|
|
29
|
+
|
|
30
|
+
options.each do |o|
|
|
31
|
+
cmd << "#{o.key} #{o.value} " unless o.empty?
|
|
32
|
+
end
|
|
33
|
+
puts "cmd:"
|
|
34
|
+
puts cmd
|
|
35
|
+
`#{cmd}`
|
|
36
|
+
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
require 'mongo-db-utils/models'
|
|
2
|
+
|
|
3
|
+
module MongoDbUtils
|
|
4
|
+
|
|
5
|
+
class ConfigLoader
|
|
6
|
+
|
|
7
|
+
ROOT_FOLDER = "~/.mongo-db-utils"
|
|
8
|
+
CONFIG_LOCATION = "#{ROOT_FOLDER}/config.yml"
|
|
9
|
+
|
|
10
|
+
def self.load(load_path = CONFIG_LOCATION)
|
|
11
|
+
full_path = File.expand_path(load_path)
|
|
12
|
+
puts "loading config from #{full_path}"
|
|
13
|
+
|
|
14
|
+
if File.exist?(full_path) && YAML.load(File.open(full_path))
|
|
15
|
+
config = YAML.load(File.open(full_path))
|
|
16
|
+
config.writer = self
|
|
17
|
+
config
|
|
18
|
+
else
|
|
19
|
+
self.create_fresh_install_config(full_path)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.flush(path = CONFIG_LOCATION)
|
|
24
|
+
path = File.expand_path(path)
|
|
25
|
+
puts "removing: #{path}"
|
|
26
|
+
FileUtils.rm(path) if File.exist?(path)
|
|
27
|
+
self.initialize_files(path)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.save(config, path = CONFIG_LOCATION)
|
|
31
|
+
raise "config is nil" if config.nil?
|
|
32
|
+
|
|
33
|
+
File.open( File.expand_path(path), 'w' ) do |out|
|
|
34
|
+
YAML.dump( config, out )
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
def self.create_fresh_install_config(full_path)
|
|
40
|
+
config = Model::Config.new
|
|
41
|
+
config.writer = self
|
|
42
|
+
config.backup_folder = "#{ROOT_FOLDER}/backups"
|
|
43
|
+
self.initialize_files(full_path)
|
|
44
|
+
File.open( full_path, 'w' ) do |out|
|
|
45
|
+
YAML.dump( config, out )
|
|
46
|
+
end
|
|
47
|
+
config
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.get_folder_name(path)
|
|
51
|
+
/(.*)\/.*.yml/.match(path)[1]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.initialize_files(path)
|
|
55
|
+
folder = self.get_folder_name(path)
|
|
56
|
+
FileUtils.mkdir_p(folder) unless File.exist?(folder)
|
|
57
|
+
FileUtils.touch(path)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
require 'highline/import'
|
|
2
|
+
require 'mongo-db-utils/version'
|
|
3
|
+
require 'mongo-db-utils/models'
|
|
4
|
+
|
|
5
|
+
module MongoDbUtils
|
|
6
|
+
class Console
|
|
7
|
+
|
|
8
|
+
HEADER = <<-eos
|
|
9
|
+
===================================
|
|
10
|
+
Mongo Db Utils - Version: #{MongoDbUtils::VERSION}
|
|
11
|
+
===================================
|
|
12
|
+
eos
|
|
13
|
+
|
|
14
|
+
def initialize(config, cmd)
|
|
15
|
+
@config = config
|
|
16
|
+
@cmd = cmd
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def run
|
|
20
|
+
say(HEADER)
|
|
21
|
+
main_menu
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
def main_menu
|
|
26
|
+
say("\nWhat do you want to do?")
|
|
27
|
+
choose do |menu|
|
|
28
|
+
prep_menu(menu)
|
|
29
|
+
menu.choice "copy a db" do copy_a_db end
|
|
30
|
+
menu.choice "backup a db" do do_backup end
|
|
31
|
+
menu.choice "remove config" do remove_config end
|
|
32
|
+
menu.choice "show config" do show_config end
|
|
33
|
+
menu.choice "add server to config" do add_config end
|
|
34
|
+
menu.choice "exit" do say("goodbye") end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def remove_config
|
|
39
|
+
@config.flush
|
|
40
|
+
main_menu
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def do_backup
|
|
44
|
+
|
|
45
|
+
if @config.empty?
|
|
46
|
+
get_config
|
|
47
|
+
else
|
|
48
|
+
list_dbs
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def get_config
|
|
53
|
+
say("You don't have any servers configured, please add one:")
|
|
54
|
+
add_config
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def show_config
|
|
58
|
+
say("Config")
|
|
59
|
+
say("--------------------")
|
|
60
|
+
say("dbs:")
|
|
61
|
+
say("--------------------")
|
|
62
|
+
@config.dbs.sort.each do |db|
|
|
63
|
+
say("#{db.to_s_simple}")
|
|
64
|
+
end
|
|
65
|
+
say("--------------------")
|
|
66
|
+
say("")
|
|
67
|
+
say("backups folder:")
|
|
68
|
+
say("--------------------")
|
|
69
|
+
say("#{@config.backup_folder}")
|
|
70
|
+
say("--------------------")
|
|
71
|
+
choose do |menu|
|
|
72
|
+
prep_menu(menu)
|
|
73
|
+
menu.choice "back" do main_menu end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def add_config
|
|
78
|
+
entry = Hash.new
|
|
79
|
+
entry[:mongo_uri] = ask("Mongo uri (eg: 'mongodb://user:pass@locahost:27017/db')")
|
|
80
|
+
successful = @config.add_db_from_uri(entry[:mongo_uri])
|
|
81
|
+
|
|
82
|
+
if successful
|
|
83
|
+
say("added server")
|
|
84
|
+
choose do |menu|
|
|
85
|
+
prep_menu(menu)
|
|
86
|
+
menu.choice "add another?" do add_config end
|
|
87
|
+
menu.choice "done" do main_menu end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
else
|
|
91
|
+
say("couldn't add uri")
|
|
92
|
+
add_config
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def list_dbs
|
|
98
|
+
say("Which db?")
|
|
99
|
+
choose do |menu|
|
|
100
|
+
prep_menu(menu)
|
|
101
|
+
@config.dbs.each do |db|
|
|
102
|
+
menu.choice "#{db.to_s_simple}" do backup(db) end
|
|
103
|
+
end
|
|
104
|
+
menu.choice "back" do main_menu end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def copy_a_db
|
|
110
|
+
|
|
111
|
+
copy_plan = Hash.new
|
|
112
|
+
|
|
113
|
+
say("Choose db to copy:")
|
|
114
|
+
choose do |menu|
|
|
115
|
+
prep_menu(menu)
|
|
116
|
+
@config.dbs.sort.each do |db|
|
|
117
|
+
menu.choice "#{db.to_s_simple}" do
|
|
118
|
+
copy_plan[:source] = db
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
end
|
|
122
|
+
menu.choice "add server to config" do add_config end
|
|
123
|
+
menu.choice "back" do
|
|
124
|
+
main_menu
|
|
125
|
+
return
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
say("Choose db destination:")
|
|
130
|
+
choose do |menu|
|
|
131
|
+
prep_menu(menu)
|
|
132
|
+
@config.dbs.sort.each do |db|
|
|
133
|
+
menu.choice "#{db.to_s_simple}" do
|
|
134
|
+
copy_plan[:destination] = db
|
|
135
|
+
end unless db == copy_plan[:source]
|
|
136
|
+
end
|
|
137
|
+
menu.choice "add server to config" do add_config end
|
|
138
|
+
end
|
|
139
|
+
show_copy_plan(copy_plan)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def show_copy_plan(plan)
|
|
143
|
+
say("Copy: (we'll backup the destination before we copy)")
|
|
144
|
+
say("#{plan[:source].to_s_simple} --> #{plan[:destination].to_s_simple}")
|
|
145
|
+
|
|
146
|
+
choose do |menu|
|
|
147
|
+
prep_menu(menu)
|
|
148
|
+
menu.choice "Begin" do begin_copy(plan) end
|
|
149
|
+
menu.choice "Reverse" do
|
|
150
|
+
show_copy_plan( {:source => plan[:destination], :destination => plan[:source]})
|
|
151
|
+
end
|
|
152
|
+
menu.choice "Back" do main_menu end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def begin_copy(plan)
|
|
157
|
+
say("doing copy...")
|
|
158
|
+
@cmd.copy(@config.backup_folder, plan[:source], plan[:destination], false)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
private
|
|
162
|
+
def backup(db)
|
|
163
|
+
puts ">> ..backing up #{db}"
|
|
164
|
+
@cmd.backup(db, @config.backup_folder)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def prep_menu(menu)
|
|
168
|
+
menu.index = :number
|
|
169
|
+
menu.index_suffix = ") "
|
|
170
|
+
menu.prompt = "?"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
module MongoDbUtils
|
|
2
|
+
|
|
3
|
+
module Model
|
|
4
|
+
class Config
|
|
5
|
+
attr_reader :dbs
|
|
6
|
+
attr_writer :writer
|
|
7
|
+
attr_accessor :backup_folder
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@dbs = []
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def empty?
|
|
14
|
+
@dbs.nil? || @dbs.empty?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def flush
|
|
18
|
+
@dbs = []
|
|
19
|
+
@writer.flush
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def add_db_from_uri(uri)
|
|
23
|
+
@dbs = [] if @dbs.nil?
|
|
24
|
+
db = Db.from_uri(uri)
|
|
25
|
+
unless db.nil?
|
|
26
|
+
@dbs << db
|
|
27
|
+
@dbs.sort!
|
|
28
|
+
@writer.save(self)
|
|
29
|
+
end
|
|
30
|
+
!db.nil?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def save
|
|
34
|
+
@writer.save(self)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_s
|
|
38
|
+
"Config"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# A Db stored in the config
|
|
44
|
+
class Db
|
|
45
|
+
|
|
46
|
+
URI_NO_USER = /mongodb:\/\/(.*):(.*)\/(.*$)/
|
|
47
|
+
URI_USER = /mongodb:\/\/(.*):(.*)@(.*):(.*)\/(.*$)/
|
|
48
|
+
|
|
49
|
+
attr_accessor :host, :username, :password, :port, :name
|
|
50
|
+
|
|
51
|
+
def initialize(name, host, port, username=nil, password=nil)
|
|
52
|
+
@host = host
|
|
53
|
+
@port = port
|
|
54
|
+
@name = name
|
|
55
|
+
@username = username
|
|
56
|
+
@password = password
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.from_uri(uri)
|
|
60
|
+
|
|
61
|
+
user,pwd,host,port,db = nil
|
|
62
|
+
|
|
63
|
+
if( uri.match(URI_USER))
|
|
64
|
+
match, user, pwd, host, port, name = *uri.match(URI_USER)
|
|
65
|
+
elsif(uri.match(URI_NO_USER))
|
|
66
|
+
match, host, port, name = *uri.match(URI_NO_USER)
|
|
67
|
+
user = ""
|
|
68
|
+
pwd = ""
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
return nil if( host.nil? || port.nil? || name.nil? )
|
|
72
|
+
|
|
73
|
+
Db.new(name,host,port,user,pwd)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def to_s
|
|
78
|
+
user_pass = ""
|
|
79
|
+
unless(@username.empty? || @password.empty? )
|
|
80
|
+
user_pass = "#{@username}:#{@password}@"
|
|
81
|
+
end
|
|
82
|
+
"mongodb://#{user_pass}#{@host}:#{@port}/#{@name}"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def to_s_simple
|
|
86
|
+
"#{@host}:#{@port}/#{@name}"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def <=>(other)
|
|
90
|
+
self.to_s <=> other.to_s
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/mongo-db-utils/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["edeustace"]
|
|
6
|
+
gem.email = ["ed.eustace@gmail.com"]
|
|
7
|
+
gem.description = %q{some utilities for managing your mongod dbs}
|
|
8
|
+
gem.summary = %q{some utilities for managing your mongod dbs}
|
|
9
|
+
gem.homepage = ""
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = "mongo-db-utils"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = MongoDbUtils::VERSION
|
|
17
|
+
gem.add_development_dependency "rspec", "~> 2.6"
|
|
18
|
+
gem.add_development_dependency "cucumber"
|
|
19
|
+
gem.add_development_dependency "aruba"
|
|
20
|
+
gem.add_dependency "highline", "~> 1.6.13"
|
|
21
|
+
gem.add_dependency "thor"
|
|
22
|
+
gem.add_dependency "mongo"
|
|
23
|
+
gem.add_dependency "bson"
|
|
24
|
+
gem.add_dependency "bson_ext"
|
|
25
|
+
|
|
26
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'mongo-db-utils/config-loader'
|
|
2
|
+
|
|
3
|
+
describe MongoDbUtils do
|
|
4
|
+
|
|
5
|
+
it "should create a config if one doesn't exist" do
|
|
6
|
+
|
|
7
|
+
tmp_file = ".tmp_path/config.yml"
|
|
8
|
+
|
|
9
|
+
FileUtils.rm_rf(".tmp_path")
|
|
10
|
+
|
|
11
|
+
File.exist?(tmp_file).should eql(false)
|
|
12
|
+
|
|
13
|
+
config = MongoDbUtils::ConfigLoader.load(".tmp_path/config.yml")
|
|
14
|
+
|
|
15
|
+
File.exist?(tmp_file).should eql(true)
|
|
16
|
+
|
|
17
|
+
FileUtils.rm_rf(".tmp_path")
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'mongo-db-utils/models'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
describe MongoDbUtils::Model do
|
|
5
|
+
|
|
6
|
+
it "should parse mongo uris" do
|
|
7
|
+
|
|
8
|
+
uri = "mongodb://localhost:27017/ed-backup"
|
|
9
|
+
db = MongoDbUtils::Model::Db.from_uri(uri)
|
|
10
|
+
db.to_s.should eql(uri)
|
|
11
|
+
db.host.should eql("localhost")
|
|
12
|
+
db.port.should eql("27017")
|
|
13
|
+
db.name.should eql("ed-backup")
|
|
14
|
+
db.username.should eql("")
|
|
15
|
+
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should parse mongo uris" do
|
|
19
|
+
|
|
20
|
+
uri = "mongodb://ed:password@localhost:27017/ed-backup"
|
|
21
|
+
db = MongoDbUtils::Model::Db.from_uri(uri)
|
|
22
|
+
db.to_s.should eql(uri)
|
|
23
|
+
db.host.should eql("localhost")
|
|
24
|
+
db.port.should eql("27017")
|
|
25
|
+
db.name.should eql("ed-backup")
|
|
26
|
+
db.username.should eql("ed")
|
|
27
|
+
db.password.should eql("password")
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should return nil if its a bad uri" do
|
|
32
|
+
|
|
33
|
+
uri = ""
|
|
34
|
+
db = MongoDbUtils::Model::Db.from_uri(uri)
|
|
35
|
+
db.should be(nil)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
class MockWriter
|
|
39
|
+
def save(config)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def flush
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
it "config should add dbs" do
|
|
48
|
+
config = MongoDbUtils::Model::Config.new
|
|
49
|
+
config.writer = MockWriter.new
|
|
50
|
+
|
|
51
|
+
config.add_db_from_uri("mongodb://blah:3333/blah")
|
|
52
|
+
config.dbs.length.should eql(1)
|
|
53
|
+
config.add_db_from_uri("mongodb://blah:3333/blah")
|
|
54
|
+
config.dbs.length.should eql(2)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mongo-db-utils
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- edeustace
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-07-22 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rspec
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '2.6'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '2.6'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: cucumber
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: aruba
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ! '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: highline
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
66
|
+
requirements:
|
|
67
|
+
- - ~>
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 1.6.13
|
|
70
|
+
type: :runtime
|
|
71
|
+
prerelease: false
|
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
73
|
+
none: false
|
|
74
|
+
requirements:
|
|
75
|
+
- - ~>
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: 1.6.13
|
|
78
|
+
- !ruby/object:Gem::Dependency
|
|
79
|
+
name: thor
|
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
82
|
+
requirements:
|
|
83
|
+
- - ! '>='
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
type: :runtime
|
|
87
|
+
prerelease: false
|
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
89
|
+
none: false
|
|
90
|
+
requirements:
|
|
91
|
+
- - ! '>='
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '0'
|
|
94
|
+
- !ruby/object:Gem::Dependency
|
|
95
|
+
name: mongo
|
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
|
97
|
+
none: false
|
|
98
|
+
requirements:
|
|
99
|
+
- - ! '>='
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
type: :runtime
|
|
103
|
+
prerelease: false
|
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
105
|
+
none: false
|
|
106
|
+
requirements:
|
|
107
|
+
- - ! '>='
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
- !ruby/object:Gem::Dependency
|
|
111
|
+
name: bson
|
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
|
113
|
+
none: false
|
|
114
|
+
requirements:
|
|
115
|
+
- - ! '>='
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :runtime
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
none: false
|
|
122
|
+
requirements:
|
|
123
|
+
- - ! '>='
|
|
124
|
+
- !ruby/object:Gem::Version
|
|
125
|
+
version: '0'
|
|
126
|
+
- !ruby/object:Gem::Dependency
|
|
127
|
+
name: bson_ext
|
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
|
129
|
+
none: false
|
|
130
|
+
requirements:
|
|
131
|
+
- - ! '>='
|
|
132
|
+
- !ruby/object:Gem::Version
|
|
133
|
+
version: '0'
|
|
134
|
+
type: :runtime
|
|
135
|
+
prerelease: false
|
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
137
|
+
none: false
|
|
138
|
+
requirements:
|
|
139
|
+
- - ! '>='
|
|
140
|
+
- !ruby/object:Gem::Version
|
|
141
|
+
version: '0'
|
|
142
|
+
description: some utilities for managing your mongod dbs
|
|
143
|
+
email:
|
|
144
|
+
- ed.eustace@gmail.com
|
|
145
|
+
executables:
|
|
146
|
+
- mongo-db-utils
|
|
147
|
+
extensions: []
|
|
148
|
+
extra_rdoc_files: []
|
|
149
|
+
files:
|
|
150
|
+
- .gitignore
|
|
151
|
+
- .mongo-db-utils/config.yml
|
|
152
|
+
- Gemfile
|
|
153
|
+
- LICENSE
|
|
154
|
+
- README.md
|
|
155
|
+
- Rakefile
|
|
156
|
+
- bin/mongo-db-utils
|
|
157
|
+
- features/mongo-db-utils.feature
|
|
158
|
+
- features/support/setup.rb
|
|
159
|
+
- install_rdebug.sh
|
|
160
|
+
- lib/mongo-db-utils/cli.rb
|
|
161
|
+
- lib/mongo-db-utils/cmd.rb
|
|
162
|
+
- lib/mongo-db-utils/cmd/mongotools.rb
|
|
163
|
+
- lib/mongo-db-utils/config-loader.rb
|
|
164
|
+
- lib/mongo-db-utils/console.rb
|
|
165
|
+
- lib/mongo-db-utils/models.rb
|
|
166
|
+
- lib/mongo-db-utils/version.rb
|
|
167
|
+
- mongo-db-utils.gemspec
|
|
168
|
+
- spec/config-loader-spec.rb
|
|
169
|
+
- spec/mongo_db_utils_spec.rb
|
|
170
|
+
homepage: ''
|
|
171
|
+
licenses: []
|
|
172
|
+
post_install_message:
|
|
173
|
+
rdoc_options: []
|
|
174
|
+
require_paths:
|
|
175
|
+
- lib
|
|
176
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
177
|
+
none: false
|
|
178
|
+
requirements:
|
|
179
|
+
- - ! '>='
|
|
180
|
+
- !ruby/object:Gem::Version
|
|
181
|
+
version: '0'
|
|
182
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
183
|
+
none: false
|
|
184
|
+
requirements:
|
|
185
|
+
- - ! '>='
|
|
186
|
+
- !ruby/object:Gem::Version
|
|
187
|
+
version: '0'
|
|
188
|
+
requirements: []
|
|
189
|
+
rubyforge_project:
|
|
190
|
+
rubygems_version: 1.8.24
|
|
191
|
+
signing_key:
|
|
192
|
+
specification_version: 3
|
|
193
|
+
summary: some utilities for managing your mongod dbs
|
|
194
|
+
test_files:
|
|
195
|
+
- features/mongo-db-utils.feature
|
|
196
|
+
- features/support/setup.rb
|
|
197
|
+
- spec/config-loader-spec.rb
|
|
198
|
+
- spec/mongo_db_utils_spec.rb
|