NextCloudBackup/backup.sh

373 lines
11 KiB
Bash
Raw Normal View History

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"
err="\e[1;31m"
warn="\e[1;93m"
ok="\e[32m"
lit="\e[93m"
op="\e[39m"
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!"
# exit with code 1 -- there is no use logging this
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 {
# list generated warnings, if any
if [ ${#exitWarn[@]} -gt 0 ]; then
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"
done
fi
2018-09-19 15:36:57 -06:00
if [ -z "$1" ]; then
# exit cleanly
echo -e "\e[1;35m${stamp} -- ${scriptName} completed" \
"--${normal}" >> "$logFile"
2018-09-19 15:36:57 -06:00
exit 0
else
# log error code and exit with said code
echo -e "${err}${stamp} -- [ERROR] ${errorExplain[$1]}" \
"(code: $1) --$normal" >> "$logFile"
2018-09-19 15:36:57 -06:00
exit "$1"
fi
}
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
}
### ncMaint - pass requested mode change type to NextCloud occ
2018-09-20 00:04:10 -06:00
function ncMaint {
su -c "php ${ncRoot}/occ maintenance:mode --$1" - "${webUser}" \
2018-09-20 00:04:10 -06:00
>> "$logFile" 2>&1
maintResult="$?"
return "$maintResult"
}
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
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 value of 'clean503' to see if this is necessary (=1) otherwise, skip
if [ "$clean503" -eq 1 ]; then
2018-09-30 20:00:39 -06:00
# proceed with cleanup
echo -e "${op}${stamp} Removing 503 error page..." >> "$logFile"
2018-09-30 20:00:39 -06:00
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
echo -e "${info}${stamp} -- [INFO] 503 page removed from webroot" \
"--${normal}" >> "$logFile"
2018-09-30 20:00:39 -06:00
fi
else
echo -e "${op}${stamp} 503 error page never copied to webroot," \
"nothing to cleanup" >> "$logFile"
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"
# set default 503 error page name and location in scriptPath
err503Path="$scriptPath/503.html"
err503File="${err503Path##*/}"
2018-10-03 15:36:18 -06:00
# set default sqlDetails path to scriptPath
sqlDetails="$scriptPath/nc_sql.details"
# set borg parameters to 'normal' verbosity
borgCreateParams='--stats'
borgPruneParams='--list'
2018-09-19 15:12:56 -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-19 22:14:19 -06:00
unset webroot
2018-09-30 21:35:43 -06:00
unset ncRoot
2018-10-03 13:57:02 -06:00
unset webUser
unset clean503
2018-09-19 15:45:59 -06:00
errorExplain=()
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
errorExplain[100]="Could not put NextCloud into maintenance mode"
errorExplain[101]="Could not exit NextCloud maintenance mode"
2018-09-19 15:51:38 -06:00
2018-09-19 22:24:24 -06:00
### Warning codes & messages
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!"
warningExplain[5031]="No webroot path was specified (-w parameter missing)"
warningExplain[5032]="The specified webroot (-w parameter) could not be found"
warningExplain[5033]="No 503 error page could be found. If not using the default located in the script directory, then check your -5 parameter"
2018-09-19 22:24:24 -06:00
warningExplain[5035]="Error copying 503 error page to webroot"
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
# If parameters are provided but don't start with '-' then show the help page
# and exit with an error
if [ -n "$1" ] && [[ ! "$1" =~ ^- ]]; then
# show script help page
scriptHelp
fi
2018-09-19 15:12:56 -06:00
# use GetOpts to process parameters
2018-10-03 15:34:44 -06:00
while getopts ':l:n:u:v5:w:s:' 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
2018-10-03 14:04:18 -06:00
ncRoot="${OPTARG%/}"
2018-09-19 16:30:18 -06:00
;;
2018-10-03 13:57:02 -06:00
u)
# webuser
webUser="${OPTARG}"
;;
2018-09-19 16:30:18 -06:00
v)
# 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)
# Full path to 503 error page
err503Path="${OPTARG%/}"
err503File="${err503Path##*/}"
2018-09-19 21:53:34 -06:00
;;
2018-09-30 21:38:54 -06:00
w)
# path to webserver webroot to copy 503 error page
webroot="${OPTARG%/}"
2018-09-19 22:14:19 -06:00
;;
2018-10-03 15:34:44 -06:00
s)
# path to file containing SQL login details
sqlDetails="${OPTARG%/}"
;;
2018-09-19 15:41:44 -06:00
?)
# unrecognized parameters trigger scriptHelp
scriptHelp
2018-09-19 15:41:44 -06:00
;;
2018-09-19 15:12:56 -06:00
esac
done
### Verify script pre-requisties
## If not running as root, display error on console and exit
if [ $(id -u) -ne 0 ]; then
2018-10-03 13:57:02 -06:00
echo -e "\n${err}This script MUST be run as ROOT. Exiting.${normal}"
exit 2
fi
## Check NextCloud webroot
2018-10-03 14:04:18 -06:00
# Ensure NextCloud webroot is provided
if [ -z "$ncRoot" ]; then
2018-10-03 13:37:15 -06:00
echo -e "\n${err}The NextCloud webroot must be specified (-n parameter)" \
"${normal}\n"
exit 1
2018-10-03 14:04:18 -06:00
# Ensure NextCloud webroot directory exists
elif [ -n "$ncRoot" ]; then
checkExist fd "$ncRoot"
checkResult="$?"
if [ "$checkResult" = "1" ]; then
# Specified NextCloud webroot directory could not be found
echo -e "\n${err}The provided NextCloud webroot directory" \
"(-n parameter) does not exist.${normal}\n"
exit 1
fi
fi
## Check NextCloud webuser account
2018-10-03 13:57:02 -06:00
# Ensure NextCloud webuser account is provided
if [ -z "$webUser" ]; then
2018-10-03 13:57:02 -06:00
echo -e "\n${err}The webuser account running NextCloud must be provided" \
"(-u parameter)${normal}\n"
exit 1
# Check if supplied webUser account exists
elif [ -n "$webUser" ]; then
user_exists=$(id -u $webUser > /dev/null 2>&1; echo $?)
if [ $user_exists -ne 0 ]; then
echo -e "\n${err}The supplied webuser account (-u parameter) does not" \
"exist.${normal}\n"
exit 1
fi
fi
2018-09-19 16:00:38 -06:00
### Log start of script operations
echo -e "\e[1;35m${stamp}-- Start $scriptName execution ---${normal}" \
2018-09-30 21:49:15 -06:00
>> "$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"
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
### 503 error page: If you dont' plan on using the auto-copied 503 then comment
### this entire section starting with '--- Begin 503 section ---' until
### '--- End 503 section ---' to suppress generated warnings
### --- Begin 503 section ---
## Check if webroot has been specified, if not, skip this entire section since there is nowhere to copy the 503 file.
if [ -z "$webroot" ]; then
# no webroot path provided
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \
>> "$logFile"
2018-10-01 23:19:11 -06:00
exitWarn+=('5031')
clean503=0
2018-09-19 21:53:34 -06:00
else
# verify webroot actually exists
checkExist fd "$webroot"
checkResult="$?"
if [ "$checkResult" = "1" ]; then
# webroot directory specified could not be found
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \
>> "$logFile"
2018-10-01 23:19:11 -06:00
exitWarn+=('5032')
clean503=0
else
# webroot exists
echo -e "${op}${stamp} Using webroot: ${lit}${webroot}${normal}" \
2018-09-30 21:48:15 -06:00
>> "$logFile"
# Verify 503 file existance at given path
checkExist ff "$err503Path"
checkResult="$?"
if [ "$checkResult" = "1" ]; then
# 503 file could not be found
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \
>> "$logFile"
2018-10-01 23:19:11 -06:00
exitWarn+=('5033')
clean503=0
2018-09-19 22:13:19 -06:00
else
# 503 file exists and webroot is valid. Let's copy it!
echo -e "${op}${stamp} ${err503File} found at ${lit}${err503Path}" \
"${normal}" >> "$logFile"
echo -e "${op}${stamp} Copying 503 error page to webroot..." \
"${normal}" >> "$logFile"
cp "${err503Path}" "$webroot/" >> "$logFile" 2>&1
copyResult="$?"
# verify copy was successful
if [ "$copyResult" = "1" ]; then
# copy was unsuccessful
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \
>> "$logFile"
exitWarn+=('5035')
clean503=0
else
# copy was successful
echo -e "${info}${stamp} -- [INFO] 503 error page" \
"successfully copied to webroot --${normal}" >> "$logFile"
clean503=1
fi
2018-09-19 22:13:19 -06:00
fi
fi
fi
### --- End 503 section ---
2018-09-19 22:13:19 -06:00
2018-09-19 23:50:33 -06:00
### Put NextCloud in maintenance mode
ncMaint on
# check if successful
if [ "$maintResult" = "0" ]; then
echo -e "${info}${stamp} -- [INFO] NextCloud now in maintenance mode --" \
"${normal}" >> "$logFile"
else
cleanup 503
quit 100
fi
### Exit NextCloud maintenance mode
ncMaint off
2018-09-19 23:50:33 -06:00
# check if successful
if [ "$maintResult" = "0" ]; then
echo -e "${info}${stamp} -- [INFO] NextCloud now in normal operating mode" \
"--${normal}" >> "$logFile"
else
cleanup 503
quit 101
fi
2018-09-19 23:50:33 -06:00
### Exit script
echo -e "${bold}${default}${stamp} ***Normal exit process***${normal}" \
>> "$logFile"
2018-09-20 00:46:10 -06:00
cleanup
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