class Backup::Database::MongoDB
Attributes
Additional “mongodump” options
Credentials for the specified database
Connectivity options
IPv6 support (disabled by default)
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 of the database that needs to get dumped
Collections to dump, collections that aren't specified won't get dumped
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.
Credentials for the specified database
Connectivity options
Credentials for the specified database
Public Class Methods
Backup::Database::Base::new
# File lib/backup/database/mongodb.rb, line 52 def initialize(model, database_id = nil, &block) super instance_eval(&block) if block_given? end
Public Instance Methods
Backup::Database::Base#perform!
# File lib/backup/database/mongodb.rb, line 57 def perform! super lock_database if @lock dump! package! ensure unlock_database if @lock end
Private Instance Methods
# File lib/backup/database/mongodb.rb, line 139 def connectivity_options opts = [] opts << "--host='#{ host }'" if host opts << "--port='#{ port }'" if port opts.join(' ') end
# File lib/backup/database/mongodb.rb, line 131 def credential_options opts = [] opts << "--username='#{ username }'" if username opts << "--password='#{ password }'" if password opts << "--authenticationDatabase='#{ authdb }'" if authdb opts.join(' ') end
Performs all required mongodump commands, dumping the output files into the dump_packaging_path
directory for packaging.
# File lib/backup/database/mongodb.rb, line 73 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
# File lib/backup/database/mongodb.rb, line 117 def dump_packaging_path File.join(dump_path, dump_filename) end
# File lib/backup/database/mongodb.rb, line 146 def ipv6_option '--ipv6' if ipv6 end
# File lib/backup/database/mongodb.rb, line 158 def lock_database lock_command = <<-EOS.gsub(/^ +/, '') echo 'use admin db.setProfilingLevel(0) db.fsyncLock()' | #{ mongo_shell } EOS run(lock_command) end
# File lib/backup/database/mongodb.rb, line 177 def mongo_shell cmd = "#{ utility(:mongo) } #{ connectivity_options }".rstrip cmd << " #{ credential_options }".rstrip cmd << " #{ ipv6_option }".rstrip cmd << " '#{ name }'" if name cmd end
# 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
# File lib/backup/database/mongodb.rb, line 127 def name_option "--db='#{ name }'" if name end
# File lib/backup/database/mongodb.rb, line 150 def oplog_option '--oplog' if oplog end
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 93 def package! pipeline = Pipeline.new dump_ext = 'tar' pipeline << "#{ utility(:tar) } -cf - " + "-C '#{ dump_path }' '#{ dump_filename }'" model.compressor.compress_with do |command, ext| pipeline << command dump_ext << ext end if model.compressor 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
# File lib/backup/database/mongodb.rb, line 168 def unlock_database unlock_command = <<-EOS.gsub(/^ +/, '') echo 'use admin db.fsyncUnlock()' | #{ mongo_shell } EOS run(unlock_command) end
# File lib/backup/database/mongodb.rb, line 154 def user_options Array(additional_options).join(' ') end