class Backup::Database::MongoDB

Attributes

additional_options[RW]

Additional “mongodump” options

authdb[RW]

Credentials for the specified database

host[RW]

Connectivity options

ipv6[RW]

IPv6 support (disabled by default)

lock[RW]

Forces mongod to flush all pending write operations to the disk and locks the entire mongod instance to prevent additional writes until the dump is complete.

Note that if Profiling is enabled, this will disable it and will not re-enable it after the dump is complete.

name[RW]

Name of the database that needs to get dumped

only_collections[RW]

Collections to dump, collections that aren’t specified won’t get dumped

oplog[RW]

Creates a dump of the database that includes an oplog, to create a point-in-time snapshot of the state of a mongod instance.

If this option is used, you would not use the ‘lock` option.

This will only work against nodes that maintain a oplog. This includes all members of a replica set, as well as master nodes in master/slave replication deployments.

password[RW]

Credentials for the specified database

port[RW]

Connectivity options

username[RW]

Credentials for the specified database

Public Class Methods

new(model, database_id = nil, &block) click to toggle source
Calls superclass method Backup::Database::Base::new
# File lib/backup/database/mongodb.rb, line 50
def initialize(model, database_id = nil, &block)
  super
  instance_eval(&block) if block_given?
end

Public Instance Methods

perform!() click to toggle source
Calls superclass method Backup::Database::Base#perform!
# File lib/backup/database/mongodb.rb, line 55
def perform!
  super

  lock_database if @lock
  dump!
  package!

ensure
  unlock_database if @lock
end

Private Instance Methods

connectivity_options() click to toggle source
# File lib/backup/database/mongodb.rb, line 140
def connectivity_options
  opts = []
  opts << "--host='#{host}'" if host
  opts << "--port='#{port}'" if port
  opts.join(" ")
end
credential_options() click to toggle source
# File lib/backup/database/mongodb.rb, line 132
def credential_options
  opts = []
  opts << "--username='#{username}'" if username
  opts << "--password='#{password}'" if password
  opts << "--authenticationDatabase='#{authdb}'" if authdb
  opts.join(" ")
end
dump!() click to toggle source

Performs all required mongodump commands, dumping the output files into the dump_packaging_path directory for packaging.

# File lib/backup/database/mongodb.rb, line 71
def dump!
  FileUtils.mkdir_p dump_packaging_path

  collections = Array(only_collections)
  if collections.empty?
    run(mongodump)
  else
    collections.each do |collection|
      run("#{mongodump} --collection='#{collection}'")
    end
  end
end
dump_packaging_path() click to toggle source
# File lib/backup/database/mongodb.rb, line 117
def dump_packaging_path
  File.join(dump_path, dump_filename)
end
ipv6_option() click to toggle source
# File lib/backup/database/mongodb.rb, line 147
def ipv6_option
  "--ipv6" if ipv6
end
lock_database() click to toggle source
# File lib/backup/database/mongodb.rb, line 159
      def lock_database
        lock_command = <<-EOS.gsub(/^ +/, "")
          echo 'use admin
          db.setProfilingLevel(0)
          db.fsyncLock()' | #{mongo_shell}
        EOS

        run(lock_command)
      end
mongo_shell() click to toggle source
# File lib/backup/database/mongodb.rb, line 178
def mongo_shell
  cmd = "#{utility(:mongo)} #{connectivity_options}".rstrip
  cmd << " #{credential_options}".rstrip
  cmd << " #{ipv6_option}".rstrip
  cmd << " '#{name}'" if name
  cmd
end
mongodump() click to toggle source
# File lib/backup/database/mongodb.rb, line 121
def mongodump
  "#{utility(:mongodump)} #{name_option} #{credential_options} " \
    "#{connectivity_options} #{ipv6_option} #{oplog_option} " \
    "#{user_options} --out='#{dump_packaging_path}'"
end
name_option() click to toggle source
# File lib/backup/database/mongodb.rb, line 127
def name_option
  return unless name
  "--db='#{name}'"
end
oplog_option() click to toggle source
# File lib/backup/database/mongodb.rb, line 151
def oplog_option
  "--oplog" if oplog
end
package!() click to toggle source

Creates a tar archive of the dump_packaging_path directory and stores it in the dump_path using dump_filename.

<trigger>/databases/MongoDB[-<database_id>].tar[.gz]

If successful, dump_packaging_path is removed.

# File lib/backup/database/mongodb.rb, line 91
def package!
  pipeline = Pipeline.new
  dump_ext = "tar"

  pipeline << "#{utility(:tar)} -cf - " \
      "-C '#{dump_path}' '#{dump_filename}'"

  if model.compressor
    model.compressor.compress_with do |command, ext|
      pipeline << command
      dump_ext << ext
    end
  end

  pipeline << "#{utility(:cat)} > " \
    "'#{File.join(dump_path, dump_filename)}.#{dump_ext}'"

  pipeline.run
  if pipeline.success?
    FileUtils.rm_rf dump_packaging_path
    log!(:finished)
  else
    raise Error, "Dump Failed!\n#{pipeline.error_messages}"
  end
end
unlock_database() click to toggle source
# File lib/backup/database/mongodb.rb, line 169
      def unlock_database
        unlock_command = <<-EOS.gsub(/^ +/, "")
          echo 'use admin
          db.fsyncUnlock()' | #{mongo_shell}
        EOS

        run(unlock_command)
      end
user_options() click to toggle source
# File lib/backup/database/mongodb.rb, line 155
def user_options
  Array(additional_options).join(" ")
end