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, #rm, #run, #utility

Constructor Details

#initialize(&block) ⇒ MongoDB

Creates a new instance of the MongoDB database object



33
34
35
36
37
38
39
40
41
42
# File 'lib/backup/database/mongodb.rb', line 33

def initialize(&block)
  load_defaults!

  @only_collections   ||= Array.new
  @additional_options ||= Array.new
  @ipv6               ||= 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

#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



73
74
75
# File 'lib/backup/database/mongodb.rb', line 73

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



57
58
59
60
61
62
# File 'lib/backup/database/mongodb.rb', line 57

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



47
48
49
50
51
52
# File 'lib/backup/database/mongodb.rb', line 47

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



79
80
81
# File 'lib/backup/database/mongodb.rb', line 79

def database
  "--db='#{ name }'"
end

#dump!Object

Builds and runs the mongodump command



121
122
123
# File 'lib/backup/database/mongodb.rb', line 121

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>



92
93
94
# File 'lib/backup/database/mongodb.rb', line 92

def dump_directory
  "--out='#{ dump_path }'"
end

#mongodumpObject

Builds the full mongodump string based on all attributes



98
99
100
101
# File 'lib/backup/database/mongodb.rb', line 98

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



109
110
111
112
113
114
115
116
117
# File 'lib/backup/database/mongodb.rb', line 109

def perform!
  log!

  if collections_to_dump.is_a?(Array) and not collections_to_dump.empty?
    specific_collection_dump!
  else
    dump!
  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



129
130
131
132
133
# File 'lib/backup/database/mongodb.rb', line 129

def specific_collection_dump!
  collections_to_dump.each do |collection|
    run("#{mongodump} --collection='#{collection}'")
  end
end