siba-source-mongo-db 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 ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in siba-destination-mongo-db.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest', :notify=>false do
5
+ watch(%r|^test/unit/(.*\/)*test_(.*)\.rb|)
6
+ watch(%r|^lib/siba-source-mongo-db/(.*\/)*([^/]+)\.rb|) do |m|
7
+ "test/unit/#{m[1]}test_#{m[2]}.rb" unless m[2][0] == '.'
8
+ end
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2012 Evgeny Neumerzhitskiy
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,11 @@
1
+ # Overview
2
+
3
+ This is a plugin for [SIBA backup and restore utility](https://github.com/evgenyneu/siba). It allows to back and restore MongoDB.
4
+
5
+ ## Installation
6
+
7
+ $ gem install siba-sourcen-mongo-db
8
+
9
+ ## Usage
10
+
11
+ Run `siba generate FILE` command to generate options file and select mongo-db as a source plugin. Edit the generated file and set your backup options.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ namespace "test" do
5
+ desc "Run all unit tests"
6
+ Rake::TestTask.new("unit") do |t|
7
+ t.pattern = "test/unit/**/test*.rb"
8
+ t.libs << 'test'
9
+ end
10
+
11
+ desc "Run all integration tests"
12
+ Rake::TestTask.new("integration") do |t|
13
+ t.pattern = "test/integration/**/i9n_*.rb"
14
+ t.libs << 'test'
15
+ end
16
+
17
+ desc "Run all integration tests"
18
+ task :i9n => ["test:integration"] do
19
+ end
20
+ end
21
+
22
+ desc "Run all unit tests"
23
+ task :test => ["test:unit"] do
24
+ end
25
+
26
+ desc "Run tests"
27
+ task :default => "test:unit"
28
+
@@ -0,0 +1,100 @@
1
+ # encoding: UTF-8
2
+
3
+ module Siba::Source
4
+ module MongoDb
5
+ class Db
6
+ HIDE_PASSWORD_TEXT = "****p7d****"
7
+ include Siba::FilePlug
8
+ include Siba::LoggerPlug
9
+ attr_accessor :settings
10
+
11
+ def initialize(settings)
12
+ @settings = settings
13
+ check_installed
14
+ end
15
+
16
+ def check_installed
17
+ siba_file.run_this do
18
+ raise Siba::Error, "'mongodump' utility is not found. Please install mongodb." unless siba_file.shell_ok? "mongodump --help"
19
+ raise Siba::Error, "'mongorestore' utility is not found. Please install mongodb." unless siba_file.shell_ok? "mongorestore --help"
20
+ logger.info "Mongo backup utilities verified"
21
+ end
22
+ end
23
+
24
+ def backup(dest_dir)
25
+ siba_file.run_this do
26
+ unless Siba::FileHelper.dir_empty? dest_dir
27
+ raise Siba::Error, "Failed to backup MongoDB: output directory is not empty: #{dest_dir}"
28
+ end
29
+
30
+ command_without_password = %(mongodump -o "#{dest_dir}" #{get_shell_parameters})
31
+ command = command_without_password
32
+ unless settings[:password].nil?
33
+ command = command_without_password.gsub HIDE_PASSWORD_TEXT, settings[:password]
34
+ end
35
+ output = siba_file.run_shell command, "failed to backup MongoDb: #{command_without_password}"
36
+ raise Siba::Error, "failed to backup MongoDb: #{output}" if output =~ /ERROR:/
37
+
38
+ if Siba::FileHelper.dir_empty?(dest_dir)
39
+ raise Siba::Error, "Failed to backup MongoDB: dump directory is empty"
40
+ end
41
+
42
+ Siba::FileHelper.entries(dest_dir).each do |entry|
43
+ path_to_collection = File.join dest_dir, entry
44
+ next unless File.directory? path_to_collection
45
+ if Siba::FileHelper.dir_empty? path_to_collection
46
+ logger.warn "MongoDB collection/database name '#{entry}' is incorrect or it has no data."
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def restore(from_dir)
53
+ siba_file.run_this do
54
+ if Siba::FileHelper.dirs_count(from_dir) == 0
55
+ raise Siba::Error, "Failed to restore MongoDB: backup directory is empty: #{from_dir}"
56
+ end
57
+
58
+ unless settings[:database].nil?
59
+ dirs = Siba::FileHelper.dirs from_dir
60
+ if dirs.size != 1
61
+ raise Siba::Error, "Dump should contain exactly one directory when restoring a single database"
62
+ end
63
+ from_dir = File.join from_dir, dirs[0]
64
+ end
65
+
66
+ command_without_password = %(mongorestore --drop #{get_shell_parameters} "#{from_dir}")
67
+ command = command_without_password
68
+ unless settings[:password].nil?
69
+ command = command_without_password.gsub HIDE_PASSWORD_TEXT, settings[:password]
70
+ end
71
+ output = siba_file.run_shell command, "failed to restore MongoDb: #{command_without_password}"
72
+ raise Siba::Error, "failed to restore MongoDb: #{output}" if output =~ /ERROR:/
73
+ end
74
+ end
75
+
76
+ def get_shell_parameters
77
+ params = []
78
+ params << "-h \"#{escape_for_shell settings[:host]}\"" unless settings[:host].nil?
79
+ params << "-u \"#{escape_for_shell settings[:username]}\"" unless settings[:username].nil?
80
+ params << "-p \"#{HIDE_PASSWORD_TEXT}\"" unless settings[:password].nil?
81
+ params << "-d \"#{escape_for_shell settings[:database]}\"" unless settings[:database].nil?
82
+ params << "-c \"#{escape_for_shell settings[:collection]}\"" unless settings[:collection].nil?
83
+ params.join " "
84
+ end
85
+
86
+ def escape_for_shell(str)
87
+ str.gsub "\"", "\\\""
88
+ end
89
+
90
+ def db_and_collection_names
91
+ names = []
92
+ names << "db: #{settings[:database]}" unless settings[:database].nil?
93
+ names << "collection: #{settings[:collection]}" unless settings[:collection].nil?
94
+ out = names.join(", ")
95
+ out = " > " + out unless out.empty?
96
+ out
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'siba-source-mongo-db/db'
4
+
5
+ module Siba::Source
6
+ module MongoDb
7
+ class Init
8
+ include Siba::LoggerPlug
9
+ attr_accessor :db
10
+
11
+ def initialize(options)
12
+ host = Siba::SibaCheck.options_string options, "host", true
13
+ username = Siba::SibaCheck.options_string options, "username", true
14
+ password = Siba::SibaCheck.options_string options, "password", true
15
+ database = Siba::SibaCheck.options_string options, "database", true
16
+ collection = Siba::SibaCheck.options_string options, "collection", true
17
+ @db = Siba::Source::MongoDb::Db.new({
18
+ host: host,
19
+ username: username,
20
+ password: password,
21
+ database: database,
22
+ collection: collection})
23
+ end
24
+
25
+ # Collect source files and put them into dest_dir
26
+ # No return value is expected
27
+ def backup(dest_dir)
28
+ logger.info "Dumping MongoDb#{db.db_and_collection_names}"
29
+ @db.backup dest_dir
30
+ end
31
+
32
+ # Restore source files from_dir
33
+ # No return value is expected
34
+ def restore(from_dir)
35
+ logger.info "Restoring MongoDb#{db.db_and_collection_names}"
36
+ @db.restore from_dir
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ host: "hostname:port" # port is optional
2
+ username:
3
+ password:
4
+ database: # all databases if missing
5
+ collection: # all collections if missing
6
+ # Note: all above MongoDB settings are optional
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+
3
+ module Siba
4
+ module Source
5
+ module MongoDb
6
+ VERSION = "0.0.1"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+
3
+ require "siba-source-mongo-db/version"
4
+ require "siba-source-mongo-db/init"
5
+
6
+ module Siba
7
+ module Source
8
+ module MongoDb
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "siba-source-mongo-db/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "siba-source-mongo-db"
7
+ s.version = Siba::Source::MongoDb::VERSION
8
+ s.authors = ["Evgeny Neumerzhitskiy"]
9
+ s.email = ["sausageskin@gmail.com"]
10
+ s.homepage = "https://github.com/evgenyneu/siba-source-mongo-db"
11
+ s.license = "MIT"
12
+ s.summary = %q{MongoDB backup and restore extention for SIBA utility}
13
+ s.description = %q{An extension for SIBA backup and restore utility. It allows to backup and restore MongoDB database.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency 'siba', '~>0.5'
21
+
22
+ s.add_development_dependency 'minitest', '~>2.10'
23
+ s.add_development_dependency 'rake', '~>0.9'
24
+ s.add_development_dependency 'guard-minitest', '~>0.4'
25
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'siba/helpers/test/require'
4
+ SibaTest.init_integration
5
+
@@ -0,0 +1,4 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'siba/helpers/test/require'
4
+ SibaTest.init_unit
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper/require_integration'
4
+ require 'siba-source-mongo-db/init'
5
+
6
+ # Integration test example
7
+ # 'rake test:i9n' command runs integration tests
8
+ describe Siba::Source::MongoDb::Db do
9
+ before do
10
+ @cls = Siba::Source::MongoDb::Db
11
+ end
12
+
13
+ it "should init" do
14
+ @cls.new({})
15
+ end
16
+
17
+ it "should backup and restore" do
18
+ out_dir = mkdir_in_tmp_dir "mongob"
19
+ @cls.new({database: "sibatest"}).backup out_dir
20
+ Siba::FileHelper.dir_empty?(out_dir).must_equal false
21
+
22
+ @cls.new({database: "sibatest"}).restore out_dir
23
+ end
24
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper/require_unit'
4
+ require 'siba-source-mongo-db/init'
5
+
6
+ describe Siba::Source::MongoDb::Db do
7
+ before do
8
+ @cls = Siba::Source::MongoDb::Db
9
+ end
10
+
11
+ it "should init" do
12
+ settings= {one: "two"}
13
+ @obj = @cls.new settings
14
+ @obj.settings.must_equal settings
15
+ end
16
+
17
+ it "should backup" do
18
+ @obj = @cls.new({})
19
+ @obj.backup("/dest_dir")
20
+ end
21
+
22
+ it "should restore" do
23
+ @obj = @cls.new({})
24
+ @obj.restore("/from_dir")
25
+ end
26
+
27
+ it "should call get_shell_parameters" do
28
+ settings = {
29
+ host: "host:port",
30
+ username: "uname",
31
+ password: "my password",
32
+ database: "dbname",
33
+ collection: "db collection"}
34
+ @obj = @cls.new(settings)
35
+ params = @obj.get_shell_parameters
36
+ settings.each do |key,value|
37
+ params.must_include %("#{value}") if key != :password
38
+ params.must_include %("#{@cls::HIDE_PASSWORD_TEXT}") if key == :password
39
+ end
40
+ end
41
+
42
+ it "should espace for shell" do
43
+ @obj = @cls.new({})
44
+ @obj.escape_for_shell("hi\"").must_equal "hi\\\""
45
+ end
46
+
47
+ it "should call db_and_collection_names" do
48
+ @obj = @cls.new({})
49
+ @obj.db_and_collection_names.must_be_empty
50
+
51
+ @obj = @cls.new({database:"mydb"})
52
+ @obj.db_and_collection_names.must_include "mydb"
53
+ end
54
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper/require_unit'
4
+ require 'siba-source-mongo-db/init'
5
+
6
+ describe Siba::Source::MongoDb::Init do
7
+ before do
8
+ @yml_path = File.expand_path('../yml', __FILE__)
9
+ @plugin_category = "source"
10
+ @plugin_type = "mongo-db"
11
+ end
12
+
13
+ it "siba should load plugin" do
14
+ options = load_options "valid"
15
+ plugin = create_plugin "empty"
16
+ plugin = Siba::Source::MongoDb::Init.new({})
17
+ plugin = create_plugin "valid"
18
+ plugin.db.must_be_instance_of Siba::Source::MongoDb::Db
19
+ plugin.db.settings[:host].must_equal options["host"]
20
+ plugin.db.settings[:username].must_equal options["username"]
21
+ plugin.db.settings[:password].must_equal options["password"]
22
+ plugin.db.settings[:database].must_equal options["database"]
23
+ plugin.db.settings[:collection].must_equal options["collection"]
24
+ end
25
+
26
+ it "should call backup" do
27
+ create_plugin("valid").backup "/dest_dir"
28
+ end
29
+
30
+ it "should call restore" do
31
+ create_plugin("valid").restore "/from_dir"
32
+ end
33
+
34
+ end
@@ -0,0 +1 @@
1
+ name: value
@@ -0,0 +1,5 @@
1
+ host: "host:post"
2
+ username: user
3
+ password: password
4
+ database: database
5
+ collection: collection
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siba-source-mongo-db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Evgeny Neumerzhitskiy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: siba
16
+ requirement: &75282640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.5'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *75282640
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &75297850 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.10'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *75297850
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &75296690 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.9'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *75296690
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-minitest
49
+ requirement: &75295580 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *75295580
58
+ description: An extension for SIBA backup and restore utility. It allows to backup
59
+ and restore MongoDB database.
60
+ email:
61
+ - sausageskin@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - Guardfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - lib/siba-source-mongo-db.rb
73
+ - lib/siba-source-mongo-db/db.rb
74
+ - lib/siba-source-mongo-db/init.rb
75
+ - lib/siba-source-mongo-db/options.yml
76
+ - lib/siba-source-mongo-db/version.rb
77
+ - siba-source-mongo-db.gemspec
78
+ - test/helper/require_integration.rb
79
+ - test/helper/require_unit.rb
80
+ - test/integration/i9n_db.rb
81
+ - test/unit/test_db.rb
82
+ - test/unit/test_init.rb
83
+ - test/unit/yml/empty.yml
84
+ - test/unit/yml/valid.yml
85
+ homepage: https://github.com/evgenyneu/siba-source-mongo-db
86
+ licenses:
87
+ - MIT
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.11
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: MongoDB backup and restore extention for SIBA utility
110
+ test_files: []