Class: MongoOplogBackup::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_oplog_backup/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.


7
8
9
10
11
# File 'lib/mongo_oplog_backup/config.rb', line 7

def initialize(options)
  config_file = options.delete(:file)
  # Command line options take precedence
  @options = from_file(config_file).merge(options) 
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.


5
6
7
# File 'lib/mongo_oplog_backup/config.rb', line 5

def options
  @options
end

Instance Method Details

#backup_dirObject


30
31
32
# File 'lib/mongo_oplog_backup/config.rb', line 30

def backup_dir
  options[:dir]
end

#command_line_optionsObject


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mongo_oplog_backup/config.rb', line 38

def command_line_options
  args = []
  args << '--ssl' if options[:ssl]
  args << '--sslAllowInvalidCertificates' if options[:sslAllowInvalidCertificates]
  [:host, :port, :username, :password, :sslCAFile].each do |option|
    args += ["--#{option}", options[option].strip] if options[option]
  end

  args += ['--authenticationDatabase', 'admin'] if options[:username]

  args
end

#command_string(cmd) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mongo_oplog_backup/config.rb', line 84

def command_string(cmd)
  previous = nil
  filtered = cmd.map do |token|
    pwd = (previous == '--password')
    previous = token
    if pwd
      '***'
    else
      token
    end
  end
  filtered.shelljoin
end

#exec(cmd) ⇒ Object


71
72
73
74
# File 'lib/mongo_oplog_backup/config.rb', line 71

def exec(cmd)
  MongoOplogBackup.log.debug ">>> #{command_string(cmd)}"
  Command.execute(cmd)
end

#from_file(file) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mongo_oplog_backup/config.rb', line 13

def from_file file
  options = {}
  unless file.nil?
    conf = YAML.load_file(file)
    options[:gzip] = conf["gzip"] unless conf["gzip"].nil?
    options[:ssl] = conf["ssl"] unless conf["ssl"].nil?
    options[:sslAllowInvalidCertificates] = conf["sslAllowInvalidCertificates"] unless conf["sslAllowInvalidCertificates"].nil?
    options[:sslCAFile] = conf["sslCAFile"] unless conf["sslCAFile"].nil?
    options[:host] = conf["host"] unless conf["host"].nil?
    options[:port] = conf["port"].to_s unless conf["port"].nil?
    options[:username] = conf["username"] unless conf["username"].nil?
    options[:password] = conf["password"] unless conf["password"].nil?
  end

  options
end

#global_lock_fileObject


67
68
69
# File 'lib/mongo_oplog_backup/config.rb', line 67

def global_lock_file
  File.join(backup_dir, 'backup.lock')
end

#global_state_fileObject


63
64
65
# File 'lib/mongo_oplog_backup/config.rb', line 63

def global_state_file
  File.join(backup_dir, 'backup.json')
end

#mongo(db, script) ⇒ Object


80
81
82
# File 'lib/mongo_oplog_backup/config.rb', line 80

def mongo(db, script)
  exec(['mongo'] + command_line_options + ['--quiet', '--norc', script])
end

#mongodump(*args) ⇒ Object


76
77
78
# File 'lib/mongo_oplog_backup/config.rb', line 76

def mongodump(*args)
  exec(['mongodump'] + command_line_options + args.flatten)
end

#oplog_dumpObject


55
56
57
58
59
60
61
# File 'lib/mongo_oplog_backup/config.rb', line 55

def oplog_dump
  if use_compression?
    File.join(oplog_dump_folder, 'local/oplog.rs.bson.gz')
  else
    File.join(oplog_dump_folder, 'local/oplog.rs.bson')
  end
end

#oplog_dump_folderObject


51
52
53
# File 'lib/mongo_oplog_backup/config.rb', line 51

def oplog_dump_folder
  File.join(backup_dir, 'tmp-dump')
end

#use_compression?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/mongo_oplog_backup/config.rb', line 34

def use_compression?
  !!options[:gzip]
end