-
Notifications
You must be signed in to change notification settings - Fork 71
/
foreman_db.rb
73 lines (65 loc) · 2.3 KB
/
foreman_db.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Procedures::Backup
module Offline
class ForemanDB < ForemanMaintain::Procedure
metadata do
description 'Backup Foreman DB offline'
tags :backup
label :backup_offline_foreman_db
for_feature :foreman_database
preparation_steps { Checks::Foreman::DBUp.new unless feature(:foreman_database).local? }
param :backup_dir, 'Directory where to backup to', :required => true
param :tar_volume_size, 'Size of tar volume (indicates splitting)'
end
def run
if feature(:foreman_database).local?
if File.exist?(pg_backup_file)
puts 'Already done'
else
local_backup
end
else
puts "Backup of #{pg_data_dirs.join(',')} is not supported for remote databases." \
' Doing postgres dump instead...'
with_spinner('Getting Foreman DB dump') do
feature(:foreman_database).dump_db(File.join(@backup_dir, 'foreman.dump'))
end
end
end
private
def local_backup
with_spinner("Collecting data from #{pg_data_dirs.join(',')}") do
pg_data_dirs.each_with_index do |pg_dir, index|
do_backup(pg_dir, (index == 0) ? 'create' : 'append')
end
end
end
def do_backup(pg_dir, cmd)
restore_dir = el? ? feature(:foreman_database).data_dir : pg_dir
feature(:foreman_database).backup_local(
pg_backup_file,
:listed_incremental => File.join(@backup_dir, '.postgres.snar'),
:volume_size => @tar_volume_size,
:data_dir => pg_dir,
:restore_dir => restore_dir,
:command => cmd
)
end
def pg_backup_file
File.join(@backup_dir, 'pgsql_data.tar')
end
def pg_data_dirs
el? ? [pg_data_dir_el] : pg_data_dirs_deb
end
def pg_data_dirs_deb
# The Debian based OSes support multiple installations of Postgresql
# There could be situations where Foreman db is either of these versions
# To be sure we backup the system correctly without missing anything
# we backup all of the Postgresql dirs
feature(:foreman_database).data_dir
end
def pg_data_dir_el
feature(:foreman_database).data_dir
end
end
end
end