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.



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

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.



3
4
5
# File 'lib/mongo_oplog_backup/config.rb', line 3

def options
  @options
end

Instance Method Details

#backup_dirObject



25
26
27
# File 'lib/mongo_oplog_backup/config.rb', line 25

def backup_dir
  options[:dir]
end

#command_line_optionsObject



29
30
31
32
33
34
35
36
# File 'lib/mongo_oplog_backup/config.rb', line 29

def command_line_options
  ssl = options[:ssl] ? '--ssl ' : ''
  host = options[:host] ? "--host #{options[:host].strip} " : ''
  port = options[:port] ? "--port #{options[:port].strip} " : ''
  username = options[:username] ? "--username #{options[:username].strip} " : ''
  password = options[:password] ? "--password #{options[:password].strip} " : ''
  "#{host}#{port}#{ssl}#{username}#{password}"
end

#exec(cmd) ⇒ Object



54
55
56
57
# File 'lib/mongo_oplog_backup/config.rb', line 54

def exec(cmd)
  MongoOplogBackup.log.debug ">>> #{cmd}"
  `#{cmd}`
end

#from_file(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mongo_oplog_backup/config.rb', line 11

def from_file file
  options = {}
  unless file.nil?
    conf = YAML.load_file(file)
    options[:ssl] = conf["ssl"] unless conf["ssl"].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

#lock_fileObject



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

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

#mongo(db, script) ⇒ Object



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

def mongo(db, script)
  exec("mongo #{command_line_options} --quiet --norc #{db} #{script}")
end

#mongodump(args) ⇒ Object



59
60
61
# File 'lib/mongo_oplog_backup/config.rb', line 59

def mongodump(args)
  MongoOplogBackup.log.info exec("mongodump #{command_line_options} #{args}")
end

#oplog_dumpObject



42
43
44
# File 'lib/mongo_oplog_backup/config.rb', line 42

def oplog_dump
  File.join(oplog_dump_folder, 'local/oplog.rs.bson')
end

#oplog_dump_folderObject



38
39
40
# File 'lib/mongo_oplog_backup/config.rb', line 38

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

#state_fileObject



46
47
48
# File 'lib/mongo_oplog_backup/config.rb', line 46

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