-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakebackup
executable file
·692 lines (632 loc) · 25.5 KB
/
makebackup
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#!/bin/bash
#------------------------------------------------------------------------------#
# vi: set sw=4 ts=4 ai: ("set modeline" in ~/.exrc) #
#------------------------------------------------------------------------------#
# Program : makebackup #
# #
# Author : Ton Kersten Groesbeek, The Netherlands #
# #
# Date : 26-03-2008 Time : 11:35 #
# #
# Description : Create a backup of all specified directories #
# #
# Parameters : None #
# #
# Pre reqs : Rsync, the backupit tool and Perl #
# #
# Exit codes : 0 -> OK #
# <> 0 -> !OK #
# #
# Updates : None (yet) #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# V e r s i o n i n f o r m a t i o n #
#------------------------------------------------------------------------------#
# $Id:: makebackup 51 2015-03-25 11:08:39 tonk $: #
# $Revision:: 51 $: #
# $Author:: Ton Kersten <[email protected]> $: #
# $Date:: 2015-03-25 11:08:39 +0200 (Wed, 25 Mar 2015) $: #
#------------------------------------------------------------------------------#
# E n d o f v e r s i o n i n f o r m a t i o n #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# Determine the program name and the 'running directory' #
#------------------------------------------------------------------------------#
IAM="${0##*/}"
CRD="$( [[ "${0:0:2}" = "./" ]] &&
{ printf "${PWD}/${0#./}"
} || {
printf "${0}"
})"
CRD="${CRD%/*}"
CUR="${PWD}"
#------------------------------------------------------------------------------#
# Save the shell settings #
#------------------------------------------------------------------------------#
SETA=0; [[ ${-} = *a* ]] && SETA=1
SETE=0; [[ ${-} = *e* ]] && SETE=1
SETU=0; [[ ${-} = *u* ]] && SETU=1
SETX=0; [[ ${-} = *x* ]] && SETX=1
#------------------------------------------------------------------------------#
# Set and unset the needed shell settings #
#------------------------------------------------------------------------------#
set +o noclobber # Overwrite existing files, if needed #
set -o nounset # Do not allow uninitialized variables #
set +o errexit # No returncode checking #
#------------------------------------------------------------------------------#
# Define the date/time function #
#------------------------------------------------------------------------------#
Now()
{
date '+%Y-%m-%d %H:%M:%S'
return 0
}
#------------------------------------------------------------------------------#
# Calculate h:m:s from seconds #
#------------------------------------------------------------------------------#
hms()
{
s="${1}"
h=$(( ${s}/3600 ))
s=$(( ${s} - ( ${h} * 3600) ))
m=$(( ${s} / 60))
s=$(( ${s} -( ${m} * 60 ) ))
printf "%d:%02d:%02d" ${h} ${m} ${s}
}
#------------------------------------------------------------------------------#
# Send a line to syslog (if $1 = '-f' the line is forced to syslog) #
#------------------------------------------------------------------------------#
syslog()
{ if [[ x"${1:-}" = x"-f" ]]
then
shift
echo "${*:-}" | logger -t "${IAM}" -p "${SYSLOGFAC:-local4.info}"
return
fi
if [[ x"${SYSLOG:-0}" = x"1" ]]
then
echo "${*:-}" | logger -t "${IAM}" -p "${SYSLOGFAC:-local4.info}"
fi
}
#------------------------------------------------------------------------------#
# Check the configfile #
#------------------------------------------------------------------------------#
CONFIGFILE="${1:-${CRD}/${IAM}.conf}"
if [[ ${#} = 0 ]]
then
LOGFILE="/var/log/${IAM}.log"
else
LOGFILE="/var/log/${IAM}-$(basename ${CONFIGFILE} | sed 's!\.conf.*$!!').log"
fi
#------------------------------------------------------------------------------#
# Start the logging #
#------------------------------------------------------------------------------#
exec 6>&1 # Save stdout into fd6 #
exec 7>&2 # Save stderr into fd7 #
exec > ${LOGFILE}
exec 2>&1
#------------------------------------------------------------------------------#
# Define constants and variables #
#------------------------------------------------------------------------------#
VER="1.$(awk '/^# \$Revision::/ { print $3 }' ${0})"
PATH="${PATH}:/bin:/sbin"
PATH="${PATH}:/usr/bin:/usr/sbin"
PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
PATH="${PATH}:${CRD}"
BCK="${CRD}/backupit"
PID="/var/run/${IAM}.pid"
HOSTNAME="$(hostname)"
WD="80" # Screen width #
s="$(printf "%${WD}s" "")"; s="${s// /-}" # Single dash line #
d="$(printf "%${WD}s" "")"; d="${d// /=}" # Double dash line #
typeset -i RC=0
typeset -i BCKS=0
#------------------------------------------------------------------------------#
# Setup trapping to remove the PID file #
#------------------------------------------------------------------------------#
trap "rm -f ${PID}" EXIT
#------------------------------------------------------------------------------#
# Announce the start of the program #
#------------------------------------------------------------------------------#
BCKSTART=$(date '+%s')
BCKSTARTTIME=$(Now)
echo "${d}"
echo
echo "$(Now) -> Starting ${IAM} version ${VER}"
echo
echo "${d}"
echo
echo " System backup at ${HOSTNAME} ended BACKUPSTATUS"
echo
echo "${d}"
echo
syslog -f "Starting ${IAM}"
#------------------------------------------------------------------------------#
# Do we already have a PID file??? If not, create it. #
#------------------------------------------------------------------------------#
if [[ -f ${PID} ]]
then
syslog -f "PID file ${PID} present. Skipping backup"
exit 0
fi
echo ${$} > ${PID}
#------------------------------------------------------------------------------#
# Read the config file #
#------------------------------------------------------------------------------#
if [[ -r "${CONFIGFILE}" ]]
then
. "${CONFIGFILE}" || {
echo "Error processing config '${CONFIGFILE}'!"
syslog -f "Error processing config '${CONFIGFILE}'!"
rm -f ${PID}
exit 1
}
else
echo "Could not find config '${CONFIGFILE}'!" >&2
syslog -f "Could not find config '${CONFIGFILE}'!"
rm -f ${PID}
exit 1
fi
#------------------------------------------------------------------------------#
# Set verbose and debug #
#------------------------------------------------------------------------------#
[[ "${VERBOSE:-0}" = "1" ]] && VERBOSE="-v" || VERBOSE=""
[[ "${DEBUG:-0}" = "1" ]] && DEBUG="-d" || DEBUG=""
[[ x"${DEBUG}" != x"" ]] && set -x
#------------------------------------------------------------------------------#
# Set defaults for unset stuff #
#------------------------------------------------------------------------------#
FAILS=0
MAX="${MAX:-99}"
BACKUPMYSQL="${BACKUPMYSQL:-no}"
SYSLOG="${SYSLOG:-1}"
SYSLOGFAC="${SYSLOGFAC:-local4.info}"
MAILTO="${MAILTO:-root@localhost}"
PREBACKUP="${PREBACKUP:-}"
POSTBACKUP="${POSTBACKUP:-}"
#------------------------------------------------------------------------------#
# Define the language settings (not allways needed) #
#------------------------------------------------------------------------------#
export LANG=${LANG:-C}
export LC_ALL=${LC_ALL:-C}
#------------------------------------------------------------------------------#
# Run a command before we begin #
#------------------------------------------------------------------------------#
if [[ x"${PREBACKUP:-}" != x"" ]]
then
syslog "Starting pre backup command"
echo "${d}"
echo "Prebackup command output."
echo
eval ${PREBACKUP} 2>&1 > /tmp/${IAM}_${$}
THISRC=${?}
sed 's/^/ /' /tmp/${IAM}_${$}
if [[ ( ${THISRC} != 0 ) && ( ${SYSLOG:-0} = 1 ) ]]
then
sed "s/^/${SHRT[${CNT}]:-} : /" /tmp/${IAM}_${$} | \
logger -t "${IAM}" -p "${SYSLOGFAC:-local4.info}"
fi
echo
echo "Returncode for the prebackup command: ${THISRC}"
echo
echo "${d}"
echo
syslog "Prebackup command ended with returncode ${THISRC}"
rm -f /tmp/${IAM}_${$}
if [[ ${THISRC} != 0 ]]
then
echo "Exit because of failed 'pre' command"
syslog "Exit because of failed 'pre' command"
exit ${THISRC}
fi
fi
#------------------------------------------------------------------------------#
# Backup MySQL if this is requested #
#------------------------------------------------------------------------------#
if [[ x"${BACKUPMYSQL:-no}" = x"yes" ]]
then
syslog "Starting MySQL backup"
if [[ -x ${CRD}/mysqlbackup ]]
then
echo "$(Now) -> ${IAM}: Starting MySQL backup on host '$(hostname)'"
syslog "Starting MySQL backup on host '$(hostname)'"
${CRD}/mysqlbackup
echo "$(Now) -> ${IAM}: Finished MySQL backup on host '$(hostname)'"
syslog "Finished MySQL backup on host '$(hostname)'"
else
echo "Cannot find MySQL backup tool '${CRD}/mysqlbackup'" >&2
echo "MySQL backup will be skipped!!" >&2
syslog "Cannot find MySQL backup tool '${CRD}/mysqlbackup'"
syslog "MySQL backup will be skipped!!"
fi
syslog "Finished MySQL backup"
fi
#------------------------------------------------------------------------------#
# Check if the 'backupit' program exists. If not, there is no way to backup #
#------------------------------------------------------------------------------#
if [[ ! -x "${BCK}" ]]
then
echo "The backup program '${BCK}' cannot be found!" >&2
echo "There is no way to create a backup." >&2
echo "The program will be stopped." >&2
syslog "The backup program '${BCK}' cannot be found!"
syslog "There is no way to create a backup."
syslog "The program will be stopped. (RC=1)"
rm -f ${PID}
exit 1
fi
#------------------------------------------------------------------------------#
# Create the backups for all defined sources and destinations and remove the #
# ones that are obsolete #
#------------------------------------------------------------------------------#
TOTALCYCLESTART=$(date '+%s')
TOTALDISKUSED=0
for CNT in $(seq 1 ${BCKS})
do
syslog "Backup cycle for '${SHRT[${CNT}]:-}' starting..."
HOSTCYCLESTART=$(date '+%s')
for DST in ${DSTS[${CNT}]}
do
CYCLESTART=$(date '+%s')
CYCLESTARTTIME=$(Now)
#----------------------------------------------------------------------#
# Debug per host #
#----------------------------------------------------------------------#
if [[ x"${DBG[${CNT}]:-0}" = x"1" ]]
then
set -x
DEBUG="-d"
else
set +x
DEBUG=""
fi
#----------------------------------------------------------------------#
# Remove old backups if needed #
#----------------------------------------------------------------------#
if [[ x"${REMOVE[${CNT}]:-}" = x"REMOVE_OBSOLETE_BACKUP" ]]
then
if [[ -d ${DST} ]]
then
#--------------------------------------------------------------#
# Make sure we do not remove the root #
#--------------------------------------------------------------#
if [[ x"${DST}" = x"/" ]]
then
echo "I will NOT remove the root for destination '${DST}'." >&2
syslog "I will NOT remove the root for destination '${DST}'."
continue
fi
echo "Removing the old backups for destination '${DST}'." >&2
syslog "Removing the old backups for destination '${DST}'."
rm -rf ${DST}
continue
fi
fi
#----------------------------------------------------------------------#
# Create the destination directory if it's not there yet #
#----------------------------------------------------------------------#
[[ ! -d ${DST} ]] &&
{ mkdir -p ${DST}
chmod 700 ${DST}
}
#----------------------------------------------------------------------#
# Create the backup source string #
#----------------------------------------------------------------------#
SRC=""
SRCTXT=""
for dir in ${DIRS[${CNT}]}
do
SRC="${SRC} --source='${dir}'"
SRCTXT="${SRCTXT} ${dir}"
done
#----------------------------------------------------------------------#
# Create the backup exclude string (may be empty) #
#----------------------------------------------------------------------#
EXC=""
EXCTXT=""
for exc in ${EXCL[${CNT}]:-}
do
EXC="${EXC} --exclude='${exc}'"
EXCTXT="${EXCTXT} ${exc}"
done
[[ x"${EXCTXT}" = x"" ]] && EXCTXT="None"
#----------------------------------------------------------------------#
# Find out the number of cycles to keep #
#----------------------------------------------------------------------#
CYCLES=${CYCL[${CNT}]:-${MAX}}
#----------------------------------------------------------------------#
# Start with a summary of what to do #
#----------------------------------------------------------------------#
set ${SRCTXT}; SRC1="${1}"; shift; SRCR="${@:-}"
set ${EXCTXT}; EXC1="${1}"; shift; EXCR="${@:-}"
echo
echo
echo " Starting backup cycle"
echo " ${s:4}"
echo " Description : "$(echo ${DESC[${CNT}]})
echo " Sources : "${SRC1}
for f in ${SRCR}
do
echo " "${f}
done
echo " Excludes : "${EXC1}
for f in ${EXCR}
do
echo " "${f}
done
echo " Destination : "${DST}
echo " Number of cycles : "${CYCLES}
echo " Monthly backups : "${MONTHLY:-no}
echo " Monthly cycles : "${MONTHLYMAX:-12}
echo " Cycle start time : "$(Now)
echo " ${s:4}"
echo
echo
syslog "${SHRT[${CNT}]:-} : Starting backup cycle for '${DESC[${CNT}]}'"
#----------------------------------------------------------------------#
# Do the backup #
#----------------------------------------------------------------------#
if [[ x"${RSYNCOPTS:-}" != x"" ]]
then
RSYNCOPTS="--rsyncopts='${RSYNCOPTS}'"
else
RSYNCOPTS=""
fi
${BCK} \
${VERBOSE} \
${DEBUG} \
${RSYNCOPTS} \
${SRC} \
${EXC} \
--max=${CYCLES} \
--target=${DST} 2>&1 > /tmp/${IAM}_${$}
THISRC=${?}
#----------------------------------------------------------------------#
# Create the monthly backups? #
#----------------------------------------------------------------------#
if [[ x"${MONTHLY:-no}" = x"yes" ]]
then
if [[ x"$(date '+%d')" = x"01" ]]
then
cd ${DST}/latest
pwd
if [[ ${?} = 0 ]]
then
echo " Starting monthly backup cycle"
syslog "${SHRT[${CNT}]:-} : Starting monthly backup cycle'"
${BCK} \
${VERBOSE} \
${DEBUG} \
${RSYNCOPTS} \
--max=${MONTHLYMAX} \
--source="." \
--target=${DST}/_monthly_ 2>&1 >> /tmp/${IAM}_${$}
MONTHLYRC=${?}
if [[ ${MONTHLYRC} != 0 ]]
then
echo " Monthly backup failed with RC=${MONTHLYRC}"
syslog "${SHRT[${CNT}]:-} : Monthly backup failed with RC=${MONTHLYRC}"
fi
cd - >/dev/null 2>&1
fi
fi
fi
THISRC=$(( ${THISRC} + ${MONTHLYRC:-0} ))
#----------------------------------------------------------------------#
# Parse the logfile and send it to the standard logging #
#----------------------------------------------------------------------#
sed 's/^/ /' /tmp/${IAM}_${$}
#----------------------------------------------------------------------#
# Send the logging to syslog if needed and wanted #
#----------------------------------------------------------------------#
if [[ ( ${THISRC} != 0 ) && ( ${SYSLOG:-0} = 1 ) ]]
then
sed "s/^/${SHRT[${CNT}]:-} : /" /tmp/${IAM}_${$} | \
logger -t "${IAM}" -p "${SYSLOGFAC:-local4.info}"
fi
#----------------------------------------------------------------------#
# And, if things fail, increase the failure number #
#----------------------------------------------------------------------#
if [[ ( ${THISRC} != 0 ) ]]
then
FAILS=$(( ${FAILS} + 1 ))
FAILHOST[${FAILS}]="${SHRT[${CNT}]:-}"
FAILRC[${FAILS}]="${THISRC}"
fi
#----------------------------------------------------------------------#
# Create a nice summary #
#----------------------------------------------------------------------#
RC=${RC}+${THISRC}
CYCLEEND=$(date '+%s')
CYCLEENDTIME=$(Now)
if [[ -e ${DST}/latest/ ]]
then
USEDH="$(du -hs ${DST}/latest/ 2>/dev/null | awk '{ print $1 " (" $2 ")" }')"
USEDC="$(du -bs ${DST}/latest/ 2>/dev/null | awk '{ print $1 }')"
else
USEDH="Unknown"
USEDC="0"
fi
if [[ -e ${DST} ]]
then
FREEH="$(df -hP ${DST} 2>/dev/null | awk ' !/^F/ { print $4 }')"
FREEC="$(df -P ${DST} 2>/dev/null | awk ' !/^F/ { print $4 }')"
else
FREEH="Unknown"
FREEC="0"
fi
RUN=$(hms $(( ${CYCLEEND} - ${CYCLESTART} )) )
TOTALDISKUSED=$(( ${TOTALDISKUSED} + ${USEDC} ))
echo
echo
echo " Cycle statistics"
echo " ${s:4}"
echo " Returncode : "${THISRC}
echo " Used disk space : "${USEDH}
echo " Free disk space : "${FREEH}
echo " Cycle end time : "$(Now)
echo " Cycle run time : "${RUN}
echo " ${s:4}"
echo
echo
SUM=""
SUM="${SUM} START=${CYCLESTARTTIME};"
SUM="${SUM} END=${CYCLEENDTIME};"
SUM="${SUM} RUN=${RUN};"
SUM="${SUM} RC=${THISRC};"
SUM="${SUM} USED=${USEDH};"
SUM="${SUM} FREE=${FREEH};"
syslog "BckHost = ${SHRT[${CNT}]:-} : Backup_sources = ${SRCTXT}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Backup_destination = ${DST}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Returncode = ${THISRC}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Monthly returncode = ${MONTHLYRC:-0}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Used_disk_space = ${USEDC}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Free_disk_space = ${FREEC}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Cycle_run_time = ${RUN}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Summary = ${SUM}"
syslog "BckHost = ${SHRT[${CNT}]:-} : Finished backup cycle for '${DESC[${CNT}]}'"
rm -f /tmp/${IAM}_${$}
syslog "Backup cycle for '${SHRT[${CNT}]:-}' to '${DST}' finished... (Run time: ${RUN})"
done
echo
echo "${d}"
echo
HOSTCYCLEEND=$(date '+%s')
HOSTRUN=$(hms $(( ${HOSTCYCLEEND} - ${HOSTCYCLESTART} )) )
syslog "Run_Time_${SHRT[${CNT}]:-} = ${HOSTRUN}"
done
#------------------------------------------------------------------------------#
# Statistics in syslog #
#------------------------------------------------------------------------------#
TOTALCYCLEEND=$(date '+%s')
TOTALRUN=$(hms $(( ${TOTALCYCLEEND} - ${TOTALCYCLESTART} )) )
syslog "Total = Finished : Run_time = ${TOTALRUN}"
syslog "Total = Finished : Total_disk_used = ${TOTALDISKUSED})"
#------------------------------------------------------------------------------#
# Check if it all went well #
#------------------------------------------------------------------------------#
if [[ ${RC} = 0 ]]
then
echo "$(Now) -> Backup ended OK!"
BCKSTATUS="OK! Returncode = 0"
else
if [[ ${RC} = 24 ]]
then
echo "$(Now) -> Backup ended OK!"
BCKSTATUS="OK! (Some files vanished). Returncode = 24"
else
echo "$(Now) -> Backup ended NOT OK! Returncode = ${RC}"
BCKSTATUS="NOT OK! Returncode = ${RC}"
fi
fi
#------------------------------------------------------------------------------#
# Run a command after we are done #
#------------------------------------------------------------------------------#
if [[ x"${POSTBACKUP:-}" != x"" ]]
then
syslog "Starting post backup command"
echo "${d}"
echo "Postbackup command output."
echo
eval ${POSTBACKUP} 2>&1 > /tmp/${IAM}_${$}
THISRC=${?}
sed 's/^/ /' /tmp/${IAM}_${$}
if [[ ( ${THISRC} != 0 ) && ( ${SYSLOG:-0} = 1 ) ]]
then
sed "s/^/${SHRT[${CNT}]:-} : /" /tmp/${IAM}_${$} | \
logger -t "${IAM}" -p "${SYSLOGFAC:-local4.info}"
fi
echo
echo "Returncode for the postbackup command: ${THISRC}"
echo
echo "${d}"
echo
syslog "Postbackup command ended with returncode ${THISRC}"
rm -f /tmp/${IAM}_${$}
fi
#------------------------------------------------------------------------------#
# Log and display the summary #
#------------------------------------------------------------------------------#
BCKEND=$(date '+%s')
BCKENDTIME=$(Now)
RUN=$(hms $(( ${BCKEND} - ${BCKSTART} )) )
SUM=""
SUM="${SUM} START=${BCKSTARTTIME};"
SUM="${SUM} END=${BCKENDTIME};"
SUM="${SUM} RUN=${RUN};"
SUM="${SUM} RC=${RC};"
SUM="${SUM} STATUS=${BCKSTATUS};"
echo
echo "Total run time : "${RUN}
echo "${d}"
syslog "Total run time: "${RUN}
syslog "Ended on '${HOSTNAME}' (RC=${BCKSTATUS})"
syslog "Summary : ${SUM}"
syslog "Ending ${IAM}"
#------------------------------------------------------------------------------#
# And, if things failed, show that as well #
#------------------------------------------------------------------------------#
if [[ ${FAILS} != 0 ]]
then
typeset -i i=0
echo
echo "Failed backups"
echo "${d}"
syslog "Failed backups:"
while [[ ${i} < ${FAILS} ]]
do
i=$(( ${i} + 1 ))
echo " RC=${FAILRC[${i}]} for host ${FAILHOST[${i}]}"
syslog " RC=${FAILRC[${i}]} for host ${FAILHOST[${i}]}"
done
echo "${d}"
fi
#------------------------------------------------------------------------------#
# Clear the log redirection and insert the program status into the log #
#------------------------------------------------------------------------------#
exec 1>&6 6>&- # Restore stdout and close fd6 #
exec 2>&7 7>&- # Restore stderr and close fd7 #
sed -i.bck "s/BACKUPSTATUS/${BCKSTATUS}/" ${LOGFILE}
rm ${LOGFILE}.bck
#------------------------------------------------------------------------------#
# Mail the logging if requested #
#------------------------------------------------------------------------------#
case "${NOTIFY}"
in
syslog)
#----------------------------------------------------------------------#
# Send the logging to syslog #
#----------------------------------------------------------------------#
cat ${LOGFILE} | logger -t "${IAM}" -p "${SYSLOGFAC:-local4.info}"
;;
mail)
#----------------------------------------------------------------------#
# Send the logging through mail #
#----------------------------------------------------------------------#
SUBJ="Logging of '${IAM}' on '${HOSTNAME}' at $(Now) (RC=${BCKSTATUS})"
cat ${LOGFILE} | mail -s "${SUBJ}" ${MAILTO}
;;
mail-failonly)
#----------------------------------------------------------------------#
# Send the logging through mail #
#----------------------------------------------------------------------#
if [[ ${FAILS} != 0 ]]
then
SUBJ="Logging of '${IAM}' on '${HOSTNAME}' at $(Now) (RC=${BCKSTATUS})"
cat ${LOGFILE} | mail -s "${SUBJ}" ${MAILTO}
fi
;;
none)
;;
*)
#----------------------------------------------------------------------#
# Send the logging to stdout #
#----------------------------------------------------------------------#
cat ${LOGFILE}
;;
esac
#------------------------------------------------------------------------------#
# That's all, folks! #
#------------------------------------------------------------------------------#
rm -f ${PID}
exit ${RC}