Class: Martilla::Backup
- Inherits:
-
Object
show all
- Extended by:
- Memoist
- Includes:
- Utilities
- Defined in:
- lib/martilla/backup.rb,
lib/martilla/utilities.rb
Defined Under Namespace
Modules: Utilities
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Utilities
#duration_format, #formatted_file_size, #s_to_h, #s_to_m
Constructor Details
#initialize(config) ⇒ Backup
Returns a new instance of Backup.
12
13
14
15
16
17
|
# File 'lib/martilla/backup.rb', line 12
def initialize(config)
@options = config['options'] || {}
@db = Database.create(config['db'])
@storage = Storage.create(config['storage'])
@notifiers = config['notifiers'].map { |c| Notifier.create(c) }.compact
end
|
Instance Attribute Details
#file_size ⇒ Object
Returns the value of attribute file_size.
10
11
12
|
# File 'lib/martilla/backup.rb', line 10
def file_size
@file_size
end
|
Class Method Details
.create(config) ⇒ Object
19
20
21
22
23
|
# File 'lib/martilla/backup.rb', line 19
def self.create(config)
backup = Backup.new(config)
backup.execute
backup
end
|
.sample_config ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/martilla/backup.rb', line 91
def self.sample_config
{
'db' => {
'type' => 'postgres',
'options' => {
'host' => 'localhost',
'user' => 'username',
'password' => 'password',
'db' => 'databasename'
}
},
'storage' => {
'type' => 'local',
'options' => {
'filename' => 'database-backup.sql',
'retention' => 0
}
},
'notifiers' => [
{
'type' => 'none'
}
]
}
end
|
Instance Method Details
#execute ⇒ Object
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
|
# File 'lib/martilla/backup.rb', line 36
def execute
begin
@ticks = [Time.now]
@db.dump(tmp_file: tmp_file, gzip: gzip?)
@file_size = File.size(tmp_file).to_f if File.exist?(tmp_file)
@ticks << Time.now
@storage.persist(tmp_file: tmp_file, gzip: gzip?)
@storage.enfore_retention!(gzip: gzip?)
rescue Exception => e
@notifiers.each do |notifier|
notifier.error(e.message, metadata) if notifier.send_failure?
end
puts "An error occurred: #{e.inspect}"
else
@notifiers.each do |notifier|
notifier.success(metadata) if notifier.send_success?
end
puts "Backup created and persisted successfully"
end
File.delete(tmp_file) if File.exist?(tmp_file)
end
|
#gzip? ⇒ Boolean
25
26
27
28
|
# File 'lib/martilla/backup.rb', line 25
def gzip?
return true if @options['gzip'].nil?
@options['gzip']
end
|
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/martilla/backup.rb', line 64
def metadata
@ticks << Time.now
data = []
if @file_size.nil?
data << "No backup file was created"
else
data << "Total backup size: #{formatted_file_size}"
end
if @ticks.count >= 2
time_diff = duration_format(@ticks[1] - @ticks[0])
data << "Backup 'dump' duration: #{time_diff}"
end
if @ticks.count >= 3
time_diff = duration_format(@ticks[2] - @ticks[1])
data << "Backup storage duration: #{time_diff}"
end
data
end
|
#tmp_file ⇒ Object
30
31
32
33
34
|
# File 'lib/martilla/backup.rb', line 30
def tmp_file
filename = @options.dig('tmp_file') || '/tmp/backup'
return "#{filename}.gz" if gzip?
filename
end
|