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"
2018-09-30 21:54:27 -06:00
op = "\e[39m"
2018-09-30 21:32:26 -06:00
info = "\e[96m"
2018-10-14 23:05:52 -06:00
note = "\e[35m"
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 {
2018-10-14 23:05:52 -06:00
echo -e " ${ bold } ${ note } \n ${ scriptName } usage instructions:\n ${ normal } "
echo -e " ${ default } This script performs a backup of your NextCloud system "
echo -e "assuming a fairly standard set up such as outlined at"
echo -e " ${ lit } https://mytechiethoughts.com ${ default } . Full details about "
echo -e "this script can be found at that site."
echo -e " ${ bold } \nThe script performs the following tasks: ${ normal } ${ default } "
echo -e "1. Puts NextCloud in maintenance mode."
echo -e "2. Optionally copies a 503 error page to your webroot."
echo -e "3. Dumps SQL to a temporary directory."
echo -e "4. Invokes borgbackup to backup your SQL info, NextCloud program"
echo -e "\tand data files along with any other files you specify."
echo -e "5. Removes temp files, the 503 error page and restores"
echo -e "\tNextCloud to operational status."
echo -e "\nThe readme file included in this script's git contains detailed"
echo -e "usage information. The following is a brief summary:\n"
echo -e " ${ bold } ${ note } Mandatory parameters: ${ normal } ${ default } "
echo -e " ${ lit } \n-d, NextCloud data directory ${ default } "
echo -e "This is the physical location of your NextCloud data."
echo -e " ${ lit } \n-n, NextCloud webroot ${ default } "
echo -e "This is the location of the actual NextCloud web files (php & html"
echo -e ",etc.), usually within your NGINX or Apache webroot."
echo -e " ${ lit } \n-u, NGINX/Apache webuser account ${ default } "
echo -e "The webuser account NGINX/Apache (and thus, NextCloud) are running"
echo -e "under. Some actions must run as this user due to file ownership"
echo -e "restrictions."
echo -e " ${ bold } ${ note } \nOptional parameters: ${ normal } ${ default } "
echo -e " ${ lit } \n-5, Location of 503 error page file ${ default } "
echo -e "FULL PATH to the 503 error page HTML file you want copied to your"
echo -e "webroot to inform users the server is down during the backup. If"
echo -e "you don't specify a path/file, the default will be used. If the"
echo -e "default cannot be found, a warning will be logged and the script"
echo -e "will continue."
echo -e " ${ info } Default: ScriptPath/503.html ${ default } "
echo -e " ${ lit } \n-b, Location of file with borg repo details ${ default } "
echo -e "FULL PATH to the plain text file containing all information needed"
echo -e "to connect and process your borg repo. Details on the structure of"
echo -e " this file are in the readme and on ${ lit } https://mytechiethoughts.com ${ default } "
echo -e " ${ info } Default: ScriptPath/nc_borg.details ${ default } "
echo -e " ${ lit } \n-l, Location to save log file ${ default } "
echo -e "This script writes a detailed log file of all activities. It is"
echo -e "structured in an way easy for log parsers (like Logwatch) to read."
echo -e " ${ info } Default: ScriptPath/ScriptName.log ${ default } "
echo -e " ${ lit } \n-s, Location of file with mySQL details ${ default } "
echo -e "FULL PATH to the plain text file containing all information needed"
echo -e "to connect to your mySQL (mariaDB) server and NextCloud database."
echo -e " Details on the structure of this file are in the readme and on ${ lit } "
echo -e " https://mytechiethoughts.com ${ default } "
echo -e " ${ info } Default: ScriptPath/nc_sql.details ${ default } "
echo -e " ${ lit } \n-v, Verbose output from borgbackup ${ default } "
echo -e "By default, this script will only log summary data from borg."
echo -e "If you need/want more detailed information, the verbose setting"
echo -e "will list every file processed along with their status. Note: Your"
echo -e "log file can quickly get very very large using this option!"
echo -e " ${ info } Default: NOT activated (standard logging) ${ default } "
echo -e " ${ lit } \n-w, webserver's webroot directory ${ default } "
echo -e "This is the location from which your webserver (NGINX, Apache,"
echo -e "etc.) physically stores files to be served. This is NOT the"
echo -e "configuration directory for your webserver! It is the place"
echo -e "where the actual HTML/PHP/CSS/JS/etc. files are stored."
echo -e "NOTE: If you omit this option, then the entire 503 copy process"
echo -e "will be skipped regardless of the presence of a 503.html file."
echo -e "If you don't want to use the 503 feature, omitting this is an easy"
echo -e "way to skip it!"
echo -e " ${ info } Default: NONE ${ default } "
echo -e " ${ lit } \n-?, This help screen ${ default } \n "
echo -e " ${ bold } Please refer to the readme file and/or ${ lit } https://mytechiethoughts.com ${ default } "
echo -e " for more information on this script. ${ normal } \n "
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-10-14 10:32:59 -06:00
if [ -z " ${ exitError } " ] ; then
2018-09-19 15:36:57 -06:00
# exit cleanly
2018-10-14 23:05:52 -06:00
echo -e " ${ note } ${ stamp } -- ${ scriptName } completed " \
2018-09-30 21:32:26 -06:00
" -- ${ normal } " >> " $logFile "
2018-09-19 15:36:57 -06:00
exit 0
else
2018-10-14 10:32:59 -06:00
# list generated errors and explanations then exit script with code 2
echo -e " ${ err } ${ scriptName } generated the following errors: " \
" ${ normal } " >> " $logFile "
for errCode in " ${ exitError [@] } " ; do
echo -e " ${ err } -- [ERROR] ${ errorExplain [ $errCode ] } " \
" (code: ${ errCode } ) -- $normal " >> " $logFile "
done
exit 2
2018-09-19 15:36:57 -06:00
fi
}
2018-09-19 16:45:37 -06:00
function checkExist {
if [ " $1 " = "ff" ] ; then
# find file
2018-10-03 15:58:40 -06:00
if [ -f " $2 " ] ; then
2018-09-19 16:45:37 -06:00
# 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-10-03 15:01:10 -06:00
### ncMaint - pass requested mode change type to NextCloud occ
2018-09-20 00:04:10 -06:00
function ncMaint {
2018-10-03 15:01:10 -06:00
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
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
2018-10-02 00:07:32 -06:00
# 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
2018-10-02 00:07:32 -06:00
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
2018-10-01 23:33:57 -06:00
# file removed
echo -e " ${ info } ${ stamp } -- [INFO] 503 page removed from webroot " \
" -- ${ normal } " >> " $logFile "
2018-09-30 20:00:39 -06:00
fi
2018-10-01 23:33:57 -06:00
else
2018-10-02 00:07:32 -06:00
echo -e " ${ op } ${ stamp } 503 error page never copied to webroot, " \
2018-10-02 00:18:00 -06:00
"nothing to cleanup" >> " $logFile "
2018-09-20 00:19:59 -06:00
fi
2018-10-14 10:22:35 -06:00
## Exit NextCloud maintenance mode regardless of current status
ncMaint off
# check if successful
if [ " $maintResult " = "0" ] ; then
echo -e " ${ info } ${ stamp } -- [INFO] NextCloud now in regular " \
" operating mode -- ${ normal } " >> " $logFile "
else
2018-10-14 10:37:44 -06:00
exitError += ( '101' )
quit
2018-10-14 10:22:35 -06:00
fi
2018-09-20 00:19:59 -06:00
}
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-10-01 23:23:06 -06:00
# set default 503 error page name and location in scriptPath
2018-10-01 23:57:57 -06:00
err503Path = " $scriptPath /503.html "
err503File = " ${ err503Path ##*/ } "
2018-10-01 23:23:06 -06:00
2018-10-03 15:36:18 -06:00
# set default sqlDetails path to scriptPath
sqlDetails = " $scriptPath /nc_sql.details "
2018-10-03 17:41:57 -06:00
# set default borgDetails path to scriptPath
2018-10-14 09:50:10 -06:00
borgDetails = " $scriptPath /nc_borg.details "
2018-10-03 17:41:57 -06:00
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-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
2018-10-02 00:07:32 -06:00
unset clean503
2018-10-03 16:28:35 -06:00
unset sqlParams
2018-10-14 06:47:22 -06:00
unset ncDataDir
2018-10-05 16:27:04 -06:00
unset borgXtra
unset borgExclude
unset borgPrune
2018-10-14 05:48:34 -06:00
unset BORG_BASE_DIR
unset BORG_RSH
unset BORG_REPO
unset BORG_PASSPHRASE
unset BORG_REMOTE_PATH
2018-10-14 10:32:59 -06:00
exitError = ( )
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-10-05 16:27:04 -06:00
borgConfig = ( )
xtraFiles = ( )
2018-09-19 15:12:56 -06:00
2018-09-19 15:51:38 -06:00
### Error codes
2018-10-03 15:01:10 -06:00
errorExplain[ 100] = "Could not put NextCloud into maintenance mode"
errorExplain[ 101] = "Could not exit NextCloud maintenance mode"
2018-10-03 16:28:35 -06:00
errorExplain[ 200] = "Could not dump NextCloud SQL database"
2018-10-14 06:17:35 -06:00
errorExplain[ 210] = "Invalid or non-existant borg base directory specified (borg backup details file)"
errorExplain[ 211] = "Invalid or non-existant path to borg SSH keyfile (borg backup details file)"
errorExplain[ 212] = "Name of borg repo was not specified (borg backup details file)"
2018-10-14 07:07:21 -06:00
errorExplain[ 220] = "Borg exited with a critical error. Please check this script's logfile for details"
2018-10-14 15:40:43 -06:00
errorExplain[ 221] = "Borg prune exited with ERRORS. Please check this script's logfile for details"
2018-09-19 15:51:38 -06:00
2018-09-19 22:24:24 -06:00
### Warning codes & messages
2018-10-02 00:18:00 -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-10-02 00:18:00 -06:00
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"
2018-09-30 21:32:26 -06:00
warn503 = "Web users will NOT be informed the server is down!"
2018-10-14 14:55:27 -06:00
warningExplain[ 2111] = "No password used for SSH keys or access to remote borg repo. This is an insecure configuration"
2018-10-14 14:55:05 -06:00
warningExplain[ 2112] = "No remote borg instance specified. Operations will be slower in this configuration"
warningExplain[ 2113] = "The specified file containing extra files for inclusion in borgbackup could not be found"
2018-10-14 16:54:57 -06:00
warningExplain[ 2114] = "The specified file containing exclusion patterns for borgbackup could not be found. Backup was performed as though NO exclusions were defined"
2018-10-14 14:55:05 -06:00
warningExplain[ 2115] = "No paramters provided for borg prune. No repo pruning has taken place. You should reconsider this decision to control the size/history of your backups"
warningExplain[ 2200] = "Borg completed with warnings. Please check this script's logfile for details"
warningExplain[ 2201] = "Borg exited with an unknown return-code. Please check this script's logfile for details"
warningExplain[ 2210] = "Borg prune exited with warnings. Please check this script's logfile for details"
warningExplain[ 2212] = "Borg prune exited with an unknown return-code. Please check this script's logfile for details"
2018-10-14 07:07:21 -06:00
2018-09-19 22:24:24 -06:00
2018-09-19 15:12:56 -06:00
### Process script parameters
2018-10-01 23:26:04 -06:00
# If parameters are provided but don't start with '-' then show the help page
# and exit with an error
if [ -n " $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-10-14 06:47:22 -06:00
while getopts ':l:n:u:v5:w:s:b:d:' 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)
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-10-01 23:57:57 -06:00
# Full path to 503 error page
err503Path = " ${ OPTARG %/ } "
2018-10-02 00:22:48 -06:00
err503File = " ${ err503Path ##*/ } "
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-30 22:22:48 -06:00
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-10-03 17:41:57 -06:00
b)
# path to file containing borgbackup settings and details
borgDetails = " ${ OPTARG %/ } "
; ;
2018-10-14 06:47:22 -06:00
d)
# nextcloud data directory
ncDataDir = " ${ 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
2018-10-03 15:38:35 -06:00
## 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-10-03 13:57:02 -06:00
echo -e " \n ${ err } This script MUST be run as ROOT. Exiting. ${ normal } "
2018-09-30 19:49:56 -06:00
exit 2
2018-10-03 15:38:35 -06:00
fi
## Check NextCloud webroot
2018-10-03 14:04:18 -06:00
# Ensure NextCloud webroot is provided
2018-10-03 15:38:35 -06:00
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
2018-10-03 15:38:35 -06:00
fi
## Check NextCloud webuser account
2018-10-03 13:57:02 -06:00
# Ensure NextCloud webuser account is provided
2018-10-03 15:38:35 -06:00
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
2018-09-19 16:04:59 -06:00
fi
2018-10-03 15:57:30 -06:00
## Ensure sqlDetails file exists
2018-10-03 16:04:05 -06:00
checkExist ff " $sqlDetails "
checkResult = " $? "
if [ " $checkResult " = "1" ] ; then
# sqlDetails file cannot be found
2018-10-03 15:57:30 -06:00
echo -e " \n ${ err } The file containing your SQL details does not exist " \
" (-s parameter) ${ normal } \n "
exit 1
fi
2018-10-03 17:41:57 -06:00
## Ensure borgDetails file exists
checkExist ff " $borgDetails "
checkResult = " $? "
if [ " $checkResult " = "1" ] ; then
# sqlDetails file cannot be found
echo -e " \n ${ err } The file containing your borgbackup details does not " \
" exist (-b parameter) ${ normal } \n "
exit 1
fi
2018-10-14 10:00:48 -06:00
## Check NextCloud data directory
# Ensure NextCloud data directory is provided
if [ -z " $ncDataDir " ] ; then
echo -e " \n ${ err } The NextCloud data directory must be specified " \
" (-d parameter) ${ normal } \n "
exit 1
# Ensure NextCloud data directory exists
elif [ -n " $ncDataDir " ] ; then
checkExist fd " $ncDataDir "
checkResult = " $? "
if [ " $checkResult " = "1" ] ; then
# Specified NextCloud data directory could not be found
echo -e " \n ${ err } The provided NextCloud data directory " \
" (-d parameter) does not exist. ${ normal } \n "
exit 1
fi
fi
2018-09-19 16:04:59 -06:00
2018-09-19 16:00:38 -06:00
### Log start of script operations
2018-10-14 23:05:52 -06:00
echo -e " ${ note } ${ 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"
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-10-01 23:02:52 -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' )
2018-10-02 00:07:32 -06:00
clean503 = 0
2018-09-19 21:53:34 -06:00
else
2018-10-01 23:02:52 -06:00
# 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' )
2018-10-02 00:07:32 -06:00
clean503 = 0
2018-10-01 23:02:52 -06:00
else
2018-10-01 23:10:20 -06:00
# webroot exists
echo -e " ${ op } ${ stamp } Using webroot: ${ lit } ${ webroot } ${ normal } " \
2018-09-30 21:48:15 -06:00
>> " $logFile "
2018-10-01 23:59:26 -06:00
# Verify 503 file existance at given path
checkExist ff " $err503Path "
2018-10-01 23:10:20 -06:00
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' )
2018-10-02 00:07:32 -06:00
clean503 = 0
2018-09-19 22:13:19 -06:00
else
2018-10-01 23:10:20 -06:00
# 503 file exists and webroot is valid. Let's copy it!
2018-10-02 00:18:00 -06:00
echo -e " ${ op } ${ stamp } ${ err503File } found at ${ lit } ${ err503Path } " \
" ${ normal } " >> " $logFile "
2018-10-01 23:10:20 -06:00
echo -e " ${ op } ${ stamp } Copying 503 error page to webroot... " \
" ${ normal } " >> " $logFile "
2018-10-01 23:59:26 -06:00
cp " ${ err503Path } " " $webroot / " >> " $logFile " 2>& 1
2018-10-01 23:10:20 -06:00
copyResult = " $? "
# verify copy was successful
if [ " $copyResult " = "1" ] ; then
# copy was unsuccessful
echo -e " ${ info } ${ stamp } -- [INFO] ${ warn503 } -- ${ normal } " \
>> " $logFile "
exitWarn += ( '5035' )
2018-10-02 00:07:32 -06:00
clean503 = 0
2018-10-01 23:10:20 -06:00
else
# copy was successful
echo -e " ${ info } ${ stamp } -- [INFO] 503 error page " \
" successfully copied to webroot -- ${ normal } " >> " $logFile "
2018-10-02 00:07:32 -06:00
clean503 = 1
2018-10-01 23:10:20 -06:00
fi
2018-09-19 22:13:19 -06:00
fi
fi
fi
2018-10-01 23:10:20 -06:00
### --- End 503 section ---
2018-09-19 22:13:19 -06:00
2018-09-19 23:50:33 -06:00
### Put NextCloud in maintenance mode
2018-10-03 15:01:10 -06:00
ncMaint on
# check if successful
if [ " $maintResult " = "0" ] ; then
echo -e " ${ info } ${ stamp } -- [INFO] NextCloud now in maintenance mode -- " \
" ${ normal } " >> " $logFile "
else
2018-10-14 10:37:44 -06:00
exitError += ( '100' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-03 15:01:10 -06:00
fi
2018-10-03 16:28:35 -06:00
### Get SQL info from sqlDetails
mapfile -t sqlParams < " $sqlDetails "
### Dump SQL
echo -e " ${ op } ${ stamp } Dumping NextCloud SQL database... ${ normal } " >> " $logFile "
mysqldump --single-transaction -h" ${ sqlParams [0] } " -u" ${ sqlParams [1] } " \
-p" ${ sqlParams [2] } " " ${ sqlParams [3] } " > " ${ sqlDumpDir } / ${ sqlDumpFile } " \
2>> " $logFile "
# verify
dumpResult = " $? "
if [ " $dumpResult " = "0" ] ; then
echo -e " ${ ok } ${ stamp } -- [SUCCESS] SQL dumped successfully -- ${ normal } " \
>> " $logFile "
else
2018-10-14 10:37:44 -06:00
exitError += ( '200' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-03 16:28:35 -06:00
fi
2018-10-03 17:41:57 -06:00
### Call borgbackup to copy actual files
2018-10-14 06:13:16 -06:00
echo -e " ${ op } ${ stamp } Pre-backup tasks completed, calling borgbackup... " \
" ${ normal } " >> " $logFile "
2018-10-03 17:41:57 -06:00
## Get borgbackup settings and repo details
2018-10-05 16:27:04 -06:00
# read definition file and map to array variable
2018-10-14 06:13:16 -06:00
mapfile -t borgConfig < " $borgDetails "
## check if any required borg configuration variables in defintion file are
## empty and exit with error, otherwise, map array items to variables
# check: borg base directory
2018-10-14 10:50:50 -06:00
echo -e " ${ op } ${ stamp } Verifying supplied borg configuration variables... " \
" ${ normal } " >> " $logFile "
2018-10-14 06:13:16 -06:00
if [ -z " ${ borgConfig [0] } " ] ; then
2018-10-14 10:37:44 -06:00
exitError += ( '210' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-14 06:13:16 -06:00
else
2018-10-14 06:21:01 -06:00
# verify the path actually exists
checkExist fd " ${ borgConfig [0] } "
checkResult = " $? "
if [ " $checkResult " = "1" ] ; then
# borg base directory specified could not be found
2018-10-14 10:37:44 -06:00
exitError += ( '210' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-14 06:21:01 -06:00
fi
echo -e " ${ op } ${ stamp } Borg base dir... OK ${ normal } " >> " $logFile "
2018-10-14 06:13:16 -06:00
export BORG_BASE_DIR = " ${ borgConfig [0] } "
fi
# check: path to SSH keyfile
if [ -z " ${ borgConfig [1] } " ] ; then
2018-10-14 10:37:44 -06:00
exitError += ( '211' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-14 06:13:16 -06:00
else
2018-10-14 06:24:37 -06:00
checkExist ff " ${ borgConfig [1] } "
checkResult = " $? "
if [ " $checkResult " = 1 ] ; then
# SSH keyfile specified could not be found
2018-10-14 10:37:44 -06:00
exitError += ( '211' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-14 06:24:37 -06:00
fi
echo -e " ${ op } ${ stamp } Borg SSH key... OK ${ normal } " >> " $logFile "
2018-10-14 06:13:16 -06:00
export BORG_RSH = " ssh -i ${ borgConfig [1] } "
fi
# check: name of borg repo
if [ -z " ${ borgConfig [2] } " ] ; then
2018-10-14 10:37:44 -06:00
exitError += ( '212' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-14 06:13:16 -06:00
else
2018-10-14 14:59:48 -06:00
export BORG_REPO = " ${ borgConfig [2] } "
2018-10-14 06:13:16 -06:00
fi
# repo password
if [ -n " ${ borgConfig [3] } " ] ; then
2018-10-14 14:59:48 -06:00
export BORG_PASSPHRASE = " ${ borgConfig [3] } "
2018-10-14 06:13:16 -06:00
else
2018-10-14 14:55:05 -06:00
exitWarn += ( '2111' )
2018-10-14 06:13:16 -06:00
# if the password was omitted by mistake, export a dummy password so borg
# fails with an error instead of sitting and waiting for input
export BORG_PASSPHRASE = "DummyPasswordSoBorgFails"
fi
# additional files to be backed up
borgXtra = " ${ borgConfig [4] } "
# file with pattern definition for excluded files
borgExclude = " ${ borgConfig [5] } "
# parameters for borg prune
borgPrune = " ${ borgConfig [6] } "
# export: borg remote path (if not blank)
if [ -n " ${ borgConfig [7] } " ] ; then
export BORG_REMOTE_PATH = " ${ borgConfig [7] } "
else
2018-10-14 14:55:05 -06:00
exitWarn += ( '2112' )
2018-10-14 06:13:16 -06:00
fi
2018-10-05 16:27:04 -06:00
## If borgXtra exists, map contents to an array variable
if [ -n " $borgXtra " ] ; then
echo -e " ${ op } ${ stamp } Processing referenced extra files list for " \
" borgbackup to include in backup ${ normal } " >> " $logFile "
checkExist ff " $borgXtra "
checkResult = " $? "
if [ " $checkResult " = "0" ] ; then
2018-10-14 15:31:18 -06:00
echo -e " ${ op } ${ stamp } Found ${ lit } ${ borgXtra } ${ normal } " >> " $logFile "
2018-10-14 06:35:03 -06:00
mapfile -t xtraFiles < " $borgXtra "
2018-10-14 15:31:18 -06:00
echo -e " ${ op } ${ stamp } Processed extra files list for inclusion in " \
2018-10-05 16:27:04 -06:00
" borgbackup ${ normal } " >> " $logFile "
else
2018-10-14 14:55:05 -06:00
exitWarn += ( '2113' )
2018-10-05 16:27:04 -06:00
fi
fi
2018-10-14 16:54:57 -06:00
## Check if borgExclude exists since borg will throw an error if it's missing
checkExist ff " $borgExclude "
checkResult = " $? "
if [ " $checkResult " = "0" ] ; then
echo -e " ${ op } ${ stamp } Found ${ lit } ${ borgExclude } ${ normal } " \
>> " $logFile "
else
# file not found, unset the variable so it's like it was not specified
# in the first place and continue with backup
unset borgExclude
exitWarn += ( '2114' )
fi
2018-10-14 06:50:42 -06:00
## Generate and execute borg
# commandline depends on whether borgExclude is empty or not
if [ -z " $borgExclude " ] ; then
# borgExclude is empty
2018-10-14 15:40:43 -06:00
echo -e " ${ bold } ${ op } ${ stamp } Executing borg without exclusions ${ normal } " \
2018-10-14 06:50:42 -06:00
>> " $logFile "
2018-10-14 14:57:23 -06:00
borg --show-rc create ${ borgCreateParams } ::` date +%Y-%m-%d_%H%M%S` \
2018-10-14 16:31:30 -06:00
${ xtraFiles [@] } \
${ sqlDumpDir } ${ ncDataDir } \
2018-10-14 07:09:01 -06:00
2>> " $logFile "
2018-10-14 06:50:42 -06:00
else
# borgExclude is not empty
2018-10-14 15:40:43 -06:00
echo -e " ${ bold } ${ op } ${ stamp } Executing borg with exclusions ${ normal } " \
2018-10-14 06:50:42 -06:00
>> " $logFile "
2018-10-14 16:31:30 -06:00
borg --show-rc create ${ borgCreateParams } --exclude-from ${ borgExclude } \
2018-10-14 14:57:23 -06:00
::` date +%Y-%m-%d_%H%M%S` \
2018-10-14 16:31:30 -06:00
${ xtraFiles [@] } \
${ sqlDumpDir } ${ ncDataDir } \
2018-10-14 07:09:01 -06:00
2>> " $logFile "
2018-10-14 06:50:42 -06:00
fi
2018-10-05 16:27:04 -06:00
2018-10-14 07:07:21 -06:00
## Check status of borg operation
borgResult = " $? "
if [ " $borgResult " -eq 0 ] ; then
2018-10-14 07:18:34 -06:00
echo -e " ${ ok } ${ stamp } -- [SUCCESS] Borg backup completed successfully -- " \
2018-10-14 07:07:21 -06:00
" ${ normal } " >> " $logFile "
elif [ " $borgResult " -eq 1 ] ; then
2018-10-14 14:55:05 -06:00
exitWarn += ( '2200' )
2018-10-14 07:07:21 -06:00
elif [ " $borgResult " -ge 2 ] ; then
2018-10-14 10:37:44 -06:00
exitError += ( '220' )
2018-10-14 10:44:50 -06:00
cleanup
2018-10-14 10:37:44 -06:00
quit
2018-10-14 07:07:21 -06:00
else
2018-10-14 14:55:05 -06:00
exitWarn += ( '2201' )
2018-10-14 07:07:21 -06:00
fi
2018-10-14 06:56:25 -06:00
## Generate and execute borg prune
# command depends on whether or not parameters have been defined
if [ -n " $borgPrune " ] ; then
# parameters defined
2018-10-14 15:40:43 -06:00
echo -e " ${ bold } ${ op } ${ stamp } Executing borg prune operation ${ normal } " \
2018-10-14 06:56:25 -06:00
>> " $logFile "
2018-10-14 16:32:03 -06:00
borg prune --show-rc -v ${ borgPruneParams } ${ borgPrune } \
2018-10-14 07:18:11 -06:00
2>> " $logFile "
# check return-status
2018-10-14 14:55:05 -06:00
pruneResult = " $? "
if [ " $pruneResult " -eq 0 ] ; then
2018-10-14 07:18:11 -06:00
echo -e " ${ ok } ${ stamp } -- [SUCCESS] Borg prune completed successfully " \
" -- ${ normal } " >> " $logFile "
2018-10-14 14:55:05 -06:00
elif [ " $pruneResult " -eq 1 ] ; then
exitWarn += ( '2210' )
elif [ " $pruneResult " -ge 2 ] ; then
2018-10-14 15:40:43 -06:00
exitError += ( '221' )
2018-10-14 07:18:11 -06:00
else
2018-10-14 14:55:05 -06:00
exitWarn += ( '2212' )
2018-10-14 07:18:11 -06:00
fi
2018-10-14 06:56:25 -06:00
else
# parameters not defined... skip pruning
2018-10-14 14:55:05 -06:00
exitWarn += ( '2115' )
2018-10-14 06:56:25 -06:00
fi
2018-10-03 16:28:35 -06:00
2018-10-14 07:21:05 -06:00
### borgbackup completed
echo -e " ${ op } ${ stamp } Borgbackup completed... begin cleanup " \
" ${ normal } " >> " $logFile "
2018-09-19 23:23:44 -06:00
### Exit script
2018-10-14 23:05:52 -06:00
echo -e " ${ bold } ${ op } ${ stamp } ***Normal exit process*** ${ normal } " \
2018-10-03 15:00:18 -06:00
>> " $logFile "
2018-09-20 00:46:10 -06:00
cleanup
2018-10-14 16:58:47 -06:00
echo -e " ${ bold } ${ ok } ${ stamp } -- [SUCCESS] All processes completed " \
" successfully -- ${ normal } " >> " $logFile "
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