Class: Backup::Database::MongoDB
- Defined in:
- lib/backup/database/mongodb.rb
Instance Attribute Summary collapse
-
#additional_options ⇒ Object
Builds a MongoDB compatible string for the additional options specified by the user.
-
#host ⇒ Object
Connectivity options.
-
#ipv6 ⇒ Object
Returns the mongodump syntax for enabling ipv6.
-
#lock ⇒ Object
‘lock’ dump meaning wrapping mongodump with fsync & lock.
-
#name ⇒ Object
Name of the database that needs to get dumped.
-
#only_collections ⇒ Object
Collections to dump, collections that aren’t specified won’t get dumped.
-
#password ⇒ Object
Credentials for the specified database.
-
#port ⇒ Object
Connectivity options.
-
#username ⇒ Object
Credentials for the specified database.
Attributes inherited from Base
Instance Method Summary collapse
-
#collections_to_dump ⇒ Object
Returns an array of collections to dump.
-
#connectivity_options ⇒ Object
Builds the MongoDB connectivity options syntax to connect the user to perform the database dumping process.
-
#credential_options ⇒ Object
Builds the MongoDB credentials syntax to authenticate the user to perform the database dumping process.
-
#database ⇒ Object
Returns the MongoDB database selector syntax.
-
#dump! ⇒ Object
Builds and runs the mongodump command.
-
#dump_directory ⇒ Object
Returns the MongoDB syntax for determining where to output all the database dumps, e.g.
-
#initialize(&block) ⇒ MongoDB
constructor
Creates a new instance of the MongoDB database object.
-
#lock_database ⇒ Object
Locks and FSyncs the database to bring it up to sync and ensure no ‘write operations’ are performed during the dump process.
-
#mongo_shell ⇒ Object
Builds the full mongo string based on all attributes.
-
#mongodump ⇒ Object
Builds the full mongodump string based on all attributes.
-
#perform! ⇒ Object
Performs the mongodump command and outputs the data to the specified path based on the ‘trigger’.
-
#specific_collection_dump! ⇒ Object
For each collection in the @only_collections array, it’ll build the whole ‘mongodump’ command, append the ‘–collection’ option, and run the command built command.
-
#unlock_database ⇒ Object
Unlocks the (locked) database.
Methods inherited from Base
Methods included from Configuration::Helpers
#clear_defaults!, #getter_methods, #load_defaults!, #setter_methods
Methods included from CLI
#mkdir, #raise_if_command_not_found!, #rm, #run, #utility
Constructor Details
#initialize(&block) ⇒ MongoDB
Creates a new instance of the MongoDB database object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/backup/database/mongodb.rb', line 37 def initialize(&block) load_defaults! @only_collections ||= Array.new @additional_options ||= Array.new @ipv6 ||= false @lock ||= false instance_eval(&block) prepare! end |
Instance Attribute Details
#additional_options ⇒ Object
Builds a MongoDB compatible string for the additional options specified by the user
29 30 31 |
# File 'lib/backup/database/mongodb.rb', line 29 def @additional_options end |
#host ⇒ Object
Connectivity options
17 18 19 |
# File 'lib/backup/database/mongodb.rb', line 17 def host @host end |
#ipv6 ⇒ Object
Returns the mongodump syntax for enabling ipv6
21 22 23 |
# File 'lib/backup/database/mongodb.rb', line 21 def ipv6 @ipv6 end |
#lock ⇒ Object
‘lock’ dump meaning wrapping mongodump with fsync & lock
33 34 35 |
# File 'lib/backup/database/mongodb.rb', line 33 def lock @lock end |
#name ⇒ Object
Name of the database that needs to get dumped
9 10 11 |
# File 'lib/backup/database/mongodb.rb', line 9 def name @name end |
#only_collections ⇒ Object
Collections to dump, collections that aren’t specified won’t get dumped
25 26 27 |
# File 'lib/backup/database/mongodb.rb', line 25 def only_collections @only_collections end |
#password ⇒ Object
Credentials for the specified database
13 14 15 |
# File 'lib/backup/database/mongodb.rb', line 13 def password @password end |
#port ⇒ Object
Connectivity options
17 18 19 |
# File 'lib/backup/database/mongodb.rb', line 17 def port @port end |
#username ⇒ Object
Credentials for the specified database
13 14 15 |
# File 'lib/backup/database/mongodb.rb', line 13 def username @username end |
Instance Method Details
#collections_to_dump ⇒ Object
Returns an array of collections to dump
78 79 80 |
# File 'lib/backup/database/mongodb.rb', line 78 def collections_to_dump @only_collections end |
#connectivity_options ⇒ Object
Builds the MongoDB connectivity options syntax to connect the user to perform the database dumping process
62 63 64 65 66 67 |
# File 'lib/backup/database/mongodb.rb', line 62 def %w[host port].map do |option| next if send(option).nil? or (send(option).respond_to?(:empty?) and send(option).empty?) "--#{option}='#{send(option)}'" end.compact.join("\s") end |
#credential_options ⇒ Object
Builds the MongoDB credentials syntax to authenticate the user to perform the database dumping process
52 53 54 55 56 57 |
# File 'lib/backup/database/mongodb.rb', line 52 def %w[username password].map do |option| next if send(option).nil? or send(option).empty? "--#{option}='#{send(option)}'" end.compact.join("\s") end |
#database ⇒ Object
Returns the MongoDB database selector syntax
84 85 86 |
# File 'lib/backup/database/mongodb.rb', line 84 def database "--db='#{ name }'" end |
#dump! ⇒ Object
Builds and runs the mongodump command
133 134 135 |
# File 'lib/backup/database/mongodb.rb', line 133 def dump! run(mongodump) end |
#dump_directory ⇒ Object
Returns the MongoDB syntax for determining where to output all the database dumps, e.g. ~/Backup/.tmp/MongoDB/<databases here>/<database collections>
97 98 99 |
# File 'lib/backup/database/mongodb.rb', line 97 def dump_directory "--out='#{ dump_path }'" end |
#lock_database ⇒ Object
Locks and FSyncs the database to bring it up to sync and ensure no ‘write operations’ are performed during the dump process
157 158 159 160 161 162 163 164 |
# File 'lib/backup/database/mongodb.rb', line 157 def lock_database lock_command = <<-EOS echo 'use admin db.runCommand({"fsync" : 1, "lock" : 1})' | #{mongo_shell} EOS run(lock_command) end |
#mongo_shell ⇒ Object
Builds the full mongo string based on all attributes
149 150 151 |
# File 'lib/backup/database/mongodb.rb', line 149 def mongo_shell [utility(:mongo), database, , , ipv6].join(' ') end |
#mongodump ⇒ Object
Builds the full mongodump string based on all attributes
103 104 105 106 |
# File 'lib/backup/database/mongodb.rb', line 103 def mongodump "#{ utility(:mongodump) } #{ database } #{ } " + "#{ } #{ ipv6 } #{ } #{ dump_directory }" end |
#perform! ⇒ Object
Performs the mongodump command and outputs the data to the specified path based on the ‘trigger’. If the user hasn’t specified any specific collections to dump, it’ll dump everything. If the user has specified collections to dump, it’ll loop through the array of collections and invoke the ‘mongodump’ command once per collection
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/backup/database/mongodb.rb', line 114 def perform! log! begin lock_database if @lock.eql?(true) if collections_to_dump.is_a?(Array) and not collections_to_dump.empty? specific_collection_dump! else dump! end unlock_database if @lock.eql?(true) rescue => exception unlock_database if @lock.eql?(true) raise exception end end |
#specific_collection_dump! ⇒ Object
For each collection in the @only_collections array, it’ll build the whole ‘mongodump’ command, append the ‘–collection’ option, and run the command built command
141 142 143 144 145 |
# File 'lib/backup/database/mongodb.rb', line 141 def specific_collection_dump! collections_to_dump.each do |collection| run("#{mongodump} --collection='#{collection}'") end end |
#unlock_database ⇒ Object
Unlocks the (locked) database
168 169 170 171 172 173 174 175 |
# File 'lib/backup/database/mongodb.rb', line 168 def unlock_database unlock_command = <<-EOS echo 'use admin db.$cmd.sys.unlock.findOne()' | #{mongo_shell} EOS run(unlock_command) end |