2018-09-19 15:12:56 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-09-19 15:31:07 -06:00
|
|
|
|
|
|
|
### Text formatting presets
|
|
|
|
normal="\e[0m"
|
|
|
|
bold="\e[1m"
|
|
|
|
default="\e[39m"
|
|
|
|
red="\e[31m"
|
|
|
|
green="\e[32m"
|
|
|
|
yellow="\e[33m"
|
|
|
|
magenta="\e[35m"
|
|
|
|
cyan="\e[36m"
|
|
|
|
stamp="[`date +%Y-%m-%d` `date +%H:%M:%S`]"
|
|
|
|
|
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
### Functions ###
|
|
|
|
|
|
|
|
### scriptHelp -- display usage information for this script
|
|
|
|
function scriptHelp {
|
|
|
|
echo "In the future, I will be something helpful!"
|
2018-09-19 15:57:06 -06:00
|
|
|
# exit with code 98 -- there is no use logging this
|
2018-09-19 16:04:59 -06:00
|
|
|
exit 1
|
2018-09-19 15:12:56 -06:00
|
|
|
}
|
|
|
|
|
2018-09-19 15:36:57 -06:00
|
|
|
### quit -- exit the script after logging any errors, warnings, etc. and
|
|
|
|
### cleaning up as necessary
|
|
|
|
function quit {
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
# exit cleanly
|
|
|
|
echo -e "${bold}${green}${stamp} -- [SUCCESS] Script completed" \
|
2018-09-19 15:50:50 -06:00
|
|
|
"--$normal" >> "$logFile"
|
2018-09-19 15:36:57 -06:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
# log error code and exit with said code
|
|
|
|
echo -e "${bold}${red}${stamp} -- [ERROR] Script exited with code $1" \
|
2018-09-19 15:50:50 -06:00
|
|
|
" --$normal" >> "$logFile"
|
|
|
|
echo -e "${red}${errorExplain[$1]}$normal" >> "$logFile"
|
2018-09-19 15:36:57 -06:00
|
|
|
exit "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-09-19 16:45:37 -06:00
|
|
|
function checkExist {
|
|
|
|
if [ "$1" = "ff" ]; then
|
|
|
|
# find file
|
|
|
|
if [ -e "$2" ]; then
|
|
|
|
# found
|
|
|
|
echo -e "${normal}${stamp} File found: ${yellow}${2}${normal}" \
|
|
|
|
>> $logFileVerbose
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
# not found
|
|
|
|
echo -e "${red}${stamp} File NOT found: ${yellow}${2}${normal}" \
|
|
|
|
>> $logFileVerbose
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
elif [ "$1" = "fd" ]; then
|
|
|
|
# find directory
|
|
|
|
if [ -d "$2" ]; then
|
|
|
|
# found
|
|
|
|
echo -e "${normal}${stamp} Dir found: ${yellow}${2}${normal}" \
|
|
|
|
>> $logFileVerbose
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
# not found
|
|
|
|
echo -e "${red}${stamp} Dir NOT found: ${yellow}${2}${normal}" \
|
|
|
|
>> $logFileVerbose
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
### End of Functions ###
|
|
|
|
|
|
|
|
|
|
|
|
### Default parameters
|
|
|
|
|
|
|
|
# store the logfile in the same directory as this script using the script's name
|
|
|
|
# with the extension .log
|
|
|
|
scriptPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
scriptName="$( basename ${0} )"
|
|
|
|
logFile="$scriptPath/${scriptName%.*}.log"
|
|
|
|
|
2018-09-19 15:45:59 -06:00
|
|
|
# set script parameters to null and initialize array variables
|
|
|
|
unset PARAMS
|
2018-09-19 16:30:18 -06:00
|
|
|
unset logLevel
|
|
|
|
unset logFileNormal
|
|
|
|
unset logFileVerbose
|
|
|
|
unset borgCreateParams
|
|
|
|
unset borgPruneParams
|
2018-09-19 15:45:59 -06:00
|
|
|
errorExplain=()
|
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
|
2018-09-19 15:51:38 -06:00
|
|
|
### Error codes
|
2018-09-19 16:09:27 -06:00
|
|
|
errorExplain[2]="This script MUST be run as ROOT."
|
2018-09-19 15:51:38 -06:00
|
|
|
|
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
### Process script parameters
|
|
|
|
|
|
|
|
# if no parameters provided, then show the help page and exit with error
|
|
|
|
if [ -z $1 ]; then
|
|
|
|
# show script help page
|
2018-09-19 15:57:06 -06:00
|
|
|
scriptHelp
|
2018-09-19 15:12:56 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
# use GetOpts to process parameters
|
2018-09-19 16:30:18 -06:00
|
|
|
while getopts ':l:nv' PARAMS; do
|
2018-09-19 15:12:56 -06:00
|
|
|
case "$PARAMS" in
|
|
|
|
l)
|
|
|
|
# use provided location for logFile
|
|
|
|
logFile="${OPTARG}"
|
|
|
|
;;
|
2018-09-19 16:30:18 -06:00
|
|
|
n)
|
|
|
|
# standard logging (script errors, Borg summary)
|
|
|
|
logLevel="normal"
|
|
|
|
;;
|
|
|
|
v)
|
|
|
|
# verbose logging (script errors, Borg details)
|
|
|
|
logLevel="verbose"
|
|
|
|
;;
|
2018-09-19 15:41:44 -06:00
|
|
|
?)
|
|
|
|
# unrecognized parameters trigger scriptHelp
|
2018-09-19 15:57:06 -06:00
|
|
|
scriptHelp
|
2018-09-19 15:41:44 -06:00
|
|
|
;;
|
2018-09-19 15:12:56 -06:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2018-09-19 16:09:27 -06:00
|
|
|
### Verify script running as root, otherwise exit
|
2018-09-19 16:04:59 -06:00
|
|
|
if [ $(id -u) -ne 0 ]; then
|
2018-09-19 16:09:27 -06:00
|
|
|
quit 2
|
2018-09-19 16:04:59 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2018-09-19 16:50:32 -06:00
|
|
|
### Check for mandatory parameters
|
|
|
|
if [ -z "$sqlDumpDir" ] || [[ "$sqlDumpDir" == -* ]]; then
|
|
|
|
quit 101
|
|
|
|
fi
|
|
|
|
|
2018-09-19 16:30:18 -06:00
|
|
|
### Set logging verbosity based on invocation parameters
|
|
|
|
if [ "$logLevel" = "normal" ]; then
|
|
|
|
borgCreateParams='--stats'
|
|
|
|
borgPruneParams="--list"
|
|
|
|
unset logFileVerbose
|
|
|
|
logFileNormal="$logFile"
|
|
|
|
elif [ "$logLevel" = "verbose" ]; then
|
|
|
|
borgCreateParams='--list --stats'
|
|
|
|
borgPruneParams='--list'
|
|
|
|
logFileVerbose="$logFile"
|
|
|
|
unset logFileNormal
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2018-09-19 16:00:38 -06:00
|
|
|
### Log start of script operations
|
|
|
|
echo -e "${bold}${stamp}-- Start $scriptName execution ---" >> "$logFile"
|
|
|
|
|
|
|
|
|
2018-09-19 16:31:52 -06:00
|
|
|
### Export logFile variable for use by Borg
|
|
|
|
export logFile="$logFile"
|
|
|
|
|
|
|
|
|
2018-09-19 16:00:38 -06:00
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
|
|
|
|
# This code should not be executed since the 'quit' function should terminate
|
|
|
|
# this script. Therefore, exit with code 99 if we get to this point.
|
|
|
|
exit 99
|