Class: SqlDumper

Inherits:
Object
  • Object
show all
Defined in:
app/models/sql_dumper.rb

Constant Summary collapse

DUMP_DIR =
'/tmp/mysql-backup'
DUMP_FILE_REGEX =
/#{DUMP_DIR}\/dump_[0-9._]{17}\.sql/
ARCHIVE_FILE_REGEX =
/#{DUMP_DIR}\/dump_[0-9._]{17}\.tar\.gz/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSqlDumper

Returns a new instance of SqlDumper.



30
31
32
33
# File 'app/models/sql_dumper.rb', line 30

def initialize
  return self if load_db_config and prepare_dump_paths
  nil
end

Instance Attribute Details

#archive_fileObject (readonly)

Returns the value of attribute archive_file.



6
7
8
# File 'app/models/sql_dumper.rb', line 6

def archive_file
  @archive_file
end

#dump_fileObject (readonly)

Returns the value of attribute dump_file.



7
8
9
# File 'app/models/sql_dumper.rb', line 7

def dump_file
  @dump_file
end

Instance Method Details

#clean!Object



9
10
11
12
13
14
# File 'app/models/sql_dumper.rb', line 9

def clean!
  FileUtils.rm_rf @temp_dir
  true
rescue
  false
end

#dump!Object



16
17
18
19
20
21
22
23
24
# File 'app/models/sql_dumper.rb', line 16

def dump!
  FileUtils.mkdir_p @temp_dir
  system dump_command
  if File.exists? @dump_file
    # temporary changes pwd
    Dir.chdir(@temp_dir) { |path| system tar_command }
    return File.exists?(@archive_file) ? @archive_file : nil
  end
end

#fake_rails_envObject



26
27
28
# File 'app/models/sql_dumper.rb', line 26

def fake_rails_env
  { database: '', username: '', password: '' }
end

#rails_envObject



39
40
41
# File 'app/models/sql_dumper.rb', line 39

def rails_env
  rails_env ||= defined?(Rails) ? Rails.env : fake_rails_env
end

#rails_rootObject



35
36
37
# File 'app/models/sql_dumper.rb', line 35

def rails_root
  defined?(Rails) ? Rails.root : '/tmp'
end