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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/db2fog.rb', line 36

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

#restore(source_database_name = nil) ⇒ Object

download the latest backup and restore it to the current database.

**** CAUTION **** The database for the current RAILS_ENV will be emptied and all tables will be recreated.

The database you’re restoring from and restoring to may have a different names (ie. restoring mydb_production into mydb_development). If that’s the case, you should provide the source database name (ie. mydb_production) as an argument.



29
30
31
32
33
34
# File 'lib/db2fog.rb', line 29

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