Class: Backup::Database::MongoDB

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/database/mongodb.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#dump_path, #utility_path

Instance Method Summary collapse

Methods inherited from Base

#log!, #prepare!

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_optionsObject

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
  @additional_options
end

#hostObject

Connectivity options



17
18
19
# File 'lib/backup/database/mongodb.rb', line 17

def host
  @host
end

#ipv6Object

Returns the mongodump syntax for enabling ipv6



21
22
23
# File 'lib/backup/database/mongodb.rb', line 21

def ipv6
  @ipv6
end

#lockObject

‘lock’ dump meaning wrapping mongodump with fsync & lock



33
34
35
# File 'lib/backup/database/mongodb.rb', line 33

def lock
  @lock
end

#nameObject

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_collectionsObject

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

#passwordObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/mongodb.rb', line 13

def password
  @password
end

#portObject

Connectivity options



17
18
19
# File 'lib/backup/database/mongodb.rb', line 17

def port
  @port
end

#usernameObject

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_dumpObject

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_optionsObject

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 connectivity_options
  %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_optionsObject

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 credential_options
  %w[username password].map do |option|
    next if send(option).nil? or send(option).empty?
    "--#{option}='#{send(option)}'"
  end.compact.join("\s")
end

#databaseObject

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_directoryObject

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_databaseObject

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_shellObject

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, credential_options, connectivity_options, ipv6].join(' ')
end

#mongodumpObject

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 } #{ credential_options } " +
  "#{ connectivity_options } #{ ipv6 } #{ additional_options } #{ 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_databaseObject

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