Class: MongodbClone::MongodbReplication

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMongodbReplication

Returns a new instance of MongodbReplication.



9
10
11
# File 'lib/mongodb_clone.rb', line 9

def initialize
  @id = Time.now.utc.strftime('%Y%m%d%H%M%S')
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/mongodb_clone.rb', line 7

def id
  @id
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/mongodb_clone.rb', line 7

def path
  @path
end

Instance Method Details

#dump(environment = 'production', session = 'default') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongodb_clone.rb', line 13

def dump(environment = 'production', session = 'default')
  config = base_config[environment]['sessions'][session]

  params = {
    h: config['hosts'][0],
    d: config['database'],
    u: config['username'],
    p: config['password'],
    o: "/tmp/#{ config['database'] }/#{ @id }"
  }

  a = params.collect { |key, value| "-#{ key } #{ value }" }.join(' ')

  command = "mongodump #{ a }"

  execute(command)

  @path = "#{ params[:o] }/#{ params[:d] }"

  self
end

#restore(environment = 'development', session = 'default') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mongodb_clone.rb', line 35

def restore(environment = 'development', session = 'default')
  config = base_config[environment]['sessions'][session]

  params = {
    h: config['hosts'][0],
    d: config['database'],
    u: config['username'],
    p: config['password']
  }

  a = params.collect { |key, value| "-#{ key } #{ value }" if value }.compact.join(' ')

  command = "mongorestore #{ a } #{ @path }"

  execute(command)
end