class DB2Fog

Public Instance Methods

backup() click to toggle source
   # File lib/db2fog.rb
13 def backup
14   file_name = "dump-#{db_credentials[:database]}-#{Time.now.utc.strftime("%Y%m%d%H%M")}.sql.gz"
15   store.store(file_name, open(database.dump))
16   store.store(most_recent_dump_file_name, file_name)
17 end
clean() click to toggle source
   # File lib/db2fog.rb
25 def clean
26   to_keep = []
27   # only consider files that belong to db2fog. Other files are ignored
28   filelist = store.list.select {|file|
29     file.include?(db_credentials[:database]) && file.match(/\d{12}.sql.gz\Z/)
30   }
31   files = filelist.map { |file|
32     {
33       :path => file,
34       :date => Time.parse(file.split('-').last.split('.').first)
35     }
36   }
37   # Keep all backups from the past day
38   files.select {|x| x[:date] >= 1.day.ago }.each do |backup_for_day|
39     to_keep << backup_for_day
40   end
41 
42   # Keep one backup per day from the last week
43   files.select {|x| x[:date] >= 1.week.ago }.group_by {|x| x[:date].strftime("%Y%m%d") }.values.each do |backups_for_last_week|
44     to_keep << backups_for_last_week.sort_by{|x| x[:date].strftime("%Y%m%d") }.first
45   end
46 
47   # Keep one backup per week since forever
48   files.group_by {|x| x[:date].strftime("%Y%W") }.values.each do |backups_for_week|
49     to_keep << backups_for_week.sort_by{|x| x[:date].strftime("%Y%m%d") }.first
50   end
51 
52   to_destroy = filelist - to_keep.uniq.collect {|x| x[:path] }
53   to_destroy.each do |file|
54     store.delete(file.split('/').last)
55   end
56 end
restore() click to toggle source
   # File lib/db2fog.rb
19 def restore
20   dump_file_name = store.fetch(most_recent_dump_file_name).read
21   file = store.fetch(dump_file_name)
22   database.restore(file.path)
23 end

Private Instance Methods

database() click to toggle source
   # File lib/db2fog.rb
80 def database
81   @database ||= case db_credentials[:adapter]
82                 when /mysql/    then MysqlAdaptor.new(db_credentials)
83                 when /postgres/ || /postgis/ then PsqlAdaptor.new(db_credentials)
84                 else
85                   raise "database adaptor '#{db_credentials[:adapter]}' not supported"
86                 end
87 end
db_credentials() click to toggle source
   # File lib/db2fog.rb
68 def db_credentials
69   if Object.const_defined?(:ActiveRecord)
70     ActiveRecord::Base.connection.instance_eval { @config } # Dodgy!
71   elsif Object.const_defined?(:DataMapper)
72     DataMapper.repository.adapter.options.inject({}){|m,(k,v)| m[k.to_sym] = v;m }
73   elsif Object.const_defined?(:Sequel)
74     opts = Sequel::DATABASES.first.opts
75     opts[:username] = opts[:user]
76     opts
77   end
78 end
most_recent_dump_file_name() click to toggle source
   # File lib/db2fog.rb
64 def most_recent_dump_file_name
65   "most-recent-dump-#{db_credentials[:database]}.txt"
66 end
store() click to toggle source
   # File lib/db2fog.rb
60 def store
61   @store ||= FogStore.new
62 end