Class: Siba::Source::MongoDb::Db
- Inherits:
-
Object
- Object
- Siba::Source::MongoDb::Db
- Includes:
- FilePlug, LoggerPlug
- Defined in:
- lib/siba-source-mongo-db/db.rb
Constant Summary collapse
- HIDE_PASSWORD_TEXT =
"****p7d****"
Instance Attribute Summary collapse
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #backup(dest_dir) ⇒ Object
- #check_installed ⇒ Object
- #db_and_collection_names ⇒ Object
- #escape_for_shell(str) ⇒ Object
- #get_shell_parameters ⇒ Object
-
#initialize(settings) ⇒ Db
constructor
A new instance of Db.
- #restore(from_dir) ⇒ Object
Constructor Details
#initialize(settings) ⇒ Db
Returns a new instance of Db.
11 12 13 14 |
# File 'lib/siba-source-mongo-db/db.rb', line 11 def initialize(settings) @settings = settings check_installed end |
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
9 10 11 |
# File 'lib/siba-source-mongo-db/db.rb', line 9 def settings @settings end |
Instance Method Details
#backup(dest_dir) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/siba-source-mongo-db/db.rb', line 24 def backup(dest_dir) siba_file.run_this do unless Siba::FileHelper.dir_empty? dest_dir raise Siba::Error, "Failed to backup MongoDB: output directory is not empty: #{dest_dir}" end command_without_password = %(mongodump -o "#{dest_dir}" #{get_shell_parameters}) command = command_without_password unless settings[:password].nil? command = command_without_password.gsub HIDE_PASSWORD_TEXT, settings[:password] end output = siba_file.run_shell command, "failed to backup MongoDb: #{command_without_password}" raise Siba::Error, "failed to backup MongoDb: #{output}" if output =~ /ERROR:/ if Siba::FileHelper.dir_empty?(dest_dir) raise Siba::Error, "Failed to backup MongoDB: dump directory is empty" end Siba::FileHelper.entries(dest_dir).each do |entry| path_to_collection = File.join dest_dir, entry next unless File.directory? path_to_collection if Siba::FileHelper.dir_empty? path_to_collection logger.warn "MongoDB collection/database name '#{entry}' is incorrect or it has no data." end end end end |
#check_installed ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/siba-source-mongo-db/db.rb', line 16 def check_installed siba_file.run_this do raise Siba::Error, "'mongodump' utility is not found. Please install mongodb." unless siba_file.shell_ok? "mongodump --help" raise Siba::Error, "'mongorestore' utility is not found. Please install mongodb." unless siba_file.shell_ok? "mongorestore --help" logger.debug "Mongo backup utilities verified" end end |
#db_and_collection_names ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/siba-source-mongo-db/db.rb', line 90 def db_and_collection_names names = [] names << "db: #{settings[:database]}" unless settings[:database].nil? names << "collection: #{settings[:collection]}" unless settings[:collection].nil? out = names.join(", ") out = " > " + out unless out.empty? out end |
#escape_for_shell(str) ⇒ Object
86 87 88 |
# File 'lib/siba-source-mongo-db/db.rb', line 86 def escape_for_shell(str) str.gsub "\"", "\\\"" end |
#get_shell_parameters ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/siba-source-mongo-db/db.rb', line 76 def get_shell_parameters params = [] params << "-h \"#{escape_for_shell settings[:host]}\"" unless settings[:host].nil? params << "-u \"#{escape_for_shell settings[:username]}\"" unless settings[:username].nil? params << "-p \"#{HIDE_PASSWORD_TEXT}\"" unless settings[:password].nil? params << "-d \"#{escape_for_shell settings[:database]}\"" unless settings[:database].nil? params << "-c \"#{escape_for_shell settings[:collection]}\"" unless settings[:collection].nil? params.join " " end |
#restore(from_dir) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/siba-source-mongo-db/db.rb', line 52 def restore(from_dir) siba_file.run_this do if Siba::FileHelper.dirs_count(from_dir) == 0 raise Siba::Error, "Failed to restore MongoDB: backup directory is empty: #{from_dir}" end unless settings[:database].nil? dirs = Siba::FileHelper.dirs from_dir if dirs.size != 1 raise Siba::Error, "Dump should contain exactly one directory when restoring a single database" end from_dir = File.join from_dir, dirs[0] end command_without_password = %(mongorestore --drop #{get_shell_parameters} "#{from_dir}") command = command_without_password unless settings[:password].nil? command = command_without_password.gsub HIDE_PASSWORD_TEXT, settings[:password] end output = siba_file.run_shell command, "failed to restore MongoDb: #{command_without_password}" raise Siba::Error, "failed to restore MongoDb: #{output}" if output =~ /ERROR:/ end end |