class ExportMongoS3::Db

Constants

SYSTEM_COLLECTIONS

Public Class Methods

new(options) click to toggle source
# File lib/export_mongo_s3/db.rb, line 6
def initialize(options)
  @options            = options
  @connection_options = connection(options)
end

Public Instance Methods

export_db(db, out_path) click to toggle source
# File lib/export_mongo_s3/db.rb, line 14
def export_db(db, out_path)

  collection_settings_map = prepared_collection_settings

  if collection_settings_map.empty?
    collection_names = get_collections(db)
  else
    collection_names = collection_settings_map.keys
  end

  collection_names.each do |collection_name|

    collection_settings = collection_settings_map[collection_name]

    if collection_settings.nil?
      export(db, collection_name, out_path)
    else
      fields = collection_settings[:fields]
      query  = collection_settings[:query]

      export(db, collection_name, out_path, fields, query)
    end
  end

end

Private Instance Methods

connection(options) click to toggle source
# File lib/export_mongo_s3/db.rb, line 42
def connection(options)
  host                    = (options[:host].nil? || options[:host] == '') ? 'localhost' : options[:host]
  port                    = options[:port].nil? ? 27017 : options[:port]
  username                = options[:username]
  password                = options[:password]
  authentication_database = options[:authentication_database]

  auth_options = ''

  unless username.nil? || username == '' || password.nil? || password == ''
    auth_options = "-u '#{username}' -p '#{password}'"
    auth_options << " --authenticationDatabase '#{authentication_database}'" unless authentication_database.nil? || authentication_database == ''
  end

  "--host '#{host}' --port '#{port}' #{auth_options}"
end
export(db, collection, out_path, fields = [], query = nil) click to toggle source
# File lib/export_mongo_s3/db.rb, line 59
def export(db, collection, out_path, fields = [], query = nil)

  if fields.empty?
    fields = get_fields(db, collection)
  end

  return if fields.empty?

  command = 'mongoexport'
  command << " #{@connection_options}"
  command << ' --type=csv'
  command << " --db '#{db}'"
  command << " --collection '#{collection}'"
  command << " --fields '#{fields.join(',')}'"
  command << " --query '#{query}'" unless query.nil?
  command << " --out '#{out_path}/#{collection}.csv'"
  command << ' > /dev/null'

  system(command)
  raise "Error mongoexport '#{db}'" unless $?.exitstatus.zero?
end
get_collections(db) click to toggle source
# File lib/export_mongo_s3/db.rb, line 81
def get_collections(db)
  command = 'mongo'
  command << " #{@connection_options}"
  command << " #{db} --eval 'rs.slaveOk(); db.getCollectionNames();'"
  command << ' --quiet'

  result = %x(#{command})
  raise "Error get collections for db '#{db}'. Msg: #{result}" unless $?.exitstatus.zero?

  result.strip.split(',') - SYSTEM_COLLECTIONS
end
get_fields(db, collection) click to toggle source
# File lib/export_mongo_s3/db.rb, line 93
def get_fields(db, collection)
  command = 'mongo'
  command << " #{@connection_options}"
  command << " #{db} --eval 'rs.slaveOk(); var fields = []; for(var field in db.#{collection}.find().sort({_id: -1}).limit(1)[0]) { fields.push(field); }; fields;'"
  command << ' --quiet'

  result = %x(#{command})
  raise "Error get fields for db '#{db}' and collection '#{collection}'. Msg: #{result}" unless $?.exitstatus.zero?

  result.strip.split(',')
end
prepared_collection_settings() click to toggle source
# File lib/export_mongo_s3/db.rb, line 105
def prepared_collection_settings
  @prepared_collection_settings if @prepared_collection_settings

  collection_settings = @options[:collections]

  prepared_settings = {}

  if collection_settings.is_a?(Array)
    collection_settings.each do |collection|

      name  = collection['name']

      if name.nil? || name == ''
        raise "Not valid param <name: #{collection}>"
      end

      query = collection['query'].nil? || collection['query'] == '' ? nil : collection['query'].to_s

      fields = if collection['fields'].is_a?(String)
                 collection['fields'].split(',').each { |field| field.strip! }
               else
                 []
               end

      prepared_settings[name] =
          {
              name:   name,
              fields: fields,
              query:  query
          }
    end
  end

  @prepared_collection_settings = prepared_settings
end