class Siba::Source::MongoDb::Db

Constants

HIDE_PASSWORD_TEXT

Attributes

settings[RW]

Public Class Methods

new(settings) click to toggle source
# File lib/siba-source-mongo-db/db.rb, line 11
def initialize(settings)
  @settings = settings
  check_installed
end

Public Instance Methods

backup(dest_dir) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# File lib/siba-source-mongo-db/db.rb, line 86
def escape_for_shell(str)
  str.gsub "\"", "\\\""
end
get_shell_parameters() click to toggle source
# 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) click to toggle source
# 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