forked from talorh/satellite-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsatellite_backup.sh
415 lines (368 loc) · 11.6 KB
/
satellite_backup.sh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/bin/bash
#set -xv
#####################################################################################
#
# Script Name:
# satellite_backup.sh
#
# Purpose:
# Performs a backup of Red Hat Satellite Server to a local or remote mounted
# directory. This script is to be used in an environment where weekly full
# and daily incremental backups are taken. Rentention is goverened by the
# RETENTION variable at the top of the script.
#
# Dependencies:
# Red Hat Satellite 6.2 - katello-backup must be available.
# Red Hat Satellite 6.3 - satellite-backup must be available
# Red Hat Satellite 6.4 - foreman-maintain must be available
# Packages mutt, gzip must be installed and the server must be able to send mail.
#
# Disclaimer:
# This script is NOT SUPPORTED by Red Hat Global Support Service.
#
# Modification History:
# v1.0 - 15/03/2018 - Initial Version - Talor Holloway (Red Hat Consulting)
# v1.1 - 30/08/2018 - peterceeau
# Use mutt for mail, find it easier to work with.
# v1.2 - 12/12/2018 - peterceeau
# Satellite 6.4 deprecated both katello-backup and satellite-backup in favour
# of foreman-maintain backup.
# Add case statement for 6.4 BACKUP_UTILITY. Change order of command line
# options, original command failed with "ERROR: too many arguments".
# New foreman-maintain command has a spinning progress counter which is
# filling the logfile with lines and lines of output, add grep log before
# attaching to email.
# v1.3 - 13/12/2018 - peterceeau
# Replace grep with sed to improve cleaning up the log before attaching
# to email. First sed removes the multiple lines generated by the spinning
# progress counter. Second sed strips out the ANSI colour codes.
#
# Called by:
# root crontab
#
#####################################################################################
RETENTION=31 # how long to keep backups for
TAG=$(date '+%Y%m%d-%H%M')
LOCKFILE=/tmp/.$(basename $0).lck
LOGDIR=/var/log/satellite_backup
LOG=${LOGDIR}/satellite_backup.log
SKIP_OPTIONS=""
SCRIPTNAME=${0##*/}
MSGS=true
ERRS=true
RV=0
MAIL=/usr/bin/mutt # Added to enable simpler mail configuration
#####################################################################################
################### Functions
#####################################################################################
msg()
{
$MSGS && echo "$*....."
}
msg70()
{
# like msg but doesn't print a new line instead just stops at 70th column {
$MSGS && printf "%-70s" "$*......"
}
err()
{
$ERRS && echo "$*....." >&2
}
ok()
{
$MSGS && echo ok
}
failed()
{
$MSGS && echo failed
}
usage()
{
MESSAGE=$*
echo
echo "$MESSAGE"
echo
echo "$0 -t [full|incremental] -d [</backup_directory>] [-s]"
echo
exit 1
}
generate_lock()
{
if [ -f ${LOCKFILE} ]
then
Pid=$(cat $LOCKFILE)
if ps -fp $Pid > /dev/null 2>&1
then
MSG="$SCRIPTNAME [ Pid: $Pid ] already running on ${SID} please investigate"
#cat $LOG | mail -s "$MSG" $ALERTS
cat $LOG | ${MAIL} -s "$MSG" $ALERT
exit
else
rm -rf $LOCKFILE
echo $$ > ${LOCKFILE}
fi
else
echo $$ > ${LOCKFILE}
fi
}
atexit()
# exit routine
{
katello-service status >/dev/null
rv=$?
if [ $rv -ne 0 ]
then
msg "Starting Satellite as it is not running - something must have gone wrong"
katello-service start
fi
if [ $RV -gt 0 ]
then
MSG="An error occurred whilst performing a ${TYPE} Satellite Backup on $(hostname -s)"
#cat $LOG | mail -s "$MSG" $ALERTS
sed -e 's#[^M]\ \ .*[^M]##g' $LOG | sed -r 's#\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]##g' | ${MAIL} -s "$MSG" $ALERTS
else
MSG="An ${TYPE} Satellite backup on $(hostname -s) completed successfully"
#cat $LOG |mail -s "$MSG" $REPORTS
sed -e 's#[^M]\ \ .*[^M]##g' $LOG | sed -r 's#\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]##g' | ${MAIL} -s "$MSG" $ALERTS
fi
# Make sure the lock file is removed
if [ -f $LOCKFILE ]
then
rm -f $LOCKFILE
fi
# recycle log files
if [ -f $LOG ]
then
cp -p $LOG $LOG.$TAG
gzip -f $LOG.$TAG
fi
msg "The return value is $RV"
msg Exiting
exit $RV
}
run_satver_validation()
{
SAT_VER=$(rpm -q satellite |awk -F - '{print $2}' |cut -c 1-3)
case $SAT_VER in
6.1|6.2)
BACKUP_UTILITY=katello-backup
;;
6.3)
BACKUP_UTILITY=satellite-backup
;;
6.4)
BACKUP_UTILITY="foreman-maintain backup offline"
;;
*)
usage "ERROR Unknown Satellite Version"
RV=50
exit
;;
esac
}
run_backupdir_validation()
{
if [ -d $BACKUP_DIRECTORY ] && [ -w $BACKUP_DIRECTORY ]
then
msg70 Backup Directory $BACKUP_DIRECTORY is mounted and writeable
ok
else
msg Backup Directory $BACKUP_DIRECTORY either does not exist or is not writable
RV=10
exit
fi
msg70 Checking Stale NFS Mounts
read -t1 < <(stat -t "${BACKUP_DIRECTORY}")
rv=$?
if [ $rv -eq 0 ]
then
ok
else
failed
err "Return value is $rv"
RV=11
exit
fi
case $TYPE in
FULL|Full|full)
if [ $SKIP_OPTIONS = "" ]
then
msg70 Estimating Space Requirements vs Available Space
EST_SIZE=$(( $(for directory in /var/lib/mongodb /var/lib/pulp /var/lib/pgsql; do du -ms $directory |awk '{print $1}'; done | tr "\n" "+" ) 0 ))
BACKUP_DIR_FREE_SPACE=$(df -Pm ${BACKUP_DIRECTORY} |awk '{print $4}' |grep -v Available)
if [ $EST_SIZE -gt $BACKUP_DIR_FREE_SPACE ]
then
failed
echo "Estimated Backup size is $EST_SIZE megabytes."
echo "Free space in $BACKUP_DIRECTORY is $BACKUP_DIR_FREE_SPACE megabytes"
echo "Insufficent space to run a backup"
RV=999
exit
else
ok
fi
fi
;;
esac
if [ ! -d ${BACKUP_DIRECTORY}/incr ]
then
mkdir -p ${BACKUP_DIRECTORY}/incr
fi
if [ ! -d ${BACKUP_DIRECTORY}/full ]
then
mkdir -p ${BACKUP_DIRECTORY}/full
fi
}
run_full()
{
echo "================================================================"
echo "$TYPE backup of Satellite started at $(date)"
echo "================================================================"
#$BACKUP_UTILITY ${BACKUP_DIRECTORY}/full --assumeyes ${SKIP_OPTIONS}
#peterceeau: order is important for new satellite 6.4 backup command
$BACKUP_UTILITY --assumeyes ${BACKUP_DIRECTORY}/full
rv=$?
if [ $rv -eq 0 ]
then
ok
else
failed
err "Return value is $rv"
RV=12
exit
fi
echo "================================================================"
echo "$TYPE backup of Satellite ended at $(date)"
echo "================================================================"
}
run_incremental()
{
echo "================================================================"
echo "$TYPE backup of Satellite started at $(date)"
echo "================================================================"
if [ $(find ${BACKUP_DIRECTORY}/full |egrep -c 'katello-backup|satellite-backup') -gt 0 ]
then
LASTFULL=$(find ${BACKUP_DIRECTORY}/full -type d |sort -rn |head -1)
#$BACKUP_UTILITY ${BACKUP_DIRECTORY}/incr --incremental ${LASTFULL} --assumeyes ${SKIP_OPTIONS}
#peterceeau: order is important for new satellite 6.4 backup command
$BACKUP_UTILITY --incremental ${LASTFULL} --assumeyes ${BACKUP_DIRECTORY}/incr
rv=$?
if [ $rv -eq 0 ]
then
ok
else
failed
err "Return value is $rv"
RV=13
exit
fi
echo "================================================================"
echo "$TYPE backup of Satellite ended at $(date)"
echo "================================================================"
else
echo "No Full Backup Found"
echo "Switching to Full backup"
TYPE=Full
run_full
fi
}
run_expiration()
{
echo "================================================================"
echo "Satellite Backup Expiration started at $(date)"
echo "================================================================"
msg Removing Daily Incremental Backups - ${RETENTION} days
for incr_dir in $(/usr/bin/find ${BACKUP_DIRECTORY}/incr -type d -mtime +${RETENTION} -name "*katello-backup*" -o -type d -mtime +${RETENTION} -name "*satellite-backup*")
do
msg70 Removing $incr_dir
rm -rf $incr_dir
rv=$?
if [ $rv -eq 0 ]
then
ok
else
failed
err "Return value is $rv"
RV=14
exit
fi
done
msg Removing Weekly Full Backups - ${RETENTION} days plus 7 to cater for a weeks worth of incrementals tied to the full
for full_dir in $(/usr/bin/find ${BACKUP_DIRECTORY}/full -type d -mtime +$(( $RETENTION + 7)) -name "*katello-backup*" -o -type d -mtime +$(( $RETENTION + 7)) -name "*satellite-backup*")
do
msg70 Removing $full_dir
rm -rf $full_dir
rv=$?
if [ $rv -eq 0 ]
then
ok
else
failed
err "Return value is $rv"
RV=15
exit
fi
done
echo "================================================================"
echo "Satellite Backup Expiration ended at $(date)"
echo "================================================================"
}
#####################################################################################
################### Main (Main MAIN Main)
#####################################################################################
# Process options have been passed to the script
while getopts ":t:d:s" opt
do
case $opt in
t) TYPE=${OPTARG} ;;
d) BACKUP_DIRECTORY=${OPTARG} ;;
s) SKIP_OPTIONS="--skip-pulp-content" ;;
*) usage "ERROR: Unknown Option"; exit ;;
esac
done
[ -z "$TYPE" ] && usage "ERROR: Specify the backup type"
[ -z "$BACKUP_DIRECTORY" ] && usage "ERROR: Specify the backup directory"
# Check we are running as root
if [ $(whoami) != root ]
then
echo "This script must run as the root user"
RV=2
exit
fi
# Make sure /sbin is in our $PATH
export PATH=$PATH:/sbin
# Generate the lock file
generate_lock
# make sure log directory exists
if [ ! -d $LOGDIR ]
then
mkdir -p $LOGDIR
fi
# redirect stdout and stderr to the log file
exec >$LOG 2>&1
# Call function at the end if the script exists cleanly.
trap atexit 0
msg "Validating the Satellite Version"
run_satver_validation
msg "Validating the Backup directory"
run_backupdir_validation
case $TYPE in
FULL|Full|full)
msg "Running a Full Satellite Backup"
run_full
;;
INCREMENTAL|Incremental|incremental|INC|Inc|inc|INCR|Incr|incr)
msg "Running an Incremental Satellite Backup"
run_incremental
;;
*)
usage "ERROR Unknown Backup Type"
RV=666
exit
;;
esac
msg "Kicking off Expiration of old backups"
run_expiration
# If script ends with RV=0 then atexit() is called due to trap.