mongo-db-utils 0.0.3 → 0.0.4

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 CHANGED
@@ -8,8 +8,10 @@ A little gem that simplifies backing up and copying your mongo dbs.
8
8
 
9
9
  It saves your database urls so any task is just a few clicks.
10
10
 
11
- * backup a database
12
- * copy a database from one server to another (whilst backing up the target db if it exists)
11
+ * backup a database locally
12
+ * backup a database and deploy it to Amazon S3
13
+ * copy a database from one server to another (whilst backing up locally the target db if it exists)
14
+
13
15
 
14
16
 
15
17
 
@@ -2,15 +2,42 @@ require 'thor'
2
2
  require 'mongo-db-utils/config-loader'
3
3
  require 'mongo-db-utils/cmd'
4
4
  require 'mongo-db-utils/console'
5
+ require 'mongo-db-utils/models'
6
+ require 'mongo-db-utils/s3'
5
7
 
6
8
  module MongoDbUtils
7
9
  class CLI < Thor
8
10
 
9
- desc "console", "run the console"
11
+ desc "console", "run the interactive console"
10
12
  def console
11
13
  @config = MongoDbUtils::ConfigLoader.load
12
14
  console = MongoDbUtils::Console.new(@config, MongoDbUtils::Cmd)
13
15
  console.run
14
16
  end
17
+
18
+ desc "backup MONGO_URI", "backup a db with a mongo uri eg: mongodb://user:pass@server:port/dbname"
19
+ def backup(mongo_uri)
20
+ @config = MongoDbUtils::ConfigLoader.load
21
+ db = MongoDbUtils::Model::Db.from_uri(mongo_uri)
22
+ raise "can't parse uri" if db.nil?
23
+ MongoDbUtils::Cmd.backup(db, @config.backup_folder)
24
+ end
25
+
26
+
27
+ desc "backup_s3 MONGO_URI BUCKET ACCESS_KEY SECRET_ACCESS_KEY", "backup a db to Amason s3 with a mongo uri eg: mongodb://user:pass@server:port/dbname"
28
+ def backup_s3(mongo_uri, bucket_name, access_key_id, secret_access_key)
29
+ @config = MongoDbUtils::ConfigLoader.load
30
+ db = MongoDbUtils::Model::Db.from_uri(mongo_uri)
31
+ raise "can't parse uri" if db.nil?
32
+ tar_file = MongoDbUtils::Cmd.backup(db, @config.backup_folder)
33
+
34
+ name = tar_file.gsub(File.expand_path(@config.backup_folder), "")
35
+
36
+ MongoDbUtils::S3::put_file(tar_file, name, bucket_name, access_key_id, secret_access_key)
37
+ file = File.basename(tar_file)
38
+ folder = tar_file.gsub(file, "")
39
+ `rm -fr #{folder}`
40
+ end
41
+
15
42
  end
16
43
  end
@@ -1,3 +1,5 @@
1
+ require 'aws/s3'
2
+
1
3
  module MongoDbUtils
2
4
  module Commands
3
5
 
@@ -58,5 +60,18 @@ module MongoDbUtils
58
60
  end
59
61
 
60
62
  end
63
+
64
+ class S3
65
+
66
+ def self.put_file(file, bucket_name, access_key_id, secret_access_key)
67
+ AWS::S3::Base.establish_connection!(
68
+ :access_key_id => access_key_id,
69
+ :secret_access_key => secret_access_key
70
+ )
71
+
72
+ Service.buckets.create(bucket_name) if Service.buckets.find(bucket_name).nil?
73
+ S3Object.store(file, open(file), bucket_name)
74
+ end
75
+ end
61
76
  end
62
77
  end
@@ -40,7 +40,7 @@ module MongoDbUtils
40
40
  `rm -fr #{full_path}/#{db.name}`
41
41
  end
42
42
 
43
- true
43
+ "#{full_path}/#{db.name}.tar"
44
44
  end
45
45
 
46
46
  # With remote dbs you can't do a copy_database if you're not an admin.
@@ -88,6 +88,8 @@ module MongoDbUtils
88
88
  puts "DB exists? #{db.to_s}"
89
89
 
90
90
  connection = Mongo::Connection.from_uri(db.to_s)
91
+
92
+ puts "db.name: #{db.name}"
91
93
  mongo_db = connection[db.name]
92
94
 
93
95
  exists = !mongo_db.nil?
@@ -1,4 +1,5 @@
1
1
  require 'mongo-db-utils/models'
2
+ require 'yaml'
2
3
 
3
4
  module MongoDbUtils
4
5
 
@@ -6,11 +6,11 @@ module MongoDbUtils
6
6
  class Console
7
7
 
8
8
  HEADER = <<-eos
9
- ===================================
10
- Mongo Db Utils - Version: #{MongoDbUtils::VERSION}
11
- ===================================
12
- eos
13
-
9
+ ===================================
10
+ Mongo Db Utils - Version: #{MongoDbUtils::VERSION}
11
+ ===================================
12
+ eos
13
+
14
14
 
15
15
  def initialize(config, cmd)
16
16
  @config = config
@@ -33,6 +33,7 @@ eos
33
33
  menu.choice "remove config" do remove_config end
34
34
  menu.choice "show config" do show_config end
35
35
  menu.choice "add server to config" do add_config end
36
+ menu.choice "remove server from config" do remove_server_from_config end
36
37
  menu.choice "exit" do say("goodbye") end
37
38
  end
38
39
  end
@@ -70,7 +71,7 @@ eos
70
71
  say("--------------------")
71
72
  say("#{@config.backup_folder}")
72
73
  say("--------------------")
73
- choose do |menu|
74
+ choose do |menu|
74
75
  prep_menu(menu)
75
76
  menu.choice "back" do main_menu end
76
77
  end
@@ -109,6 +110,20 @@ eos
109
110
  end
110
111
 
111
112
 
113
+ def remove_server_from_config
114
+ say("remove server from config...")
115
+ choose do |menu|
116
+ prep_menu(menu)
117
+ @config.dbs.sort.each do |db|
118
+ menu.choice "#{db.to_s}" do
119
+ @config.remove_db(db)
120
+ remove_server_from_config
121
+ end
122
+ end
123
+ menu.choice "back" do main_menu end
124
+ end
125
+ end
126
+
112
127
  def copy_a_db
113
128
 
114
129
  copy_plan = Hash.new
@@ -117,15 +132,15 @@ eos
117
132
  choose do |menu|
118
133
  prep_menu(menu)
119
134
  @config.dbs.sort.each do |db|
120
- menu.choice "#{db.to_s}" do
121
- copy_plan[:source] = db
135
+ menu.choice "#{db.to_s}" do
136
+ copy_plan[:source] = db
122
137
  end
123
-
138
+
124
139
  end
125
140
  menu.choice "add server to config" do add_config end
126
- menu.choice "back" do
127
- main_menu
128
- return
141
+ menu.choice "back" do
142
+ main_menu
143
+ return
129
144
  end
130
145
  end
131
146
 
@@ -133,7 +148,7 @@ eos
133
148
  choose do |menu|
134
149
  prep_menu(menu)
135
150
  @config.dbs.sort.each do |db|
136
- menu.choice "#{db.to_s}" do
151
+ menu.choice "#{db.to_s}" do
137
152
  copy_plan[:destination] = db
138
153
  end unless db == copy_plan[:source]
139
154
  end
@@ -149,8 +164,8 @@ eos
149
164
  choose do |menu|
150
165
  prep_menu(menu)
151
166
  menu.choice "Begin" do begin_copy(plan) end
152
- menu.choice "Reverse" do
153
- show_copy_plan( {:source => plan[:destination], :destination => plan[:source]})
167
+ menu.choice "Reverse" do
168
+ show_copy_plan( {:source => plan[:destination], :destination => plan[:source]})
154
169
  end
155
170
  menu.choice "Back" do main_menu end
156
171
  end
@@ -175,4 +190,3 @@ eos
175
190
 
176
191
  end
177
192
  end
178
-
@@ -19,6 +19,11 @@ module MongoDbUtils
19
19
  @writer.flush
20
20
  end
21
21
 
22
+ def remove_db(db)
23
+ @dbs = @dbs - [db]
24
+ @writer.save(self)
25
+ end
26
+
22
27
  def add_db_from_uri(uri)
23
28
  @dbs = [] if @dbs.nil?
24
29
  db = Db.from_uri(uri)
@@ -0,0 +1,18 @@
1
+ require 'aws/s3'
2
+
3
+ module MongoDbUtils
4
+
5
+ class S3
6
+
7
+ def self.put_file(file, name, bucket_name, access_key_id, secret_access_key)
8
+ puts "putting file to Amazon S3"
9
+ AWS::S3::Base.establish_connection!(
10
+ :access_key_id => access_key_id,
11
+ :secret_access_key => secret_access_key
12
+ )
13
+
14
+ AWS::S3::Service.buckets.create(bucket_name) if AWS::S3::Service.buckets.find(bucket_name).nil?
15
+ AWS::S3::S3Object.store(name, open(file), bucket_name)
16
+ end
17
+ end
18
+ end
@@ -1,4 +1,4 @@
1
1
  module MongoDbUtils
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  READY_FOR_USE = "(Alpha version - not for production use!!)"
4
4
  end
@@ -22,5 +22,6 @@ Gem::Specification.new do |gem|
22
22
  gem.add_dependency "mongo"
23
23
  gem.add_dependency "bson"
24
24
  gem.add_dependency "bson_ext"
25
+ gem.add_dependency "aws-s3", "~> 0.6.3"
25
26
 
26
27
  end
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.3
4
+ version: 0.0.4
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-23 00:00:00.000000000 Z
12
+ date: 2012-07-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -139,6 +139,22 @@ dependencies:
139
139
  - - ! '>='
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: aws-s3
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 0.6.3
150
+ type: :runtime
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 0.6.3
142
158
  description: some utilities for managing your mongod dbs
143
159
  email:
144
160
  - ed.eustace@gmail.com
@@ -164,6 +180,7 @@ files:
164
180
  - lib/mongo-db-utils/config-loader.rb
165
181
  - lib/mongo-db-utils/console.rb
166
182
  - lib/mongo-db-utils/models.rb
183
+ - lib/mongo-db-utils/s3.rb
167
184
  - lib/mongo-db-utils/version.rb
168
185
  - mongo-db-utils.gemspec
169
186
  - spec/config-loader-spec.rb