Commit 738dbc5d authored by clewsy's avatar clewsy
Browse files

Improved error logging for bu. Implemented logging for ball.

parent fa32b7ff
......@@ -12,13 +12,31 @@ BLUE="\033[01;34m"
BOLD="\033[01;37m"
RESET="\033[0m"
##########Define log file and ensure directory exists.
BALL_LOG_FILE="${HOME}/.log/ball.log"
if [ ! -d "$(dirname "${BALL_LOG_FILE}")" ]; then mkdir --parents "$(dirname "${BALL_LOG_FILE}")"; fi
##########Exit codes
SUCCESS=0 ## Toight.
BAD_USAGE=1 ## Incorrect usage.
SUCCESS=0 ## Toight.
INVALID_OPTIONS=1 ## Incorrect usage.
TOO_MANY_ARGS=2
##########Function to print current date and time. Used for logging.
TIMESTAMP () { echo -ne "$(date +%Y-%m-%d\ %T)"; }
##########Function for exit conditions. Log error or success and exit.
QUIT ()
{
if [ "${1}" -gt 0 ]; then echo -e "$(TIMESTAMP) - Script failed with error code ${1}." >> "${BALL_LOG_FILE}"
else echo -e "$(TIMESTAMP) - Script completed successfully." >> "${BALL_LOG_FILE}"; fi
echo -e "-----------------------------------------------------------" >> "${BALL_LOG_FILE}"
exit "${1}"
}
#########Usage
USAGE="
Usage: $(basename "$0")[option] [host/list]
Where [host/list] is either:
host - a specific remote system on which command \"${COMMAND}\" will be run.
list - a text list of remote systems on which command \"${COMMAND}\" will be run.
......@@ -30,6 +48,7 @@ Options: -q Quiet mode - suppress most output.
-h Show this help.
"
#########Default values can be changed with -q or -v options.
VERBOSITY="" ## Define the default verbosity (i.e. none). Can be changed with option -v.
DEST="/dev/null" ## Default destination for output. Change to /dev/stdout with option -v.
......@@ -41,20 +60,19 @@ while getopts 'qvh' OPTION; do ## Call getopts to identify selected options a
v) VERBOSITY="-v" ## -v activates verbose mode by iadding the -v flag to the bu.sh command.
DEST="/dev/stdout" ;;
h) echo -e "${USAGE}" ## -h option just prints the usage then quits.
exit ${SUCCESS} ;; ## Exit successfully.
?) echo -e "Invalid option/s."
QUIT ${SUCCESS} ;; ## Exit successfully.
?) echo -e "${RED}Error:${RESET} Invalid option/s."
echo -e "${USAGE}" ## Invalid option, show usage.
exit ${BAD_USAGE} ;; ## Exit.
QUIT ${INVALID_OPTIONS} ;; ## Exit.
esac
done
shift $((OPTIND -1)) ## This ensures only non-option arguments are considered arguments when referencing $#, #* and $n.
##########Check correct usage.
if [ $# -gt 1 ]; then ## Check if more than one argument was entered.
echo -e "Too many arguments." ## If so, show usage and exit.
if [ $# -gt 1 ]; then ## Check if more than one argument was entered.
echo -e "${RED}Error:${RESET} Too many arguments." ## If so, show usage and exit.
echo -e "${USAGE}"
exit ${BAD_USAGE}
QUIT ${TOO_MANY_ARGS}
fi
##########Define the argument (user input or default).
......@@ -69,7 +87,6 @@ if [ -e "${TEMP_BALL_SUMMARY}" ]; then rm "${TEMP_BALL_SUMMARY}"; fi ## If it ex
TEMP_REM_SYS_LIST="/tmp/temp_rem_sys_list" ## Define a working system list.
if [ -e "${TEMP_REM_SYS_LIST}" ]; then rm "${TEMP_REM_SYS_LIST}"; fi ## If it exists, delete the temporary file (in case script failed previously).
##########Validate the argument and thus define the remote host/s.
## Arg specifies a remote host.
if [ ! -f "${ARGUMENT}" ] || [ ! -r "${ARGUMENT}" ]; then ## If argument is not (!) a normal file (-f) or (||) in is not (!) readable (-r).
......@@ -81,7 +98,7 @@ else
while read -r LINE ; do ## Iterate for every line in the system list.
STRIPPED_LINE="$(echo "${LINE}" | cut -d "#" -f 1)" ## Strip the content of the line after (and including) the first '#'.
if [ "${STRIPPED_LINE}" ]; then ## If there is anything left in the string (i.e. if entire row is NOT a comment).
echo "${STRIPPED_LINE}" >> "${TEMP_REM_SYS_LIST}" ## Then copy the stripped line to the temp file.
echo "${STRIPPED_LINE}" >> "${TEMP_REM_SYS_LIST}" ## Then copy the stripped line to the temp file.
fi
done < "${ARGUMENT}"
echo -e "\n${BLUE}Target systems:${RESET}" >> ${DEST}
......@@ -90,9 +107,12 @@ fi
########## Loop through the remote system list.
if [ "${QUIET_MODE}" != "TRUE" ]; then echo -e "\n----------------------------------------------------------------------------------"; fi
{
echo -e "$(TIMESTAMP) - Script initiated for following hosts:"
cat "${TEMP_REM_SYS_LIST}"
} >> "${BALL_LOG_FILE}"
while read -r REM_SYS <&2; do ## Loop to repeat commands for each file name entry in the backup file list ($BU_FILE_LIST)
## <&2 needed as descriptor for nested while read loops (while read loop within called script)
if [ "${QUIET_MODE}" != "TRUE" ]; then echo -e "${BLUE}Attempting to run \"${RESET}${COMMAND}${BLUE}\" on \"${RESET}${REM_SYS}${BLUE}\"${RESET}"; fi
## For loop to set the tab spacing depending on the length of the hostname (makes the ouput summary pretty).
......@@ -105,16 +125,18 @@ while read -r REM_SYS <&2; do ## Loop to repeat commands for each file name entr
if ! ssh -t "${REM_SYS}" "${COMMAND} ${VERBOSITY}"; then ## Attempt to connect via ssh and run the backup script "bu.sh"
echo -E "${REM_SYS}${COLUMN_SPACER} ${RED}Failure.${RESET}" >> "${TEMP_BALL_SUMMARY}" ## Record if the above fails for the current host.
echo -e "${RED}Failure.${RESET}" ## Also show failure on stdio.
echo -e "$(TIMESTAMP) - Failed to run ${COMMAND} on ${REM_SYS}." >> "${BALL_LOG_FILE}" ## Also record to log file.
if [ "${QUIET_MODE}" != "TRUE" ]; then echo -e "----------------------------------------------------------------------------------"; fi
continue ## Then try the next host in the list.
else
echo -E "${REM_SYS}${COLUMN_SPACER} ${GREEN}Success.${RESET}" >> "${TEMP_BALL_SUMMARY}" ## If the above succeeds, record the success.
echo -e "$(TIMESTAMP) - Successfully ran ${COMMAND} on ${REM_SYS}." >> "${BALL_LOG_FILE}" ## Also record to log file.
if [ "${QUIET_MODE}" != "TRUE" ]; then echo -e "----------------------------------------------------------------------------------"; fi ## Note a "success" means the ssh session was created and exited.
## gracefully. Failures with the called script are not checcked.
fi
done 2< "${TEMP_REM_SYS_LIST}" ## File read by the while loop which includes a list of files to be backed up.
########### Print out in a pretty format a table indicating the success or failure for each host in the list.
echo -e "${BOLD}\n╔════════════Summary:════════════╗${RESET}"
while read -r RESULT ; do
......@@ -124,4 +146,5 @@ echo -e "${BOLD}╚════════════════════
########## All done.
rm "${TEMP_BALL_SUMMARY}" "${TEMP_REM_SYS_LIST}" ## Delete the temporary files.
exit ${SUCCESS}
QUIT ${SUCCESS}
......@@ -21,11 +21,14 @@ if [ ! -d "$(dirname "${BU_LOG_FILE}")" ]; then mkdir --parents "$(dirname "${BU
##########Exit codes
SUCCESS=0 ## Noice.
BAD_USAGE=1 ## Incorrect usage.
BAD_ARGUMENT=2 ## Specified or default file list not readable.
BAD_OPTION=1 ## Incorrect usage.
TOO_MANY_ARGS ## More than one argument was provided.
MISSING_ARG ## Option -f or -d provided but argument was not provided.
BAD_ARG=2 ## Specified or default file list not readable.
NO_RSYNC=3 ## rsync not installed.
NO_REM_DIR=4 ## ssh command to create remote directory failed.
BAD_LIST_FILE=5 ## List file not identified as ascii text file.
NO_VALID_FILES ## Parsing list file found no valid files to back up.
RSYNC_FAILED=6 ## rsync command was reached but failed.
##########Function to print current date and time. Used for logging.
......@@ -69,7 +72,7 @@ while getopts 'fdlqvh' OPTION; do ## Call getopts to identify selected options
QUIT ${SUCCESS} ;; ## Exit successfully.
?) echo -e "${RED}Error:${RESET} Invalid option/s."
echo -e "$USAGE" ## Invalid option, show usage.
QUIT ${BAD_USAGE} ;; ## Exit.
QUIT "${BAD_OPTION}" ;; ## Exit.
esac
done
shift $((OPTIND -1)) ## This ensures only non-option arguments are considered arguments when referencing $#, #* and $n.
......@@ -78,13 +81,13 @@ shift $((OPTIND -1)) ## This ensures only non-option arguments are considered ar
if [ $# -gt 1 ]; then ## Check if more than one argument was entered.
echo -e "${RED}Error:${RESET} Too many arguments." ## If so, show usage and exit.
echo -e "${USAGE}"
QUIT ${BAD_USAGE}
QUIT "${TOO_MANY_ARGS}"
fi
if [ "${ARGUMENT_TYPE}" == "FILE" ] && [ $# -lt 1 ]; then ## Check if expected argument is a file but no argument entered.
echo -e "${RED}Error:${RESET} Missing argument." ## If true, show usage and exit.
echo -e "${USAGE}" ## (Note, no argument acceptable for -l option as default list file will be assumed).
QUIT ${BAD_USAGE}
QUIT "${MISSING_ARG}"
fi
##########Validate argument
......@@ -94,7 +97,7 @@ ARGUMENT=${1-"$(dirname "$0")/bu.list"} ## First argument is the file name of t
if [ ! -e "${ARGUMENT}" ]; then ## Check the argument exists
echo -e "${RED}Error:${RESET} File \"${ARGUMENT}\" does not exist."
echo -e "${USAGE}"
QUIT ${BAD_ARGUMENT};
QUIT "${BAD_ARG}"
fi
##########Verify if rsync is installed.
......@@ -167,7 +170,7 @@ else ## Else if argument is not a specific file, assume it is a list of
if [ ! -e "${TEMP_BU_FILE_LIST}" ]; then ## If the temp list file was not created
echo -e "${RED}Error: ${RESET}The list file did not list any valid files." ## Then it didn't contain any valid files.
echo -e "${USAGE}" ## So print usage and exit.
QUIT ${BAD_LIST_FILE}
QUIT "${NO_VALID_FILES}"
fi
fi
fi
......@@ -184,7 +187,7 @@ echo > ${DEST} ## Log file will show rsync was attempted.
if [ "${QUIET_MODE}" != "TRUE" ]; then echo -e "${BLUE}Using rsync to copy listed files to \"${RESET}${BU_USER}@${BU_SERVER}:${BU_REMOTE_DIR}/${BLUE}\"${RESET}"; fi
if ! rsync -4 --recursive --relative --verbose --human-readable --progress --archive --files-from="${TEMP_BU_FILE_LIST}" / "${BU_USER}@${BU_SERVER}:${BU_REMOTE_DIR}/" > ${DEST}; then
echo -e "${RED}Error:${RESET} Sync failed." ## If rsync failed
QUIT ${RSYNC_FAILED}
QUIT "${RSYNC_FAILED}"
else
echo -e "${GREEN}Success.${RESET}"
fi
......@@ -194,4 +197,4 @@ rm "${TEMP_BU_FILE_LIST}"
##########All done.
echo -e "\n${GREEN}Success:${RESET} Script complete.\n" > ${DEST}
QUIT ${SUCCESS}
QUIT "${SUCCESS}"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment