Class: DB2Fog
- Inherits:
-
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
#backup ⇒ Object
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
|
#clean ⇒ Object
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 = []
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)
}
}
files.select {|x| x[:date] >= 1.day.ago }.each do |backup_for_day|
to_keep << backup_for_day
end
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
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 ⇒ Object
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
|