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"
|
2018-09-30 21:32:26 -06:00
|
|
|
err="\e[1;31m"
|
|
|
|
warn="\e[1;93m"
|
|
|
|
ok="\e[32m"
|
|
|
|
lit="\e[93m"
|
|
|
|
op="\e[35m"
|
|
|
|
info="\e[96m"
|
2018-09-19 15:31:07 -06:00
|
|
|
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 17:11:48 -06:00
|
|
|
# exit with code 1 -- 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-20 00:04:10 -06:00
|
|
|
### quit -- exit the script after logging any errors, warnings, etc.
|
2018-09-19 15:36:57 -06:00
|
|
|
function quit {
|
2018-09-19 23:22:57 -06:00
|
|
|
# list generated warnings, if any
|
|
|
|
if [ ${#exitWarn[@]} -gt 0 ]; then
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${warn}${scriptName} generated the following warnings:" \
|
|
|
|
"${normal}" >> "$logFile"
|
|
|
|
for warnCode in "${exitWarn[@]}"; do
|
|
|
|
echo -e "${warn}-- [WARNING] ${warningExplain[$warnCode]}" \
|
|
|
|
"(code: ${warnCode}) --${normal}" >> "$logFile"
|
2018-09-19 23:22:57 -06:00
|
|
|
done
|
|
|
|
fi
|
2018-09-19 15:36:57 -06:00
|
|
|
if [ -z "$1" ]; then
|
|
|
|
# exit cleanly
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${bold}${op}${stamp} -- ${scriptName} completed" \
|
|
|
|
"--${normal}" >> "$logFile"
|
2018-09-19 15:36:57 -06:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
# log error code and exit with said code
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${err}${stamp} -- [ERROR] ${errorExplain[$1]}" \
|
2018-09-30 19:37:26 -06:00
|
|
|
"(code: $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
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
# not found
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
elif [ "$1" = "fd" ]; then
|
|
|
|
# find directory
|
|
|
|
if [ -d "$2" ]; then
|
|
|
|
# found
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
# not found
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-09-20 00:04:10 -06:00
|
|
|
### ncMaint - perform NextCloud maintenance mode entry and exit
|
|
|
|
function ncMaint {
|
|
|
|
if [ "$1" = "on" ]; then
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] Putting NextCloud in maintenance" \
|
|
|
|
"mode --${normal}" >> "$logFile"
|
2018-09-20 00:04:10 -06:00
|
|
|
su -c "php ${ncRoot}/occ maintenance:mode --on" - ${webUser} \
|
|
|
|
>> "$logFile" 2>&1
|
|
|
|
maintResult="$?"
|
|
|
|
return "$maintResult"
|
|
|
|
elif [ "$1" = "off" ]; then
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] Exiting NextCloud maintenance" \
|
|
|
|
"mode --${normal}" >> "$logFile"
|
2018-09-20 00:04:10 -06:00
|
|
|
su -c "php ${ncRoot}/occ maintenance:mode --off" - ${webUser} \
|
|
|
|
>> "$logFile" 2>&1
|
|
|
|
maintResult="$?"
|
|
|
|
return "$maintResult"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-09-20 00:19:59 -06:00
|
|
|
### cleanup - cleanup files and directories created by this script
|
|
|
|
function cleanup {
|
2018-09-30 20:00:39 -06:00
|
|
|
## remove SQL dump file and directory
|
2018-09-20 00:46:10 -06:00
|
|
|
rm -rf "$sqlDumpDir" >> "$logFile" 2>&1
|
2018-09-20 00:19:59 -06:00
|
|
|
# verify directory is gone
|
|
|
|
checkExist fd "$sqlDumpDir"
|
|
|
|
checkResult="$?"
|
|
|
|
if [ "$checkResult" = "0" ]; then
|
|
|
|
# directory still exists
|
|
|
|
exitWarn+=('111')
|
2018-09-20 00:46:10 -06:00
|
|
|
else
|
|
|
|
# directory removed
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${op}${stamp} Removed SQL temp directory${normal}" \
|
2018-09-20 00:46:10 -06:00
|
|
|
>> "$logFile"
|
2018-09-20 00:19:59 -06:00
|
|
|
fi
|
|
|
|
|
2018-09-30 20:00:39 -06:00
|
|
|
## remove 503 error page
|
|
|
|
# check if 503 page was specified to begin with
|
|
|
|
if [ -n "$err503File" ]; then
|
|
|
|
# proceed with cleanup
|
|
|
|
rm -f "$webroot/$err503File" >> "$logFile" 2>&1
|
|
|
|
# verify file is actually gone
|
|
|
|
checkExist ff "$webroot/$err503File"
|
|
|
|
checkResult="$?"
|
|
|
|
if [ "$checkResult" = "0" ]; then
|
|
|
|
# file still exists
|
|
|
|
exitWarn+=('5030')
|
|
|
|
else
|
|
|
|
# file removed
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${op}${stamp} Removed 503 error page" \
|
2018-09-30 20:00:39 -06:00
|
|
|
"from webroot${normal}" >> "$logFile"
|
|
|
|
fi
|
2018-09-20 00:19:59 -06:00
|
|
|
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-30 20:06:25 -06:00
|
|
|
# set borg parameters to 'normal' verbosity
|
|
|
|
borgCreateParams='--stats'
|
|
|
|
borgPruneParams='--list'
|
2018-09-19 15:12:56 -06:00
|
|
|
|
2018-09-30 20:06:25 -06:00
|
|
|
|
|
|
|
### Set script parameters to null and initialize array variables
|
2018-09-19 15:45:59 -06:00
|
|
|
unset PARAMS
|
2018-09-19 17:01:02 -06:00
|
|
|
unset sqlDumpDir
|
2018-09-30 20:08:45 -06:00
|
|
|
unset err503File
|
2018-09-19 22:14:19 -06:00
|
|
|
unset webroot
|
2018-09-30 21:35:43 -06:00
|
|
|
unset ncRoot
|
2018-09-19 15:45:59 -06:00
|
|
|
errorExplain=()
|
2018-09-19 21:48:18 -06:00
|
|
|
exitWarn=()
|
2018-09-19 22:24:24 -06:00
|
|
|
warningExplain=()
|
2018-09-19 15:45:59 -06:00
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
|
2018-09-19 15:51:38 -06:00
|
|
|
### Error codes
|
2018-09-19 23:53:10 -06:00
|
|
|
errorExplain[100]="Could not put NextCloud into Maintenance mode."
|
2018-09-19 15:51:38 -06:00
|
|
|
|
2018-09-19 22:24:24 -06:00
|
|
|
### Warning codes & messages
|
2018-09-20 00:46:10 -06:00
|
|
|
warningExplain[111]="Could not remove SQL dump file and directory. Please remove manually."
|
2018-09-20 00:19:59 -06:00
|
|
|
warningExplain[5030]="Could not remove 503 error page. This MUST be removed manually before NGINX will serve webclients!"
|
2018-09-20 00:46:10 -06:00
|
|
|
warningExplain[5031]="Name of a 503 error page file was not specified (-5 parameter missing)"
|
2018-09-19 22:24:24 -06:00
|
|
|
warningExplain[5032]="The specified 503 error page could not be found"
|
2018-09-20 00:46:10 -06:00
|
|
|
warningExplain[5033]="No webroot path was specified (-r parameter missing)"
|
2018-09-19 22:24:24 -06:00
|
|
|
warningExplain[5034]="The specified webroot could not be found"
|
|
|
|
warningExplain[5035]="Error copying 503 error page to webroot"
|
2018-09-30 21:32:26 -06:00
|
|
|
warn503="Web users will NOT be informed the server is down!"
|
2018-09-19 22:24:24 -06:00
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
### Process script parameters
|
|
|
|
|
2018-09-30 20:24:24 -06:00
|
|
|
# if no parameters provided, of if parameters do not start with a '-' then show
|
|
|
|
# the help page and exit with error
|
2018-09-30 20:13:11 -06:00
|
|
|
if [ -z "$1" ] || [[ ! "$1" =~ ^- ]]; then
|
2018-09-30 20:02:22 -06:00
|
|
|
# show script help page
|
|
|
|
scriptHelp
|
|
|
|
fi
|
2018-09-19 15:12:56 -06:00
|
|
|
|
|
|
|
# use GetOpts to process parameters
|
2018-09-30 21:38:54 -06:00
|
|
|
while getopts ':l:n:v5:w:' 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)
|
2018-09-30 21:35:43 -06:00
|
|
|
# NextCloud webroot
|
|
|
|
ncRoot="${OPTARG}"
|
2018-09-19 16:30:18 -06:00
|
|
|
;;
|
|
|
|
v)
|
2018-09-19 23:09:00 -06:00
|
|
|
# verbose output from Borg
|
|
|
|
borgCreateParams='--list --stats'
|
|
|
|
borgPruneParams='--list'
|
2018-09-19 16:30:18 -06:00
|
|
|
;;
|
2018-09-19 21:53:34 -06:00
|
|
|
5)
|
2018-09-20 00:22:40 -06:00
|
|
|
# 503 error page name
|
|
|
|
err503File="${OPTARG}"
|
2018-09-19 21:53:34 -06:00
|
|
|
;;
|
2018-09-30 21:38:54 -06:00
|
|
|
w)
|
2018-09-30 21:38:04 -06:00
|
|
|
# path to webserver webroot to copy 503 error page
|
2018-09-19 22:14:19 -06:00
|
|
|
webroot="${OPTARG}"
|
|
|
|
;;
|
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-30 20:24:24 -06:00
|
|
|
### Verify script pre-requisties
|
|
|
|
# If not running as root, display error on console and exit
|
2018-09-19 16:04:59 -06:00
|
|
|
if [ $(id -u) -ne 0 ]; then
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${err}This script MUST be run as ROOT. Exiting.${normal}"
|
2018-09-30 19:49:56 -06:00
|
|
|
exit 2
|
2018-09-30 21:35:43 -06:00
|
|
|
#elif [ -z "$ncRoot" ]; then
|
|
|
|
# echo -e "\n${err}The NextCloud webroot must be specified (-n parameter)" \
|
|
|
|
# "${normal}\n"
|
|
|
|
# exit 1
|
2018-09-19 16:04:59 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2018-09-19 16:00:38 -06:00
|
|
|
### Log start of script operations
|
2018-09-30 21:49:15 -06:00
|
|
|
echo -e "${bold}${op}${stamp}-- Start $scriptName execution ---${normal}" \
|
|
|
|
>> "$logFile"
|
2018-09-19 16:00:38 -06:00
|
|
|
|
|
|
|
|
2018-09-19 16:31:52 -06:00
|
|
|
### Export logFile variable for use by Borg
|
|
|
|
export logFile="$logFile"
|
|
|
|
|
|
|
|
|
2018-09-19 17:08:52 -06:00
|
|
|
### Create sqlDump temporary directory and sqlDumpFile name
|
2018-09-19 17:01:02 -06:00
|
|
|
sqlDumpDir=$( mktemp -d )
|
2018-09-19 17:08:52 -06:00
|
|
|
sqlDumpFile="backup-`date +%Y%m%d_%H%M%S`.sql"
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] mySQL dump file will be stored" \
|
|
|
|
"at: ${lit}${sqlDumpDir}/${sqlDumpFile}${normal}" >> "$logFile"
|
2018-09-19 16:00:38 -06:00
|
|
|
|
2018-09-19 15:12:56 -06:00
|
|
|
|
2018-09-19 21:53:34 -06:00
|
|
|
### 503 error page
|
|
|
|
|
|
|
|
# Verify 503 existance
|
2018-09-20 00:22:40 -06:00
|
|
|
if [ -z "$err503File" ]; then
|
2018-09-19 21:53:34 -06:00
|
|
|
# no 503 file has been provided
|
2018-09-30 21:48:15 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" >> "$logFile"
|
2018-09-19 21:53:34 -06:00
|
|
|
exitWarn+=('5031')
|
|
|
|
else
|
2018-09-20 00:22:40 -06:00
|
|
|
checkExist ff "$err503File"
|
2018-09-19 21:53:34 -06:00
|
|
|
checkResult="$?"
|
|
|
|
if [ "$checkResult" = "1" ]; then
|
|
|
|
# 503 file specified could not be found
|
2018-09-30 21:48:15 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \
|
|
|
|
>> "$logFile"
|
2018-09-19 21:53:34 -06:00
|
|
|
exitWarn+=('5032')
|
|
|
|
else
|
2018-09-19 22:43:27 -06:00
|
|
|
# 503 file found
|
|
|
|
# verify webroot exists
|
2018-09-19 22:13:19 -06:00
|
|
|
if [ -z "$webroot" ]; then
|
|
|
|
# no webroot path provided
|
2018-09-30 21:48:15 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \
|
2018-09-30 21:32:26 -06:00
|
|
|
>> "$logFile"
|
2018-09-19 22:13:19 -06:00
|
|
|
exitWarn+=('5033')
|
|
|
|
else
|
2018-09-19 22:43:27 -06:00
|
|
|
# verify provided webroot path exists
|
|
|
|
checkExist fd "$webroot"
|
2018-09-19 22:13:19 -06:00
|
|
|
checkResult="$?"
|
|
|
|
if [ "$checkResult" = "1" ]; then
|
|
|
|
# webroot directory specified could not be found
|
2018-09-30 21:48:15 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \
|
|
|
|
>> "$logFile"
|
2018-09-19 22:13:19 -06:00
|
|
|
exitWarn+=('5034')
|
|
|
|
else
|
|
|
|
# webroot exists and 503 exists, copy 503 to webroot
|
2018-09-20 00:22:40 -06:00
|
|
|
cp "${err503File}" "$webroot/" >> "$logFile" 2>&1
|
2018-09-19 22:43:27 -06:00
|
|
|
copyResult="$?"
|
2018-09-19 22:13:19 -06:00
|
|
|
# verify copy was successful
|
|
|
|
if [ "$copyResult" = "1" ]; then
|
2018-09-19 23:43:12 -06:00
|
|
|
# copy was unsuccessful
|
2018-09-30 21:48:15 -06:00
|
|
|
echo -e "${info}${stamp} -- [INFO] ${warn503}" \
|
2018-09-30 21:32:26 -06:00
|
|
|
"--${normal}" >> "$logFile"
|
2018-09-19 22:13:19 -06:00
|
|
|
exitWarn+=('5035')
|
2018-09-19 23:43:12 -06:00
|
|
|
else
|
|
|
|
# copy was successful
|
2018-09-30 21:32:26 -06:00
|
|
|
echo -e "${op}${stamp} 503 error page" \
|
2018-09-19 23:43:12 -06:00
|
|
|
"copied to webroot${normal}" >> "$logFile"
|
|
|
|
fi
|
2018-09-19 22:13:19 -06:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2018-09-19 23:50:33 -06:00
|
|
|
### Put NextCloud in maintenance mode
|
2018-09-20 00:46:10 -06:00
|
|
|
#ncMaint on
|
2018-09-19 23:50:33 -06:00
|
|
|
# check if successful
|
2018-09-20 00:46:10 -06:00
|
|
|
#if [ "$maintResult" = "0" ]; then
|
|
|
|
# echo -e "${bold}${cyan}${stamp}...done${normal}" >> "$logFile"
|
|
|
|
#else
|
|
|
|
# cleanup 503
|
|
|
|
# quit 100
|
|
|
|
#fi
|
2018-09-19 23:50:33 -06:00
|
|
|
|
2018-09-19 23:23:44 -06:00
|
|
|
### Exit script
|
2018-09-20 00:46:10 -06:00
|
|
|
cleanup
|
2018-09-19 23:23:44 -06:00
|
|
|
quit
|
2018-09-19 21:53:34 -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
|