cleanup printf formatting

This commit is contained in:
Asif Bacchus 2020-08-14 00:39:23 -06:00
parent 760399a0fd
commit 6fc82aac83

View File

@ -54,7 +54,7 @@ badDetails () {
badParam () {
if [ "$1" = "dne" ]; then
printf "\n%sError: '%s %s'\n" "$err" "$2" "$3"
printf "file or directory does not exist.${norm}\n\n"
printf "file or directory does not exist.%s\n\n" "$norm"
exit 1
elif [ "$1" = "empty" ]; then
printf "\n%sError: '%s' cannot have a NULL (empty) value.\n" "$err" "$2"
@ -336,8 +336,8 @@ while [ $# -gt 0 ]; do
fi
;;
*)
printf "${err}\nUnknown option: %s\n" "$1"
printf "${cyan}Use '--help' for valid options.{$norm}\n\n"
printf "%s\nUnknown option: %s\n" "$err" "$1"
printf "%sUse '--help' for valid options.%s\n\n" "$cyan" "$norm"
exit 1
;;
esac
@ -348,13 +348,13 @@ done
### check pre-requisites and default values
# check if running as root, otherwise exit
if [ $( id -u ) -ne 0 ]; then
printf "\n${err}ERROR: script MUST be run as ROOT${norm}\n\n"
printf "\n%sERROR: script MUST be run as ROOT%s\n\n" "$err" "$norm"
exit 2
fi
# find pilerexport, otherwise this is all pointless
pilerExport=$( command -v pilerexport )
if [ ! "$pilerExport" ]; then
printf "\n${err}ERROR: cannot find 'pilerexport'${norm}\n\n"
printf "\n%sERROR: cannot find 'pilerexport'%s\n\n" "$err" "$norm"
exit 4
fi
# does the details file exist?
@ -363,7 +363,7 @@ if [ ! -f "$configDetails" ]; then
fi
# is borg installed?
if ! command -v borg > /dev/null; then
printf "\n${err}ERROR: BORG is not installed on this system!${norm}\n\n"
printf "\n%sERROR: BORG is not installed on this system!%s\n\n" "$err" "$norm"
exit 3
fi
# if 503 functionality is enabled, do 503 related files exist?
@ -377,26 +377,26 @@ fi
### start logging
printf "${magenta}[%s] --- Start %s execution ---${norm}\n" \
"$(stamp)" "$scriptName" >> "$logFile"
printf "${cyan}[%s] -- [INFO] Log located at ${yellow}%s${cyan} --${norm}\n" \
"$(stamp)" "$logFile" >> "$logFile"
printf "%s[%s] --- Start %s execution ---%s\n" \
"$magenta" "$(stamp)" "$scriptName" "$norm" >> "$logFile"
printf "%s[%s] -- [INFO] Log located at %s%s%s --%s\n" \
"$cyan" "$(stamp)" "$yellow" "$logFile" "$cyan" "$norm" >> "$logFile"
### 503 functionality
if [ "$use503" -eq 1 ]; then
printf "${cyan}[%s] -- [INFO] Copying 503 error page to " \
"$(stamp)" >> "$logFile"
printf "webroot -- ${norm}\n" >> "$logFile"
printf "%s[%s] -- [INFO] Copying 503 error page to " \
"$cyan" "$(stamp)" >> "$logFile"
printf "webroot -- %s\n" "$norm" >> "$logFile"
if ! \cp --force "${err503Path}" "${webroot}/${err503File}" 2>> "$logFile"
then
printf "${warn}[%s] -- [WARNING] Failed to copy 503 error page. " \
"$(stamp)" >> "$logFile"
printf "Web users will NOT be notified --${norm}\n" >> "$logFile"
printf "%s[%s] -- [WARNING] Failed to copy 503 error page. " \
"$warn" "$(stamp)" >> "$logFile"
printf "Web users will NOT be notified --%s\n" "$norm" >> "$logFile"
warnCount=$((warnCount+1))
else
printf "${ok}[%s] -- [SUCCESS] 503 error page copied --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [SUCCESS] 503 error page copied --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
# set cleanup flag
err503Copied=1
fi
@ -415,8 +415,8 @@ case "${configDetails}" in
. "./${configDetails}"
;;
esac
printf "${cyan}[%s] -- [INFO] ${yellow}%s${cyan} imported --${norm}\n" \
"$(stamp)" "$configDetails" >> "$logFile"
printf "%s[%s] -- [INFO] %s%s%s imported --%s\n" \
"$cyan" "$(stamp)" "$yellow" "$configDetails" "$cyan" "$norm" >> "$logFile"
## create tmp directory and change to it for export operations
@ -431,36 +431,36 @@ fi
if ! cd "$exportDir"; then
exitError 113 "Unable to change to temporary export directory"
fi
printf "${cyan}[%s] -- [INFO] EML files will be temporarily stored in:" \
"$(stamp)" >> "$logFile"
printf "\n${yellow}%s/${cyan} --${norm}\n" "$exportDir" >> "$logFile"
printf "%s[%s] -- [INFO] EML files will be temporarily stored in:" \
"$cyan" "$(stamp)" >> "$logFile"
printf "\n\t%s%s/%s --%s\n" "$yellow" "$exportDir" "$cyan" "$norm" >> "$logFile"
## export EML from piler
printf "${cyan}[%s] -- [INFO] Exporting EML files from piler --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [INFO] Exporting EML files from piler --%s\n" \
"$cyan" "$(stamp)" "$norm" >> "$logFile"
if [ "$exportAll" -eq 1 ]; then
if ! ${pilerExport} -A 2>>"$logFile"; then
exitError 115 "There was a problem while exporting EML from piler"
fi
printf "${ok}[%s] -- [SUCCESS] Exported EML files from piler --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [SUCCESS] Exported EML files from piler --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
else
if ! ${pilerExport} -a "$exportStart" -b "$exportEnd" 2>>"$logFile"; then
exitError 115 "There was a problem while exporting EML from piler"
fi
printf "${ok}[%s] -- [SUCCESS] Exported EML files from piler --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [SUCCESS] Exported EML files from piler --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
fi
### pre-backup tasks completed -- move to borg tasks
printf "${ok}[%s] -- [SUCCESS] Pre-backup tasks completed --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [SUCCESS] Pre-backup tasks completed --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
### Run borg variable checks
printf "${cyan}[%s] -- [INFO] Verifying supplied borg details --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [INFO] Verifying supplied borg details --%s\n" \
"$cyan" "$(stamp)" "$norm" >> "$logFile"
## verify borg base directory
if [ -z "${borgBaseDir}" ]; then
@ -468,8 +468,8 @@ if [ -z "${borgBaseDir}" ]; then
elif [ ! -d "${borgBaseDir}" ]; then
badDetails dne 'borgBaseDir'
fi
printf "${magenta}details:borgBaseDir ${norm}-- ${ok}[OK]${norm}\n" \
>> "$logFile"
printf "%sdetails:borgBaseDir%s -- %s[OK]%s\n" \
"$magenta" "$norm" "$ok" "$norm" >> "$logFile"
export BORG_BASE_DIR="${borgBaseDir%/}"
## check path to SSH keyfile
@ -478,28 +478,28 @@ if [ -z "${borgSSHKey}" ]; then
elif [ ! -f "${borgSSHKey}" ]; then
badDetails dne 'borgSSHKey'
fi
printf "${magenta}details:borgSSHKey ${norm}-- ${ok}[OK]${norm}\n" \
>> "$logFile"
printf "%sdetails:borgSSHKey%s -- %s[OK]%s\n" \
"$magenta" "$norm" "$ok" "$norm" >> "$logFile"
export BORG_RSH="ssh -i ${borgSSHKey}"
## check borg repo connect string
if [ -z "${borgConnectRepo}" ]; then
badDetails empty 'borgConnectRepo'
fi
printf "${magenta}details:borgConnectRepo ${norm}-- ${ok}[OK]${norm}\n" \
>> "$logFile"
printf "%sdetails:borgConnectRepo%s -- %s[OK]%s\n" \
"$magenta" "$norm" "$ok" "$norm" >> "$logFile"
export BORG_REPO="${borgConnectRepo}"
## check borg repo password
if [ -n "${borgRepoPassphrase}" ]; then
printf "${magenta}details:borgRepoPassphrase ${norm}-- ${ok}[OK]${norm}\n" \
>> "$logFile"
printf "%sdetails:borgRepoPassphrase%s -- %s[OK]%s\n" \
"$magenta" "$norm" "$ok" "$norm" >> "$logFile"
export BORG_PASSPHRASE="${borgRepoPassphrase}"
else
# if passwd is blank intentionally, this is insecure
printf "${warn} -- [WARNING] Using a borg repo without a password is an " \
>> "$logFile"
printf "insecure configuration --${norm}\n" >> "$logFile"
printf "%s-- [WARNING] Using a borg repo without a password is an " \
"$warn" >> "$logFile"
printf "insecure configuration --%s\n" "$norm" >> "$logFile"
warnCount=$((warnCount+1))
# if this was an accident, we need to provide a bogus passwd so borg fails
# otherwise it will sit forever just waiting for input
@ -525,8 +525,8 @@ if [ -n "${borgXtraListPath}" ]; then
done <<EOF
$( sed -e '/^\s*#.*$/d' -e '/^\s*$/d' "${borgXtraListPath}" )
EOF
printf "${magenta}details:borgXtraListPath ${norm}-- ${ok}[OK]${norm}\n" \
>> "$logFile"
printf "%sdetails:borgXtraListPath%s -- %s[OK]%s\n" \
"$magenta" "$norm" "$ok" "$norm" >> "$logFile"
includeXtra=1
fi
@ -551,9 +551,9 @@ if [ ! -d "${borgBaseDir}/tmp" ]; then
if ! mkdir "${borgBaseDir}/tmp"; then
exitError 132 "Unable to create borg ${borgBaseDir}/tmp directory"
else
printf "${cyan}[%s] -- [INFO] Created ${yellow}%s/tmp " \
"$(stamp)" "${borgBaseDir}" >> "$logFile"
printf "${cyan}--${norm}\n" >> "$logFile"
printf "%s[%s] -- [INFO] Created %s%s/tmp " \
"$cyan" "$(stamp)" "$yellow" "${borgBaseDir}" >> "$logFile"
printf "%s--%s\n" "$cyan" "$norm" >> "$logFile"
fi
fi
export TMPDIR="${borgBaseDir}/tmp"
@ -580,66 +580,66 @@ if [ "$includeXtra" -eq 1 ]; then
fi
# execute borg
printf "${cyan}[%s] -- [INFO] Executing borg backup operation --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [INFO] Executing borg backup operation --%s\n" \
"$cyan" "$(stamp)" "$norm" >> "$logFile"
${borgCMD} 2>> "$logFile"
borgResult="$?"
## check borg exit status
if [ "$borgResult" -eq 0 ]; then
printf "${ok}[%s] -- [SUCCESS] Borg backup completed --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [SUCCESS] Borg backup completed --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
elif [ "$borgResult" -eq 1 ]; then
printf "${warn}[%s] -- [WARNING] Borg completed with warnings. " \
"$(stamp)" >> "$logFile"
printf "Review this logfile for details --${norm}\n" >> "$logFile"
printf "%s[%s] -- [WARNING] Borg completed with warnings. " \
"$warn" "$(stamp)" >> "$logFile"
printf "Review this logfile for details --%s\n" "$norm" >> "$logFile"
warnCount=$((warnCount+1))
elif [ "$borgResult" -ge 2 ]; then
err_1="Borg exited with a critical error. Please review this log file"
err_2="for details."
exitError 138 "$err_1 $err_2"
else
printf "${warn}[%s] -- [WARNING] Borg exited with unknown return code. " \
"$(stamp)" >> "$logFile"
printf "Review this logfile for details --${norm}\n" >> "$logFile"
printf "%s[%s] -- [WARNING] Borg exited with unknown return code. " \
"$warn" "$(stamp)" >> "$logFile"
printf "Review this logfile for details --%s\n" "$norm" >> "$logFile"
warnCount=$((warnCount+1))
fi
### execute borg prune if paramters are provided, otherwise skip with a warning
if [ -n "${borgPruneSettings}" ]; then
printf "${cyan}[%s] -- [INFO] Executing borg prune operation --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [INFO] Executing borg prune operation --%s\n" \
"$cyan" "$(stamp)" "$norm" >> "$logFile"
borg prune --show-rc -v ${borgPruneParams} ${borgPruneSettings} \
2>> "$logFile"
borgPruneResult="$?"
else
printf "${warn}[%s] -- [WARNING] No prune parameters provided. " \
"$(stamp)" >> "$logFile"
printf "Your archive will continue growing with each backup --${norm}\n" \
>> "$logFile"
printf "%s[%s] -- [WARNING] No prune parameters provided. " \
"$warn" "$(stamp)" >> "$logFile"
printf "Your archive will continue growing with each backup --%s\n" \
"$norm" >> "$logFile"
warnCount=$((warnCount+1))
fi
## report on prune operation if executed
if [ -n "${borgPruneResult}" ]; then
if [ "${borgPruneResult}" -eq 0 ]; then
printf "${ok}[%s] -- [SUCCESS] Borg prune completed --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [SUCCESS] Borg prune completed --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
elif [ "$borgPruneResult" -eq 1 ]; then
printf "${warn}[%s] -- [WARNING] Borg prune completed with warnings. " \
"$(stamp)" >> "$logFile"
printf "Review this logfile for details --${norm}\n" >> "$logFile"
printf "%s[%s] -- [WARNING] Borg prune completed with warnings. " \
"$warn" "$(stamp)" >> "$logFile"
printf "Review this logfile for details --%s\n" "$norm" >> "$logFile"
warnCount=$((warnCount+1))
elif [ "$borgPruneResult" -ge 2 ]; then
err_1="Borg prune exited with a critical error. Please review this"
err_2="log file for details."
exitError 139 "$err_1 $err_2"
else
printf "${warn}[%s] -- [WARNING] Borg prune exited with an unknown " \
"$(stamp)" >> "$logFile"
printf "return code. Review this logfile for details --${norm}\n" \
>> "$logFile"
printf "%s[%s] -- [WARNING] Borg prune exited with an unknown " \
"$warn" "$(stamp)" >> "$logFile"
printf "return code. Review this logfile for details --%s\n" \
"$norm" >> "$logFile"
warnCount=$((warnCount+1))
fi
fi
@ -648,21 +648,21 @@ fi
### all processes successfully completed, cleanup and exit gracefully
# note successful completion of borg commands
printf "${ok}[%s] -- [SUCCESS] Backup operations completed --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "%s[%s] -- [SUCCESS] Backup operations completed --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
# cleanup
cleanup
# note complete success, tally warnings and exit
printf "${ok}[%s] -- [SUCCESS] All processes completed --${norm}\n" \
"$(stamp)" >> "$logFile"
printf "${magenta}[%s] --- %s execution completed ---${norm}\n" \
"$(stamp)" "$scriptName" >> "$logFile"
printf "%s[%s] -- [SUCCESS] All processes completed --%s\n" \
"$ok" "$(stamp)" "$norm" >> "$logFile"
printf "%s[%s] --- %s execution completed ---%s\n" \
"$magenta" "$(stamp)" "$scriptName" "$norm" >> "$logFile"
if [ "$warnCount" -gt 0 ]; then
printf "${warn}%s warnings issued!${norm}\n" "${warnCount}" >> "$logFile"
printf "%s%s warnings issued!%s\n" "$warn" "${warnCount}" "$norm" >> "$logFile"
else
printf "${ok}0 warnings issued.${norm}\n" >> "$logFile"
printf "%s0 warnings issued.%s\n" "$ok" "$norm" >> "$logFile"
fi
exit 0