Sha256: e630264d0d8cf2250628f5a81eed34d9c21698bb378ffaf5636afe5987d4cf36

Contents?: true

Size: 1.38 KB

Versions: 17

Compression:

Stored size: 1.38 KB

Contents

module Dumper
  module Database
    class MongoDB < Base
      DUMP_TOOL = 'mongodump'
      FILE_EXT = 'tar.gz'

      def command
        "cd #{tmpdir} && #{dump_tool_path} #{connection_options} #{additional_options} && tar -czf #{filename} --exclude='#{filename}' ."
      end

      def connection_options
        [ :database, :host, :port, :username, :password ].map do |option|
          next if @config[option].blank?
          "--#{option}='#{@config[option]}'".gsub('--database', '--db')
        end.compact.join(' ')
      end

      def additional_options
        "--out='#{tmpdir}'"
      end

      def set_config_for(rails_env=nil)
        return unless defined?(Mongo::DB) &&
          (mongo = find_instance_in_object_space(Mongo::DB)) ||
          defined?(Mongoid) && Mongoid::Config.respond_to?(:sessions) &&
          (mongoid = Mongoid::Config.sessions[:default])

        @config = {
          :host => mongo ? mongo.connection.host : mongoid[:hosts].first.split(/:/).first,
          :port => mongo ? mongo.connection.port : mongoid[:hosts].first.split(/:/).last,
          :database => mongo ? mongo.name : mongoid[:database],
          :dump_tool => dump_tool_path
        }.tap do |h|
          if auth = mongo ? mongo.connection.auths.first : mongoid
            h.update(:username => auth['username'], :password => auth['password'])
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
dumper-1.5.0 lib/dumper/database/mongodb.rb
dumper-1.4.5 lib/dumper/database/mongodb.rb
dumper-1.4.4 lib/dumper/database/mongodb.rb
dumper-1.4.3 lib/dumper/database/mongodb.rb
dumper-1.4.2 lib/dumper/database/mongodb.rb
dumper-1.4.1 lib/dumper/database/mongodb.rb
dumper-1.4.0 lib/dumper/database/mongodb.rb
dumper-1.3.7 lib/dumper/database/mongodb.rb
dumper-1.3.6 lib/dumper/database/mongodb.rb
dumper-1.3.5 lib/dumper/database/mongodb.rb
dumper-1.3.4 lib/dumper/database/mongodb.rb
dumper-1.3.3 lib/dumper/database/mongodb.rb
dumper-1.3.2 lib/dumper/database/mongodb.rb
dumper-1.3.1 lib/dumper/database/mongodb.rb
dumper-1.3.0 lib/dumper/database/mongodb.rb
dumper-1.2.1 lib/dumper/database/mongodb.rb
dumper-1.2.0 lib/dumper/database/mongodb.rb