This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
/
katello-backup
executable file
·203 lines (171 loc) · 5.62 KB
/
katello-backup
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env ruby
require 'optparse'
require 'fileutils'
require 'date'
require "yaml"
@options = {}
@dir = nil
EXCLUDED="--exclude goferd,foreman-proxy,squid,smart_proxy_dynflow_core,qdrouterd,qpidd"
optparse = OptionParser.new do |opts|
opts.banner = "Usage: katello-backup /path/to/dir [options]\n eg: $ katello-backup /tmp/katello-backup"
opts.on("--skip-pulp-content", "Create backup without Pulp content for debugging only") do |config_only|
@options[:config_only] = config_only
end
opts.on("--incremental [PREVIOUS_BACKUP_DIR]", String, "Backup changes since previous backup") do |dir_path|
opts.abort("Please specify the previous backup directory.") unless dir_path
if File.directory?(dir_path)
dir_path.chop! if dir_path.end_with? "/"
@options[:incremental] = dir_path
else
opts.abort("Previous backup directory does not exist: #{dir_path}")
end
end
opts.on("--online-backup", "Keep services online during backup") do |online|
@options[:online] = online
end
opts.on("--logical-db-backup", "Also dump full database schema during offline backup") do |logical|
@options[:logical_backup] = logical
end
opts.on("--preserve-directory", "Do not create a time-stamped subdirectory") do |no_subdir|
@options[:no_subdir] = no_subdir
end
opts.parse!
if ARGV.length == 0
opts.abort("**** ERROR: Please specify an export directory ****")
elsif ARGV.length != 1
puts opts
exit(-1)
end
@dir = ARGV[0].dup
end
def run_cmd(command, exit_codes=[0])
`#{command}`
unless exit_codes.include?($?.exitstatus)
STDERR.puts "Failed '#{command}'"
exit(-1)
end
end
def create_directories(directory)
@dir = File.join directory, "katello-backup-" + DateTime.now.strftime('%Y%m%d%H%M%S') unless @options[:no_subdir]
puts "Creating backup folder #{@dir}"
FileUtils.mkdir_p @dir
FileUtils.chown_R nil, 'postgres', directory
FileUtils.chmod_R 0770, directory
end
def backup_db_online
puts "Backing up postgres online schema... "
run_cmd("runuser - postgres -c 'pg_dumpall -g > #{@dir}/pg_globals.dump'")
run_cmd("runuser - postgres -c 'pg_dump -Fc foreman > #{@dir}/foreman.dump'")
run_cmd("runuser - postgres -c 'pg_dump -Fc candlepin > #{@dir}/candlepin.dump'")
puts "Done."
puts "Backing up mongo online schema... "
run_cmd('mongodump --host localhost --out mongo_dump')
puts "Done."
end
def backup_pulp_online
puts "Backing up Pulp data... "
matching = false
until matching
checksum1 = `find /var/lib/pulp -printf '%T@\n' | md5sum`
create_pulp_data_tar
checksum2 = `find /var/lib/pulp -printf '%T@\n' | md5sum`
matching = (checksum1 == checksum2)
end
puts "Done."
end
def backup_db_offline
puts "Backing up postgres db... "
run_cmd('tar --selinux --create --file=pgsql_data.tar --listed-incremental=.postgres.snar /var/lib/pgsql/data/')
puts "Done."
puts "Backing up mongo db... "
run_cmd('tar --selinux --create --file=mongo_data.tar --listed-incremental=.mongo.snar --exclude=mongod.lock /var/lib/mongodb/')
puts "Done."
end
def backup_pulp_offline
puts "Backing up Pulp data... "
create_pulp_data_tar
puts "Done."
end
def create_pulp_data_tar
run_cmd('tar --selinux --create --file=pulp_data.tar --exclude=/var/lib/pulp/katello-export --listed-incremental=.pulp.snar /var/lib/pulp/')
end
def compress_files
psql = spawn('gzip', 'pgsql_data.tar')
mongo = spawn('gzip', 'mongo_data.tar')
Process.wait(psql)
Process.wait(mongo)
end
def generate_metadata
os_version = `cat /etc/redhat-release`.chomp
plugin_list = `foreman-rake plugin:list | grep 'Foreman plugin: '`.lines
plugins = []
plugin_list.each do |line|
plugin = line.split
plugins << "#{plugin[2].chop}-#{plugin[3].chop}"
end
rpms = `rpm -qa`.split("\n")
system_facts = {os_version: os_version, plugin_list: plugins, rpms: rpms}
File.open('metadata.yml', 'w') do |metadata_file|
metadata_file.puts system_facts.to_yaml
end
end
if @dir.nil?
puts "**** ERROR: Please specify an export directory ****"
puts optparse
exit(-1)
else
puts "Starting backup: #{Time.now}"
create_directories(@dir.dup)
FileUtils.cd @dir
CONFIGS=[
'/etc/candlepin',
'/etc/foreman',
'/etc/foreman-proxy',
'/etc/hammer',
'/etc/httpd',
'/etc/foreman-installer',
'/etc/pki/katello',
'/etc/pki/katello-certs-tools',
'/etc/pki/pulp',
'/etc/pulp',
'/etc/puppet',
'/etc/puppetlabs',
'/etc/qpid',
'/etc/qpid-dispatch',
'/etc/sysconfig/tomcat*',
'/etc/tomcat*',
'/root/ssl-build',
'/var/lib/candlepin',
'/var/www/html/pub',
'/opt/puppetlabs/puppet/cache/foreman_cache_data',
'/opt/puppetlabs/puppet/ssl/',
'/var/lib/puppet/foreman_cache_data',
'/var/lib/puppet/ssl'
]
puts "Generating metadata ... "
generate_metadata
puts "Done."
if @options[:incremental]
FileUtils.cp Dir.glob(File.join(@options[:incremental], '.*.snar')), @dir
elsif @options[:no_subdir]
FileUtils.rm_rf Dir.glob(File.join(@dir, '.*.snar'))
end
puts "Backing up config files... "
run_cmd("tar --selinux --create --gzip --file=config_files.tar.gz --listed-incremental=.config.snar #{CONFIGS.join(' ')} 2>/dev/null", [0,2])
puts "Done."
if @options[:online]
backup_db_online
backup_pulp_online unless @options[:config_only]
else
if @options[:logical_backup]
backup_db_online
end
`katello-service stop #{EXCLUDED}`
backup_db_offline
backup_pulp_offline unless @options[:config_only]
`katello-service start #{EXCLUDED}`
compress_files
end
puts "Done with backup: #{Time.now}"
puts "**** BACKUP Complete, contents can be found in: #{@dir} ****"
end