Class: S3Archive::DbDump

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/s3archive/db_dump.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(db_config, name = 'dbdump') ⇒ DbDump

Returns a new instance of DbDump.



12
13
14
15
# File 'lib/s3archive/db_dump.rb', line 12

def initialize(db_config, name = 'dbdump')
  @db_config = db_config
  @name = name.gsub(/\s+/, '_')
end

Instance Attribute Details

#db_configObject (readonly)

Returns the value of attribute db_config.



10
11
12
# File 'lib/s3archive/db_dump.rb', line 10

def db_config
  @db_config
end

#dump_pathObject (readonly)

Returns the value of attribute dump_path.



10
11
12
# File 'lib/s3archive/db_dump.rb', line 10

def dump_path
  @dump_path
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/s3archive/db_dump.rb', line 10

def name
  @name
end

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/s3archive/db_dump.rb', line 17

def run
  backup_filename = "#{name}-#{Time.now.utc.strftime("%Y%m%dT%H%M%S")}.sql.gz"
  backup_path = "/tmp/#{backup_filename}"
  logger.info "* Dumping and compressing data to #{backup_path}."
  case db_config['adapter']
  when /^mysql/
    db_host = db_config['host']
    db_name = db_config['database']
    password = "-p#{db_config['password']}" if db_config['password']
    backup_options = "--opt --skip-lock-tables --single-transaction --skip-extended-insert"
    dump_options = "-u #{db_config['username']} #{password} #{backup_options} -h #{db_host} #{db_name}"
    logger.info "** Backing up mysql db #{db_name}@#{db_host}"
    system "mysqldump #{dump_options} | gzip -c > #{backup_path}"
  else
    db_config.delete 'password'
    msg = "Don't know how to dump #{db_config['adapter']} database: #{db_config.inspect}'"
    logger.fatal msg
    raise msg
  end

  @dump_path = backup_path
end