Class: DbBackupTool::Archiver

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

Instance Method Summary collapse

Constructor Details

#initialize(mapper) ⇒ Archiver

Returns a new instance of Archiver.



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

def initialize(mapper)
  @mapper = mapper
end

Instance Method Details

#export(output_directory) ⇒ Object



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

def export(output_directory)
  # Create an archive file
  path = File.join output_directory, "#{Time.now.to_i}.zip"
  puts "output path: #{path}"
  Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |zipfile|
    zipfile.get_output_stream('tables.yml') { |f|
      f.puts Hash[tables.each_with_index.map{|t,i|[i,t.first]}].to_yaml
    }
    tables.each_with_index do |t,i|
      zipfile.get_output_stream("#{i}.csv") { |f|
        DbBackupTool::Dumper.new(t.last).dump(f)
      }
    end
  end
end

#import(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/db_backup_tool/archiver.rb', line 29

def import(path)
  tables = self.tables
  Zip::ZipFile.open(path) do |zipfile|
    table_map = YAML::load zipfile.read('tables.yml')
    table_map.each do |i,tn|
      zipfile.get_input_stream("#{i}.csv"){ |f|
        DbBackupTool::Loader.new(tables[tn]).load(f)
      } if tables.has_key?(tn)
    end
  end
end

#tablesObject



11
# File 'lib/db_backup_tool/archiver.rb', line 11

def tables; @mapper.tables; end