mongodb_clone 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongodb_clone.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tiago Rafael Godinho
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,74 @@
1
+ # MongodbClone
2
+
3
+ Dump and restore databases easily.
4
+
5
+ [Backup and Restoration Strategies & MongoDB Manual](http://docs.mongodb.org/manual/administration/backups/#database-dumps)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'mongodb_clone'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ Use a rake task to **dump** production database and **restore** locally.
20
+
21
+
22
+ $ rake db:copy:production:to_local
23
+
24
+ or create your tasks:
25
+
26
+ ``` ruby
27
+ # Dump environment "staging", session "default" and restore in "development" environment, session "backup"
28
+ MongodbClone::MongodbReplication.new.dump('staging').restore('development', 'backup')
29
+ ```
30
+
31
+ Use `config/mongoid.yml` to configure the environments:
32
+
33
+ ``` yml
34
+ development:
35
+ sessions:
36
+ default:
37
+ database: mongodb_clone_development
38
+ hosts:
39
+ - localhost:27017
40
+ backup:
41
+ database: mongodb_clone_development
42
+ hosts:
43
+ - localhost:27017
44
+
45
+ staging:
46
+ sessions:
47
+ default:
48
+ database: mongodb_clone_staging
49
+ username: mongodb_clone
50
+ password: 12345678
51
+ hosts:
52
+ - mongodb_clone_staging.example.com:27017
53
+
54
+ production:
55
+ sessions:
56
+ default:
57
+ database: mongodb_clone_production
58
+ username: mongodb_clone
59
+ password: 12345678
60
+ hosts:
61
+ - mongodb_clone.example.com:27017
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
71
+
72
+ ## License
73
+
74
+ MIT License. Copyright 2012 Tiago Rafael Godinho
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: 'spec'
@@ -0,0 +1,7 @@
1
+ require 'rails/engine'
2
+
3
+ module MongodbClone
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace MongodbClone
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module MongodbClone
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,63 @@
1
+ require 'rails'
2
+ require 'mongodb_clone/engine'
3
+ require 'mongodb_clone/version'
4
+
5
+ module MongodbClone
6
+ class MongodbReplication
7
+ attr_accessor :id, :path
8
+
9
+ def initialize
10
+ @id = Time.now.utc.strftime('%Y%m%d%H%M%S')
11
+ end
12
+
13
+ def dump(environment = 'production', session = 'default')
14
+ config = base_config[environment]['sessions'][session]
15
+
16
+ params = {
17
+ h: config['hosts'][0],
18
+ d: config['database'],
19
+ u: config['username'],
20
+ p: config['password'],
21
+ o: "/tmp/#{ config['database'] }/#{ @id }"
22
+ }
23
+
24
+ a = params.collect { |key, value| "-#{ key } #{ value }" }.join(' ')
25
+
26
+ command = "mongodump #{ a }"
27
+
28
+ execute(command)
29
+
30
+ @path = "#{ params[:o] }/#{ params[:d] }"
31
+
32
+ self
33
+ end
34
+
35
+ def restore(environment = 'development', session = 'default')
36
+ config = base_config[environment]['sessions'][session]
37
+
38
+ params = {
39
+ h: config['hosts'][0],
40
+ d: config['database'],
41
+ u: config['username'],
42
+ p: config['password']
43
+ }
44
+
45
+ a = params.collect { |key, value| "-#{ key } #{ value }" if value }.compact.join(' ')
46
+
47
+ command = "mongorestore #{ a } #{ @path }"
48
+
49
+ execute(command)
50
+ end
51
+
52
+ private
53
+
54
+ def base_config
55
+ YAML.load_file(Rails.root.join('config/mongoid.yml'))
56
+ end
57
+
58
+ def execute(command)
59
+ puts command
60
+ `#{ command }`
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,10 @@
1
+ namespace :db do
2
+ namespace :copy do
3
+ namespace :production do
4
+ desc 'Copy production database'
5
+ task to_local: ['db:drop'] do
6
+ MongodbClone::MongodbReplication.new.dump.restore
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongodb_clone/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'mongodb_clone'
8
+ gem.version = MongodbClone::VERSION
9
+ gem.authors = ['Tiago Rafael Godinho']
10
+ gem.email = ['tiagogodinho3@gmail.com']
11
+ gem.description = %q{Write a gem description}
12
+ gem.summary = %q{Write a gem summary}
13
+ gem.homepage = ''
14
+
15
+ gem.add_dependency 'rails', '~> 3.2.0'
16
+
17
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ['lib']
23
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe MongodbClone::MongodbReplication do
4
+ before do
5
+ Rails.stub_chain(:root, :join).and_return(File.join(File.dirname(__FILE__), 'support', 'mongoid.yml'))
6
+ end
7
+
8
+ describe '#dump' do
9
+ it 'should dump database' do
10
+ subject.id = '201210231843100200'
11
+ subject.should_receive(:execute).with('mongodump -h mongodb_clone.example.com:27017 -d mongodb_clone_production -u mongodb_clone -p 12345678 -o /tmp/mongodb_clone_production/201210231843100200')
12
+
13
+ subject.dump
14
+ end
15
+ end
16
+
17
+ describe '#restore' do
18
+ it 'should restore database' do
19
+ subject.id = '201210231843100200'
20
+ subject.path = '/tmp/mongodb_clone_production/201210231843100200/mongodb_clone_production'
21
+ subject.should_receive(:execute).with('mongodump -h mongodb_clone.example.com:27017 -d mongodb_clone_production -u mongodb_clone -p 12345678 -o /tmp/mongodb_clone_production/201210231843100200')
22
+ subject.should_receive(:execute).with('mongorestore -h localhost:27017 -d mongodb_clone_development /tmp/mongodb_clone_production/201210231843100200/mongodb_clone_production')
23
+
24
+ subject.dump.restore
25
+ end
26
+ end
27
+ end
@@ -0,0 +1 @@
1
+ require 'mongodb_clone'
@@ -0,0 +1,15 @@
1
+ development:
2
+ sessions:
3
+ default:
4
+ database: mongodb_clone_development
5
+ hosts:
6
+ - localhost:27017
7
+
8
+ production:
9
+ sessions:
10
+ default:
11
+ database: mongodb_clone_production
12
+ username: mongodb_clone
13
+ password: 12345678
14
+ hosts:
15
+ - mongodb_clone.example.com:27017
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongodb_clone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tiago Rafael Godinho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.11.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: 2.11.0
46
+ description: Write a gem description
47
+ email:
48
+ - tiagogodinho3@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/mongodb_clone.rb
60
+ - lib/mongodb_clone/engine.rb
61
+ - lib/mongodb_clone/version.rb
62
+ - lib/tasks/mongodb_clone.rake
63
+ - mongodb_clone.gemspec
64
+ - spec/mongodb_clone_spec.rb
65
+ - spec/spec_helper.rb
66
+ - spec/support/mongoid.yml
67
+ homepage: ''
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: -3149178088274054404
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: -3149178088274054404
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.24
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Write a gem summary
97
+ test_files:
98
+ - spec/mongodb_clone_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/support/mongoid.yml