From 1ede93357e45d6fe504364ceee222a082e4eb033 Mon Sep 17 00:00:00 2001
From: CedarLUG <19336442+cedarlug@users.noreply.github.com>
Date: Tue, 23 Jan 2018 11:18:31 -0600
Subject: [PATCH] Added error handling and commentary

---
 lgsm/functions/command_backup.sh | 35 ++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/lgsm/functions/command_backup.sh b/lgsm/functions/command_backup.sh
index ea9647c4d..95a677b33 100644
--- a/lgsm/functions/command_backup.sh
+++ b/lgsm/functions/command_backup.sh
@@ -113,8 +113,15 @@ fn_backup_compression(){
 	fn_print_dots "Backup (${rootdirduexbackup}) ${backupname}.tar.gz, in progress..."
 	fn_script_log_info "backup ${rootdirduexbackup} ${backupname}.tar.gz, in progress"
         excludedir=$(fn_backup_relpath)
-	# CHECK THAT excludedir isn't empty.  Sanity check here -CedarLUG
-	tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" ./*
+
+	# CHECK THAT excludedir is a valid path.
+	if [ ! -d "${excludedir}" ] ; then
+		fn_print_info_nl "Problem identifying the previous backup directory for exclusion."
+		fn_script_log_error "Problem identifying the previous backup directory for exclusion"
+		core_exit.sh
+	fi
+
+	tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${tmpdir}/.backup.lock" ./*
 	local exitcode=$?
 	if [ ${exitcode} -ne 0 ]; then
 		fn_print_fail_eol
@@ -183,26 +190,46 @@ fn_backup_prune(){
 
 fn_backup_relpath() {
   	# Written by CedarLUG as a "realpath --relative-to" alternative in bash
+
+	# Populate an array of tokens initialized from the rootdir components:
   	declare -a rdirtoks=($(readlink -f "${rootdir}" | sed "s/\// /g"))
-	# CHECK THAT the array is populated correctly.  Sanity check here -CedarLUG
+
+	if [ ${#rdirtoks[@]} -eq 0 ]; then
+		fn_print_info_nl "Problem assessing rootdir during relative path assessment"
+		fn_script_log_error "Problem assessing rootdir during relative path assessment: ${rootdir}"
+		core_exit.sh
+	fi
+
+	# Populate an array of tokens initialized from the backupdir components:
   	declare -a bdirtoks=($(readlink -f "${backupdir}" | sed "s/\// /g"))
-	# CHECK THAT the array is populated correctly.  Sanity check here -CedarLUG
+	if [ ${#bdirtoks[@]} -eq 0 ]; then
+		fn_print_info_nl "Problem assessing backupdir during relative path assessment"
+		fn_script_log_error "Problem assessing backupdir during relative path assessment: ${rootdir}"
+		core_exit.sh
+	fi
 
+	# Compare the leading entries of each array.  These common elements will be clipped off
+	# for the relative path output.
   	for ((base=0; $base<${#rdirtoks[@]}; base++))
   	do
       		[[ "${rdirtoks[$base]}" != "${bdirtoks[$base]}" ]] && break
   	done
 
+	# Next, climb out of the remaining rootdir location with updir references...
   	for ((x=${base};$x<${#rdirtoks[@]};x++))
   	do
       		echo -n "../"
   	done
 
+	# Climb down the remaining components of the backupdir location.
   	for ((x=${base};$x<$(( ${#bdirtoks[@]} - 1 ));x++))
   	do
       		echo -n "${bdirtoks[$x]}/"
   	done
 
+	# In the event there were no directories left in the backupdir above to
+	# traverse down, just add a newline.  Otherwise at this point, there is
+	# one remaining directory component in the backupdir to navigate.
   	if (( "$base" < "${#bdirtoks[@]}" )) ; then
       		echo ${bdirtoks[ $(( ${#bdirtoks[@]} - 1)) ]}
   	else