Compare commits

...

10 Commits

4 changed files with 182 additions and 125 deletions

115
README.md
View File

@ -25,36 +25,40 @@ This script automates the following tasks:
- [Environment notes](#environment-notes) - [Environment notes](#environment-notes)
- [Why this script must be run as root](#why-this-script-must-be-run-as-root) - [Why this script must be run as root](#why-this-script-must-be-run-as-root)
- [Script parameters](#script-parameters) - [Script parameters](#script-parameters)
- [Required parameters](#required-parameters) - [Required parameters](#required-parameters)
- [NextCloud data directory: -d _/path/to/data/_](#nextcloud-data-directory--d-_pathtodata_) - [NextCloud data directory: -d _/path/to/data/_](#nextcloud-data-directory--d-pathtodata)
- [NextCloud webroot: -n _/path/to/nextcloud/_](#nextcloud-webroot--n-_pathtonextcloud_) - [NextCloud webroot: -n _/path/to/nextcloud/_](#nextcloud-webroot--n-pathtonextcloud)
- [webuser account: -w _accountName_](#webuser-account--w-_accountname_) - [webuser account: -w _accountName_](#webuser-account--w-accountname)
- [Optional parameters](#optional-parameters) - [Optional parameters](#optional-parameters)
- [Path to 503 error page: -5 _/path/to/filename.html_](#path-to-503-error-page--5-_pathtofilenamehtml_) - [Path to 503 error page: -5 _/path/to/filename.html_](#path-to-503-error-page--5-pathtofilenamehtml)
- [Path to borg details file: -b _/path/to/filename.file_](#path-to-borg-details-file--b-_pathtofilenamefile_) - [Path to borg details file: -b _/path/to/filename.file_](#path-to-borg-details-file--b-pathtofilenamefile)
- [Desired log file location: -l _/path/to/filename.file_](#desired-log-file-location--l-_pathtofilenamefile_) - [Desired log file location: -l _/path/to/filename.file_](#desired-log-file-location--l-pathtofilenamefile)
- [Path to SQL details file: -s _/path/to/filename.file_](#path-to-sql-details-file--s-_pathtofilenamefile_) - [Path to SQL details file: -s _/path/to/filename.file_](#path-to-sql-details-file--s-pathtofilenamefile)
- [Verbose output from borg: -v (no arguments)](#verbose-output-from-borg--v-no-arguments) - [Verbose output from borg: -v (no arguments)](#verbose-output-from-borg--v-no-arguments)
- [Path to webroot: -w _/path/to/webroot/_](#path-to-webroot--w-_pathtowebroot_) - [Path to webroot: -w _/path/to/webroot/_](#path-to-webroot--w-pathtowebroot)
- [Borg details file](#borg-details-file) - [Borg details file](#borg-details-file)
- [Protect your borg details file](#protect-your-borg-details-file) - [Protect your borg details file](#protect-your-borg-details-file)
- [borg specific entries (lines 1-4)](#borg-specific-entries-lines-1-4) - [borg specific entries (lines 1-4)](#borg-specific-entries-lines-1-4)
- [additional files/directories to backup](#additional-filesdirectories-to-backup) - [Line 1: Path to borg base directory](#line-1-path-to-borg-base-directory)
- [exclusion patterns](#exclusion-patterns) - [Line 2: Path to SSH key for remote server](#line-2-path-to-ssh-key-for-remote-server)
- [prune timeframe options](#prune-timeframe-options) - [Line 3: Connection string to remote repo](#line-3-connection-string-to-remote-repo)
- [borg remote location](#borg-remote-location) - [Line 4: Password for borg repo/repo key](#line-4-password-for-borg-reporepo-key)
- [Examples](#examples) - [additional files/directories to backup](#additional-filesdirectories-to-backup)
- [exclusion patterns](#exclusion-patterns)
- [prune timeframe options](#prune-timeframe-options)
- [borg remote location](#borg-remote-location)
- [Examples](#examples)
- [SQL details file](#sql-details-file) - [SQL details file](#sql-details-file)
- [Protect your sql details file](#protect-your-sql-details-file) - [Protect your sql details file](#protect-your-sql-details-file)
- [503 functionality](#503-functionality) - [503 functionality](#503-functionality)
- [Conditional forwarding by your webserver](#conditional-forwarding-by-your-webserver) - [Conditional forwarding by your webserver](#conditional-forwarding-by-your-webserver)
- [NGINX](#nginx) - [NGINX](#nginx)
- [Apache](#apache) - [Apache](#apache)
- [Disabling 503 functionality altogether](#disabling-503-functionality-altogether) - [Disabling 503 functionality altogether](#disabling-503-functionality-altogether)
- [Scheduling: Cron](#scheduling-cron) - [Scheduling: Cron](#scheduling-cron)
- [The log file](#the-log-file) - [The log file](#the-log-file)
- [Using Logwatch](#using-logwatch) - [Using Logwatch](#using-logwatch)
- [Remember to rotate your logs](#remember-to-rotate-your-logs) - [Remember to rotate your logs](#remember-to-rotate-your-logs)
- [Final notes](#final-notes) - [Final notes](#final-notes)
## Installation/copying ## Installation/copying
@ -201,9 +205,9 @@ example entries. The file must have the following information in the following
order: order:
1. path to borg base directory **(required)** 1. path to borg base directory **(required)**
2. path to ssh private key for repo **(required)** 2. path to ssh private key for remote server **(required)**
3. connection string to remote repo **(required)** 3. connection string to remote repo **(required)**
4. password for ssh key/repo **(required)** 4. password for borg repo/repo key **(required)**
5. path to file listing additional files/directories to backup 5. path to file listing additional files/directories to backup
6. path to file containing borg-specific exclusion patterns 6. path to file containing borg-specific exclusion patterns
7. prune timeframe options 7. prune timeframe options
@ -226,10 +230,59 @@ chmod 600 nc_borg.details # restrict access to root only (read/write)
If you need help with these options, then you should consult the borg If you need help with these options, then you should consult the borg
documentation or search my blog at documentation or search my blog at
[https://mytechiethoughts.com](https://mytechiethoughts.com) for borg. This is [https://mytechiethoughts.com](https://mytechiethoughts.com) for borg. Here's a
especially true if you want to understand why an SSH key and passphrase are very brief overview:
preferred and why just a passphrase on it's own presents problems automating
borg backups. #### Line 1: Path to borg base directory
This is primary directory on your local system where your borg configuration is
located, **NOT* the path to your borg binary. The base directory contains the
borg configuration, cache, security files and keys.
#### Line 2: Path to SSH key for remote server
This is the SSH key used to connect to your remote (backup) server where your
borg repo is located. **This is NOT your borg repo key!**
> Please note: If you are planning on executing this script via cron or some
> other form of automation, it is *highly recommended* that you use an SSH key
> **without** a password! SSH is designed such that passwords cannot simply be
> passed to it via environment variables, etc. so this is something not easily
> automated by a script such as this for security reasons. As such, your
> computer will sit and wait for you to enter the password and will NOT execute
> the actual backup portion of the script until the SSH key password is provided.
>
> If you really want/need to use an SSH key password, you will have to look into
> somethign like GNOME keyring or SSH-agent to provide a secure automated way to
> provide that password to SSH and allow this script to continue.
>
> In practice, SSH keys without passwords are still quite safe since the key
> must still be known in order to connect and most keys are quite long. In
> addition, they key only connects to the remote server, your actual information
> within the borg repository is still encrypted and secured with both a key and
> password.
#### Line 3: Connection string to remote repo
This is the full server and path required to connect to your borg repo on the
remote server. Very often it is the in the form of:
```
user@servername.tld:repo-name/
```
for rsync.net it is in the following form:
```
username@server-number.rsync.net:repo-name/
```
#### Line 4: Password for borg repo/repo key
This is the password needed to access and decrypt your *borg repo*. Assuming
you set up your borg repo using recommended practices, this will actually be the
password for your *borg repo private key*. **This is NOT your SSH key
password!**
### additional files/directories to backup ### additional files/directories to backup

View File

@ -12,7 +12,6 @@ lit="\e[93m"
op="\e[39m" op="\e[39m"
info="\e[96m" info="\e[96m"
note="\e[95m" note="\e[95m"
stamp="[`date +%Y-%m-%d` `date +%H:%M:%S`]"
### Functions ### ### Functions ###
@ -90,6 +89,11 @@ function scriptHelp {
exit 1 exit 1
} }
### generate dynamic timestamps
function stamp {
echo `date +%F" "%T`
}
### quit -- exit the script after logging any errors, warnings, etc. ### quit -- exit the script after logging any errors, warnings, etc.
function quit { function quit {
# list generated warnings, if any # list generated warnings, if any
@ -106,7 +110,7 @@ function quit {
fi fi
if [ -z "${exitError}" ]; then if [ -z "${exitError}" ]; then
# exit cleanly # exit cleanly
echo -e "${note}${stamp} --- ${scriptName} completed" \ echo -e "${note}[$(stamp)] --- ${scriptName} completed" \
"---${normal}" >> "$logFile" "---${normal}" >> "$logFile"
exit 0 exit 0
else else
@ -116,7 +120,7 @@ function quit {
for errCode in "${exitError[@]}"; do for errCode in "${exitError[@]}"; do
errStamp="${errCode%%_*}" errStamp="${errCode%%_*}"
errValue="${errCode##*_}" errValue="${errCode##*_}"
echo -e "${err}${errStamp}-- [ERROR] ${errorExplain[$errValue]}" \ echo -e "${err}${errStamp} -- [ERROR] ${errorExplain[$errValue]}" \
"(code: ${errValue}) --${normal}" >> "$logFile" "(code: ${errValue}) --${normal}" >> "$logFile"
done done
exit 2 exit 2
@ -147,7 +151,7 @@ function checkExist {
### ncMaint - pass requested mode change type to NextCloud occ ### ncMaint - pass requested mode change type to NextCloud occ
function ncMaint { function ncMaint {
sudo -u ${webUser} php ${ncRoot}/occ maintenance:mode --$1 \ sudo -u "${webUser}" php "${ncRoot}/occ" maintenance:mode --"$1" \
>> "$logFile" 2>&1 >> "$logFile" 2>&1
maintResult="$?" maintResult="$?"
return "$maintResult" return "$maintResult"
@ -162,10 +166,10 @@ function cleanup {
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "0" ]; then if [ "$checkResult" = "0" ]; then
# directory still exists # directory still exists
exitWarn+=("${stamp}_111") exitWarn+=("[$(stamp)]_111")
else else
# directory removed # directory removed
echo -e "${op}${stamp} Removed SQL temp directory${normal}" \ echo -e "${op}[$(stamp)] Removed SQL temp directory${normal}" \
>> "$logFile" >> "$logFile"
fi fi
@ -173,21 +177,21 @@ function cleanup {
# check value of 'clean503' to see if this is necessary (=1) otherwise, skip # check value of 'clean503' to see if this is necessary (=1) otherwise, skip
if [ "$clean503" = "1" ]; then if [ "$clean503" = "1" ]; then
# proceed with cleanup # proceed with cleanup
echo -e "${op}${stamp} Removing 503 error page..." >> "$logFile" echo -e "${op}[$(stamp)] Removing 503 error page..." >> "$logFile"
rm -f "$webroot/$err503File" >> "$logFile" 2>&1 rm -f "$webroot/$err503File" >> "$logFile" 2>&1
# verify file is actually gone # verify file is actually gone
checkExist ff "$webroot/$err503File" checkExist ff "$webroot/$err503File"
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "0" ]; then if [ "$checkResult" = "0" ]; then
# file still exists # file still exists
exitWarn+=("${stamp}_5030") exitWarn+=("[$(stamp)]_5030")
else else
# file removed # file removed
echo -e "${info}${stamp} -- [INFO] 503 page removed from webroot" \ echo -e "${info}[$(stamp)] -- [INFO] 503 page removed from webroot" \
"--${normal}" >> "$logFile" "--${normal}" >> "$logFile"
fi fi
else else
echo -e "${op}${stamp} 503 error page never copied to webroot," \ echo -e "${op}[$(stamp)] 503 error page never copied to webroot," \
"nothing to cleanup" >> "$logFile" "nothing to cleanup" >> "$logFile"
fi fi
@ -195,10 +199,10 @@ function cleanup {
ncMaint off ncMaint off
# check if successful # check if successful
if [ "$maintResult" = "0" ]; then if [ "$maintResult" = "0" ]; then
echo -e "${info}${stamp} -- [INFO] NextCloud now in regular" \ echo -e "${info}[$(stamp)] -- [INFO] NextCloud now in regular" \
"operating mode --${normal}" >> "$logFile" "operating mode --${normal}" >> "$logFile"
else else
exitError+=("${stamp}_101") exitError+=("[$(stamp)]_101")
quit quit
fi fi
} }
@ -273,7 +277,7 @@ 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" warningExplain[5033]="No 503 error page could be found. If not using the default located in the script directory, then check your -5 parameter"
warningExplain[5035]="Error copying 503 error page to webroot" warningExplain[5035]="Error copying 503 error page to webroot"
warn503="Web users will NOT be informed the server is down!" warn503="Web users will NOT be informed the server is down!"
warningExplain[2111]="No password used for SSH keys or access to remote borg repo. This is an insecure configuration" warningExplain[2111]="No password used for access to remote borg repo. This is an insecure configuration"
warningExplain[2112]="No remote borg instance specified. Operations will be slower in this configuration" 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" warningExplain[2113]="The specified file containing extra files for inclusion in borgbackup could not be found"
warningExplain[2114]="The specified file containing exclusion patterns for borgbackup could not be found. Backup was performed as though NO exclusions were defined" warningExplain[2114]="The specified file containing exclusion patterns for borgbackup could not be found. Backup was performed as though NO exclusions were defined"
@ -377,8 +381,8 @@ if [ -z "$webUser" ]; then
exit 1 exit 1
# Check if supplied webUser account exists # Check if supplied webUser account exists
elif [ -n "$webUser" ]; then elif [ -n "$webUser" ]; then
user_exists=$(id -u $webUser > /dev/null 2>&1; echo $?) user_exists=$(id -u "$webUser" > /dev/null 2>&1; echo $?)
if [ $user_exists -ne 0 ]; then if [ "$user_exists" -ne 0 ]; then
echo -e "\n${err}The supplied webuser account (-u parameter) does not" \ echo -e "\n${err}The supplied webuser account (-u parameter) does not" \
"exist.${normal}\n" "exist.${normal}\n"
exit 1 exit 1
@ -425,9 +429,9 @@ fi
### Log start of script operations ### Log start of script operations
echo -e "${note}${stamp}--- Start $scriptName execution ---${normal}" \ echo -e "${note}[$(stamp)] --- Start $scriptName execution ---${normal}" \
>> "$logFile" >> "$logFile"
echo -e "${info}${stamp}-- [INFO] Log file located at ${lit}${logFile}${info}" \ echo -e "${info}[$(stamp)] -- [INFO] Log file located at ${lit}${logFile}${info}" \
"--${normal}" >> "$logFile" "--${normal}" >> "$logFile"
@ -438,7 +442,7 @@ export logFile="$logFile"
### Create sqlDump temporary directory and sqlDumpFile name ### Create sqlDump temporary directory and sqlDumpFile name
sqlDumpDir=$( mktemp -d ) sqlDumpDir=$( mktemp -d )
sqlDumpFile="backup-`date +%Y%m%d_%H%M%S`.sql" sqlDumpFile="backup-`date +%Y%m%d_%H%M%S`.sql"
echo -e "${info}${stamp} -- [INFO] mySQL dump file will be stored" \ echo -e "${info}[$(stamp)] -- [INFO] mySQL dump file will be stored" \
"at: ${lit}${sqlDumpDir}/${sqlDumpFile}${normal}" >> "$logFile" "at: ${lit}${sqlDumpDir}/${sqlDumpFile}${normal}" >> "$logFile"
@ -451,9 +455,9 @@ echo -e "${info}${stamp} -- [INFO] mySQL dump file will be stored" \
## Check if webroot has been specified, if not, skip this entire section since there is nowhere to copy the 503 file. ## 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 if [ -z "$webroot" ]; then
# no webroot path provided # no webroot path provided
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \ echo -e "${info}[$(stamp)] -- [INFO] ${warn503} --${normal}" \
>> "$logFile" >> "$logFile"
exitWarn+=("${stamp}_5031") exitWarn+=("[$(stamp)]_5031")
clean503=0 clean503=0
else else
# verify webroot actually exists # verify webroot actually exists
@ -461,41 +465,41 @@ else
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "1" ]; then if [ "$checkResult" = "1" ]; then
# webroot directory specified could not be found # webroot directory specified could not be found
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \ echo -e "${info}[$(stamp)] -- [INFO] ${warn503} --${normal}" \
>> "$logFile" >> "$logFile"
exitWarn+=("{$stamp}_5032") exitWarn+=("{$stamp}_5032")
clean503=0 clean503=0
else else
# webroot exists # webroot exists
echo -e "${op}${stamp} Using webroot: ${lit}${webroot}${normal}" \ echo -e "${op}[$(stamp)] Using webroot: ${lit}${webroot}${normal}" \
>> "$logFile" >> "$logFile"
# Verify 503 file existance at given path # Verify 503 file existance at given path
checkExist ff "$err503Path" checkExist ff "$err503Path"
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "1" ]; then if [ "$checkResult" = "1" ]; then
# 503 file could not be found # 503 file could not be found
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \ echo -e "${info}[$(stamp)] -- [INFO] ${warn503} --${normal}" \
>> "$logFile" >> "$logFile"
exitWarn+=("${stamp}_5033") exitWarn+=("[$(stamp)]_5033")
clean503=0 clean503=0
else else
# 503 file exists and webroot is valid. Let's copy it! # 503 file exists and webroot is valid. Let's copy it!
echo -e "${op}${stamp} ${err503File} found at ${lit}${err503Path}" \ echo -e "${op}[$(stamp)] ${err503File} found at ${lit}${err503Path}" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
echo -e "${op}${stamp} Copying 503 error page to webroot..." \ echo -e "${op}[$(stamp)] Copying 503 error page to webroot..." \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
cp "${err503Path}" "$webroot/" >> "$logFile" 2>&1 cp "${err503Path}" "$webroot/" >> "$logFile" 2>&1
copyResult="$?" copyResult="$?"
# verify copy was successful # verify copy was successful
if [ "$copyResult" = "1" ]; then if [ "$copyResult" = "1" ]; then
# copy was unsuccessful # copy was unsuccessful
echo -e "${info}${stamp} -- [INFO] ${warn503} --${normal}" \ echo -e "${info}[$(stamp)] -- [INFO] ${warn503} --${normal}" \
>> "$logFile" >> "$logFile"
exitWarn+=("${stamp}_5035") exitWarn+=("[$(stamp)]_5035")
clean503=0 clean503=0
else else
# copy was successful # copy was successful
echo -e "${info}${stamp} -- [INFO] 503 error page" \ echo -e "${info}[$(stamp)] -- [INFO] 503 error page" \
"successfully copied to webroot --${normal}" >> "$logFile" "successfully copied to webroot --${normal}" >> "$logFile"
clean503=1 clean503=1
fi fi
@ -510,10 +514,10 @@ fi
ncMaint on ncMaint on
# check if successful # check if successful
if [ "$maintResult" = "0" ]; then if [ "$maintResult" = "0" ]; then
echo -e "${info}${stamp} -- [INFO] NextCloud now in maintenance mode --" \ echo -e "${info}[$(stamp)] -- [INFO] NextCloud now in maintenance mode --" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
else else
exitError+=("${stamp}_100") exitError+=("[$(stamp)]_100")
cleanup cleanup
quit quit
fi fi
@ -524,23 +528,23 @@ mapfile -t sqlParams < "$sqlDetails"
### Dump SQL ### Dump SQL
echo -e "${op}${stamp} Dumping NextCloud SQL database...${normal}" >> "$logFile" echo -e "${op}[$(stamp)] Dumping NextCloud SQL database...${normal}" >> "$logFile"
mysqldump --single-transaction -h"${sqlParams[0]}" -u"${sqlParams[1]}" \ mysqldump --single-transaction -h"${sqlParams[0]}" -u"${sqlParams[1]}" \
-p"${sqlParams[2]}" "${sqlParams[3]}" > "${sqlDumpDir}/${sqlDumpFile}" \ -p"${sqlParams[2]}" "${sqlParams[3]}" > "${sqlDumpDir}/${sqlDumpFile}" \
2>> "$logFile" 2>> "$logFile"
# verify # verify
dumpResult="$?" dumpResult="$?"
if [ "$dumpResult" = "0" ]; then if [ "$dumpResult" = "0" ]; then
echo -e "${ok}${stamp} -- [SUCCESS] SQL dumped successfully --${normal}" \ echo -e "${ok}[$(stamp)] -- [SUCCESS] SQL dumped successfully --${normal}" \
>> "$logFile" >> "$logFile"
else else
exitError+=("${stamp}_200") exitError+=("[$(stamp)]_200")
cleanup cleanup
quit quit
fi fi
### Call borgbackup to copy actual files ### Call borgbackup to copy actual files
echo -e "${op}${stamp} Pre-backup tasks completed, calling borgbackup..." \ echo -e "${op}[$(stamp)] Pre-backup tasks completed, calling borgbackup..." \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
## Get borgbackup settings and repo details ## Get borgbackup settings and repo details
@ -549,10 +553,10 @@ mapfile -t borgConfig < "$borgDetails"
## check if any required borg configuration variables in defintion file are ## check if any required borg configuration variables in defintion file are
## empty and exit with error, otherwise, map array items to variables ## empty and exit with error, otherwise, map array items to variables
# check: borg base directory # check: borg base directory
echo -e "${op}${stamp} Verifying supplied borg configuration variables..." \ echo -e "${op}[$(stamp)] Verifying supplied borg configuration variables..." \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
if [ -z "${borgConfig[0]}" ]; then if [ -z "${borgConfig[0]}" ]; then
exitError+=("${stamp}_210") exitError+=("[$(stamp)]_210")
cleanup cleanup
quit quit
else else
@ -561,16 +565,16 @@ else
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "1" ]; then if [ "$checkResult" = "1" ]; then
# borg base directory specified could not be found # borg base directory specified could not be found
exitError+=("${stamp}_210") exitError+=("[$(stamp)]_210")
cleanup cleanup
quit quit
fi fi
echo -e "${op}${stamp} Borg base dir... OK${normal}" >> "$logFile" echo -e "${op}[$(stamp)] Borg base dir... OK${normal}" >> "$logFile"
export BORG_BASE_DIR="${borgConfig[0]%/}" export BORG_BASE_DIR="${borgConfig[0]%/}"
fi fi
# check: path to SSH keyfile # check: path to SSH keyfile
if [ -z "${borgConfig[1]}" ]; then if [ -z "${borgConfig[1]}" ]; then
exitError+=("${stamp}_211") exitError+=("[$(stamp)]_211")
cleanup cleanup
quit quit
else else
@ -578,28 +582,28 @@ else
checkResult="$?" checkResult="$?"
if [ "$checkResult" = 1 ]; then if [ "$checkResult" = 1 ]; then
# SSH keyfile specified could not be found # SSH keyfile specified could not be found
exitError+=("${stamp}_211") exitError+=("[$(stamp)]_211")
cleanup cleanup
quit quit
fi fi
echo -e "${op}${stamp} Borg SSH key... OK${normal}" >> "$logFile" echo -e "${op}[$(stamp)] Borg SSH key... OK${normal}" >> "$logFile"
export BORG_RSH="ssh -i ${borgConfig[1]}" export BORG_RSH="ssh -i ${borgConfig[1]}"
fi fi
# check: name of borg repo # check: name of borg repo
if [ -z "${borgConfig[2]}" ]; then if [ -z "${borgConfig[2]}" ]; then
exitError+=("${stamp}_212") exitError+=("[$(stamp)]_212")
cleanup cleanup
quit quit
else else
echo -e "${op}${stamp} Borg REPO name... OK${normal}" >> "$logFile" echo -e "${op}[$(stamp)] Borg REPO name... OK${normal}" >> "$logFile"
export BORG_REPO="${borgConfig[2]}" export BORG_REPO="${borgConfig[2]}"
fi fi
# repo password # repo password
if [ -n "${borgConfig[3]}" ]; then if [ -n "${borgConfig[3]}" ]; then
echo -e "${op}${stamp} Borg SSH/REPO password... OK${normal}" >> "$logFile" echo -e "${op}[$(stamp)] Borg REPO password... OK${normal}" >> "$logFile"
export BORG_PASSPHRASE="${borgConfig[3]}" export BORG_PASSPHRASE="${borgConfig[3]}"
else else
exitWarn+=("${stamp}_2111") exitWarn+=("[$(stamp)]_2111")
# if the password was omitted by mistake, export a dummy password so borg # if the password was omitted by mistake, export a dummy password so borg
# fails with an error instead of sitting and waiting for input # fails with an error instead of sitting and waiting for input
export BORG_PASSPHRASE="DummyPasswordSoBorgFails" export BORG_PASSPHRASE="DummyPasswordSoBorgFails"
@ -612,31 +616,31 @@ borgExclude="${borgConfig[5]}"
borgPrune="${borgConfig[6]}" borgPrune="${borgConfig[6]}"
# export: borg remote path (if not blank) # export: borg remote path (if not blank)
if [ -n "${borgConfig[7]}" ]; then if [ -n "${borgConfig[7]}" ]; then
echo -e "${op}${stamp} Borg REMOTE path... OK${normal}" >> "$logFile" echo -e "${op}[$(stamp)] Borg REMOTE path... OK${normal}" >> "$logFile"
export BORG_REMOTE_PATH="${borgConfig[7]}" export BORG_REMOTE_PATH="${borgConfig[7]}"
else else
exitWarn+=("${stamp}_2112") exitWarn+=("[$(stamp)]_2112")
fi fi
## If borgXtra exists, map contents to an array variable ## If borgXtra exists, map contents to an array variable
if [ -n "$borgXtra" ]; then if [ -n "$borgXtra" ]; then
echo -e "${op}${stamp} Processing referenced extra files list for" \ echo -e "${op}[$(stamp)] Processing referenced extra files list for" \
"borgbackup to include in backup${normal}" >> "$logFile" "borgbackup to include in backup${normal}" >> "$logFile"
checkExist ff "$borgXtra" checkExist ff "$borgXtra"
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "0" ]; then if [ "$checkResult" = "0" ]; then
echo -e "${op}${stamp} Found ${lit}${borgXtra}${normal}" >> "$logFile" echo -e "${op}[$(stamp)] Found ${lit}${borgXtra}${normal}" >> "$logFile"
mapfile -t xtraFiles < "$borgXtra" mapfile -t xtraFiles < "$borgXtra"
echo -e "${op}${stamp} Processed extra files list for inclusion in" \ echo -e "${op}[$(stamp)] Processed extra files list for inclusion in" \
"borgbackup${normal}" >> "$logFile" "borgbackup${normal}" >> "$logFile"
else else
exitWarn+=("${stamp}_2113") exitWarn+=("[$(stamp)]_2113")
fi fi
else else
# no extra locations specified # no extra locations specified
echo -e "${op}${stamp} No additional locations specified for backup." \ echo -e "${op}[$(stamp)] No additional locations specified for backup." \
"Only NextCloud data files will be backed up${normal}" >> "$logFile" "Only NextCloud data files will be backed up${normal}" >> "$logFile"
exitWarn+=("${stamp}_2116") exitWarn+=("[$(stamp)]_2116")
fi fi
## Check if borgExclude exists since borg will throw an error if it's missing ## Check if borgExclude exists since borg will throw an error if it's missing
@ -644,16 +648,16 @@ if [ -n "$borgExclude" ]; then
checkExist ff "$borgExclude" checkExist ff "$borgExclude"
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "0" ]; then if [ "$checkResult" = "0" ]; then
echo -e "${op}${stamp} Found ${lit}${borgExclude}${normal}" \ echo -e "${op}[$(stamp)] Found ${lit}${borgExclude}${normal}" \
>> "$logFile" >> "$logFile"
else else
# file not found, unset the variable so it's like it was not specified # file not found, unset the variable so it's like it was not specified
# in the first place and continue with backup # in the first place and continue with backup
unset borgExclude unset borgExclude
exitWarn+=("${stamp}_2114") exitWarn+=("[$(stamp)]_2114")
fi fi
else else
echo -e "${op}${stamp} Exclusion pattern file not specified." \ echo -e "${op}[$(stamp)] Exclusion pattern file not specified." \
"No exclusions will be processed${normal}" >> "$logFile" "No exclusions will be processed${normal}" >> "$logFile"
fi fi
@ -664,13 +668,13 @@ fi
## the 'noexec' option for security. Thus, we will use/create a 'tmp' folder ## the 'noexec' option for security. Thus, we will use/create a 'tmp' folder
## within the BORG_BASE_DIR and instruct python to use that instead of /tmp ## within the BORG_BASE_DIR and instruct python to use that instead of /tmp
# check if BORG_BASE_DIR/tmp exists, if not, create it # check if BORG_BASE_DIR/tmp exists, if not, create it
echo -e "${op}${stamp} Checking for tmp directory at ${lit}${BORG_BASE_DIR}" \ echo -e "${op}[$(stamp)] Checking for tmp directory at ${lit}${BORG_BASE_DIR}" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
checkExist fd "$BORG_BASE_DIR/tmp" checkExist fd "$BORG_BASE_DIR/tmp"
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "1" ]; then if [ "$checkResult" = "1" ]; then
# folder not found # folder not found
echo -e "${op}${stamp} tmp folder not found... creating${lit}" \ echo -e "${op}[$(stamp)] tmp folder not found... creating${lit}" \
"${BORG_BASE_DIR}/tmp${normal}" >> "$logFile" "${BORG_BASE_DIR}/tmp${normal}" >> "$logFile"
mkdir "$BORG_BASE_DIR/tmp" 2>> "$logFile" mkdir "$BORG_BASE_DIR/tmp" 2>> "$logFile"
# verify folder created # verify folder created
@ -678,17 +682,17 @@ if [ "$checkResult" = "1" ]; then
checkResult="$?" checkResult="$?"
if [ "$checkResult" = "0" ]; then if [ "$checkResult" = "0" ]; then
# folder exists # folder exists
echo -e "${op}${stamp} tmp folder created within borg base directory" \ echo -e "${op}[$(stamp)] tmp folder created within borg base directory" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
else else
# problem creating folder and script will exit # problem creating folder and script will exit
exitError+=("${stamp}_215") exitError+=("[$(stamp)]_215")
cleanup cleanup
quit quit
fi fi
else else
# folder found # folder found
echo -e "${op}${stamp} tmp folder found within borg base directory" \ echo -e "${op}[$(stamp)] tmp folder found within borg base directory" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
fi fi
# export TMPDIR environment variable # export TMPDIR environment variable
@ -699,74 +703,74 @@ export TMPDIR="${BORG_BASE_DIR}/tmp"
# commandline depends on whether borgExclude is empty or not # commandline depends on whether borgExclude is empty or not
if [ -z "$borgExclude" ]; then if [ -z "$borgExclude" ]; then
# borgExclude is empty # borgExclude is empty
echo -e "${info}${stamp} --[INFO] Executing borg without exclusions --" \ echo -e "${info}[$(stamp)] --[INFO] Executing borg without exclusions --" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
borg --show-rc create ${borgCreateParams} ::`date +%Y-%m-%d_%H%M%S` \ borg --show-rc create ${borgCreateParams} ::`date +%Y-%m-%d_%H%M%S` \
${xtraFiles[@]} \ "${xtraFiles[@]}" \
${sqlDumpDir} ${ncDataDir} \ "${sqlDumpDir}" "${ncDataDir}" \
2>> "$logFile" 2>> "$logFile"
else else
# borgExclude is not empty # borgExclude is not empty
echo -e "${info}${stamp} --[INFO] Executing borg with exclusions --" \ echo -e "${info}[$(stamp)] --[INFO] Executing borg with exclusions --" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
borg --show-rc create ${borgCreateParams} --exclude-from ${borgExclude} \ borg --show-rc create ${borgCreateParams} --exclude-from "${borgExclude}" \
::`date +%Y-%m-%d_%H%M%S` \ ::`date +%Y-%m-%d_%H%M%S` \
${xtraFiles[@]} \ "${xtraFiles[@]}" \
${sqlDumpDir} ${ncDataDir} \ "${sqlDumpDir}" "${ncDataDir}" \
2>> "$logFile" 2>> "$logFile"
fi fi
## Check status of borg operation ## Check status of borg operation
borgResult="$?" borgResult="$?"
if [ "$borgResult" -eq 0 ]; then if [ "$borgResult" -eq 0 ]; then
echo -e "${ok}${stamp} -- [SUCCESS] Borg backup completed successfully --" \ echo -e "${ok}[$(stamp)] -- [SUCCESS] Borg backup completed successfully --" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
elif [ "$borgResult" -eq 1 ]; then elif [ "$borgResult" -eq 1 ]; then
exitWarn+=("${stamp}_2200") exitWarn+=("[$(stamp)]_2200")
elif [ "$borgResult" -ge 2 ]; then elif [ "$borgResult" -ge 2 ]; then
exitError+=("${stamp}_220") exitError+=("[$(stamp)]_220")
cleanup cleanup
quit quit
else else
exitWarn+=("${stamp}_2201") exitWarn+=("[$(stamp)]_2201")
fi fi
## Generate and execute borg prune ## Generate and execute borg prune
# command depends on whether or not parameters have been defined # command depends on whether or not parameters have been defined
if [ -n "$borgPrune" ]; then if [ -n "$borgPrune" ]; then
# parameters defined # parameters defined
echo -e "${info}${stamp} --[INFO] Executing borg prune operation --" \ echo -e "${info}[$(stamp)] --[INFO] Executing borg prune operation --" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
borg prune --show-rc -v ${borgPruneParams} ${borgPrune} \ borg prune --show-rc -v ${borgPruneParams} ${borgPrune} \
2>> "$logFile" 2>> "$logFile"
# check return-status # check return-status
pruneResult="$?" pruneResult="$?"
if [ "$pruneResult" -eq 0 ]; then if [ "$pruneResult" -eq 0 ]; then
echo -e "${ok}${stamp} -- [SUCCESS] Borg prune completed successfully" \ echo -e "${ok}[$(stamp)] -- [SUCCESS] Borg prune completed successfully" \
"--${normal}" >> "$logFile" "--${normal}" >> "$logFile"
elif [ "$pruneResult" -eq 1 ]; then elif [ "$pruneResult" -eq 1 ]; then
exitWarn+=("${stamp}_2210") exitWarn+=("[$(stamp)]_2210")
elif [ "$pruneResult" -ge 2 ]; then elif [ "$pruneResult" -ge 2 ]; then
exitError+=("${stamp}_221") exitError+=("[$(stamp)]_221")
else else
exitWarn+=("${stamp}_2212") exitWarn+=("[$(stamp)]_2212")
fi fi
else else
# parameters not defined... skip pruning # parameters not defined... skip pruning
exitWarn+=("${stamp}_2115") exitWarn+=("[$(stamp)]_2115")
fi fi
### borgbackup completed ### borgbackup completed
echo -e "${op}${stamp} Borgbackup completed... begin cleanup" \ echo -e "${op}[$(stamp)] Borgbackup completed... begin cleanup" \
"${normal}" >> "$logFile" "${normal}" >> "$logFile"
### Exit script ### Exit script
echo -e "${bold}${op}${stamp} ***Normal exit process***${normal}" \ echo -e "${bold}${op}[$(stamp)] ***Normal exit process***${normal}" \
>> "$logFile" >> "$logFile"
cleanup cleanup
echo -e "${bold}${ok}${stamp} -- [SUCCESS] All processes completed" \ echo -e "${bold}${ok}[$(stamp)] -- [SUCCESS] All processes completed" \
"successfully --${normal}" >> "$logFile" "successfully --${normal}" >> "$logFile"
quit quit

View File

@ -1,8 +1,8 @@
<path to borgbackup base directory> /var/borgbackup <path to borgbackup base directory> /var/borgbackup
<path to SSH private key for repo> /var/borgbackup/sshPrivate.key <path to SSH private key for remote server> /var/borgbackup/sshPrivate.key
<connection string to remote repo> user@server-number.rsync.net:repoName/ <connection string to remote repo> user@servername.tld:repoName/
<password for SSH key/repo> pAsSwOrd <password for repo> pAsSwOrd
<path to file listing extra files> /root/NCscripts/xtraLocations.borg <path to file listing extra files> /root/NCscripts/xtraLocations.borg
<path to file with exclusions> /root/NCscripts/excludeLocations.borg <path to file with exclusions> /root/NCscripts/excludeLocations.borg
<purge timeframe options> --keep-within=7d --keep-daily=30 --keep-weekly=12 --keep-monthly=-1 <purge timeframe options> --keep-within=7d --keep-daily=30 --keep-weekly=12 --keep-monthly=-1
<location of borg remote instance> borg1 <location of borg remote instance> borg1

View File

@ -25,9 +25,9 @@
/root/.ssh/ /root/.ssh/
/etc/mysql/my.cnf /etc/mysql/my.cnf
/etc/nginx/ /etc/nginx/
/etc/php/7.0/cli/php.ini /etc/php/7.2/cli/php.ini
/etc/php/7.0/fpm/php-fpm.conf /etc/php/7.2/fpm/php-fpm.conf
/etc/php/7.0/fpm/php.ini /etc/php/7.2/fpm/php.ini
/etc/php/7.0/fpm/pool.d/www.conf /etc/php/7.2/fpm/pool.d/www.conf
/etc/redis/redis.conf /etc/redis/redis.conf
/usr/share/nginx/html/ /usr/share/nginx/html/