Class: DB2Fog

Inherits:
Object
  • Object
show all
Defined in:
lib/db2fog.rb,
lib/db2fog/railtie.rb

Defined Under Namespace

Classes: FogStore, MysqlAdaptor, PsqlAdaptor, Railtie

Instance Method Summary collapse

Instance Method Details

#backupObject



13
14
15
16
17
# File 'lib/db2fog.rb', line 13

def backup
  file_name = "dump-#{db_credentials[:database]}-#{Time.now.utc.strftime("%Y%m%d%H%M")}.sql.gz"
  store.store(file_name, open(database.dump))
  store.store(most_recent_dump_file_name, file_name)
end

#cleanObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/db2fog.rb', line 25

def clean
  to_keep = []
  # only consider files that belong to db2fog. Other files are ignored
  filelist = store.list.select {|file|
    file.include?(db_credentials[:database]) && file.match(/\d{12}.sql.gz\Z/)
  }
  files = filelist.map { |file|
    {
      :path => file,
      :date => Time.parse(file.split('-').last.split('.').first)
    }
  }
  # Keep all backups from the past day
  files.select {|x| x[:date] >= 1.day.ago }.each do |backup_for_day|
    to_keep << backup_for_day
  end

  # Keep one backup per day from the last week
  files.select {|x| x[:date] >= 1.week.ago }.group_by {|x| x[:date].strftime("%Y%m%d") }.values.each do |backups_for_last_week|
    to_keep << backups_for_last_week.sort_by{|x| x[:date].strftime("%Y%m%d") }.first
  end

  # Keep one backup per week since forever
  files.group_by {|x| x[:date].strftime("%Y%W") }.values.each do |backups_for_week|
    to_keep << backups_for_week.sort_by{|x| x[:date].strftime("%Y%m%d") }.first
  end

  to_destroy = filelist - to_keep.uniq.collect {|x| x[:path] }
  to_destroy.each do |file|
    store.delete(file.split('/').last)
  end
end

#restoreObject



19
20
21
22
23
# File 'lib/db2fog.rb', line 19

def restore
  dump_file_name = store.fetch(most_recent_dump_file_name).read
  file = store.fetch(dump_file_name)
  database.restore(file.path)
end