seafileBackup/seafbackup.sh

776 lines
25 KiB
Bash
Raw Normal View History

2019-05-21 17:27:10 -06:00
#!/bin/sh
#######
### Backup Seafile/Seafile Pro server on bare-metal (NOT DOCKER)
#######
### text formatting presents
norm=$(tput sgr0)
err=$(tput bold)$(tput setaf 1)
warn=$(tput bold)$(tput setaf 3)
ok=$(tput setaf 2)
yellow=$(tput setaf 3)
cyan=$(tput setaf 6)
2019-05-27 06:10:06 -06:00
magenta=$(tput setaf 5)
2019-05-21 17:27:10 -06:00
2019-06-18 22:14:33 -06:00
### trap
2019-06-18 22:30:22 -06:00
trap trapExit 1 2 3 6
2019-06-18 22:14:33 -06:00
2019-05-21 17:27:10 -06:00
### functions
# bad configuration value passed in details file
badDetails () {
if [ "$1" = "empty" ]; then
exitError 130 "details:${2} cannot be NULL (undefined)"
elif [ "$1" = "dne" ]; then
exitError 131 "details:${2} file or directory does not exist."
fi
}
# bad parameter passed to script
badParam () {
if [ "$1" = "dne" ]; then
2019-05-23 21:42:35 -06:00
printf "${err}\nError: '%s %s'\n" "$2" "$3"
printf "file or directory does not exist.${norm}\n\n"
exit 1
elif [ "$1" = "empty" ]; then
printf "${err}\nError: '%s' cannot have a NULL (empty) value.\n" "$2"
printf "${cyan}Please use '--help' for assistance${norm}\n\n"
exit 1
elif [ "$1" = "svc" ]; then
printf "${err}\nError: '%s %s': Service does not exist!${norm}\n\n" \
"$2" "$3"
exit 1
fi
}
# cleanup
cleanup () {
# cleanup 503 if copied
2019-05-25 12:37:20 -06:00
if [ "$err503Copied" -eq 1 ]; then
if ! rm -f "$webroot/$err503File" 2>>"$logFile"; then
printf "${warn}[%s] -- [WARNING] Could not remove 503 error page." \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
printf " Web interface will not function until this file is " \
>> "$logFile"
printf "removed --${norm}\n" >> "$logFile"
warnCount=$((warnCount+1))
2019-05-25 13:18:31 -06:00
else
2019-05-25 19:53:00 -06:00
printf "${cyan}[%s] -- [INFO] 503 error page removed --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-25 13:18:31 -06:00
fi
fi
2019-05-25 12:37:20 -06:00
if [ "$sqlCopied" -eq 1 ]; then
if ! rm -rf "$sqlDumpDir" 2>>"$logFile"; then
printf "${warn}[%s] -- [WARNING] Could not remove temporary " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
printf "SQL dump directory at %s. " "$sqlDumpDir" >> "$logFile"
printf "Remove manually to free up space.${norm}\n" >> "$logFile"
warnCount=$((warnCount+1))
2019-05-25 13:18:31 -06:00
else
2019-05-25 13:29:09 -06:00
printf "${cyan}[%s] -- [INFO] Temporary SQL dump directory " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-25 13:18:31 -06:00
printf "removed --${norm}\n" >> "$logFile"
fi
fi
2019-05-25 13:11:31 -06:00
if [ "$offlineBackup" -eq 1 ]; then
2019-05-25 13:20:30 -06:00
printf "${cyan}[%s] -- [INFO] Starting seafile services --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-25 13:11:31 -06:00
seafSvc start
fi
}
2019-06-18 23:11:47 -06:00
# call cleanup and then exit with error report
exitError () {
2019-05-23 23:18:35 -06:00
printf "${err}[%s] -- [ERROR] %s: %s --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$1" "$2" >> "$logFile"
cleanup
# note script completion with error
printf "${err}[%s] --- %s execution completed with error ---${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$scriptName" >> "$logFile"
exit "$1"
}
2019-06-19 02:13:21 -06:00
# display script help information
scriptHelp () {
2019-06-19 23:08:07 -06:00
printf "\n${cyan}%s\n" "--------------------------------------------------------------------------------"
printf "Usage:${norm} %s [parameters]\n\n" "$scriptName"
printf "${cyan}Parameters ${yellow}(default value):${norm}\n"
printf "There are NO mandatory parameters. If a parameter is not "
printf "supplied, it's\n"
printf "default value will be used. In the case of a switch parameter, "
printf "it will\n"
printf "remain deactivated if not specified.\n\n"
printf "${magenta}script related\n"
printf "${cyan}-c, --config, --details${norm}\n"
printf "Path to the configuration key/value pair file for this script.\n"
printf "${yellow}(scriptPath/seafbackup.details)${norm}\n\n"
printf "${cyan}-h, -?, --help${norm}\n"
printf "This help screen.\n\n"
printf "${cyan}-l, --log${norm}\n"
printf "Path to write log file.\n"
printf "${yellow}(scriptPath/scriptName.log)${norm}\n\n"
printf "${cyan}-v, --verbose${norm}\n"
printf "Log borg output with increased verbosity (list all files). "
printf "Careful! Your\n"
printf "log file can get large very quickly! This is a switch value, "
printf "specifying it\n"
printf "will turn the option ON.\n"
printf "${yellow}(normal output, option is OFF)${norm}\n\n"
printf "${magenta}503 functionality\n"
printf "${cyan}-5, --use-503${norm}\n"
printf "Copy an 'error 503' page to your webroot for your webserver to "
printf "find. This is a\n"
printf "switch value, specifying it will instruct the script to copy the "
printf "error page.\n"
printf "${yellow}(do NOT copy, option is OFF)${norm}\n\n"
printf "${cyan}--503-path${norm}\n"
printf "Path to the file you want copied to your webroot as the 'error "
printf "503' page during\n"
printf "backup operations.\n"
printf "${yellow}(scriptPath/503_backup.html)${norm}\n\n"
printf "${cyan}-w, --webroot${norm}\n"
printf "Path to where the 'error 503' file should be copied during "
printf "backups.\n"
printf "${yellow}(/usr/share/nginx/html)${norm}\n\n"
printf "${magenta}seafile related\n"
printf "${cyan}-d, --data, --datadir, --seafdata${norm}\n"
printf "Path to your seafile DATA base/root directory."
printf "${yellow}(/var/seafile)${norm}\n\n"
printf "${cyan}-o, --offline${norm}\n"
printf "Stop your seafile systemd services and start them up again after "
printf "the backup.\n"
printf "This is a switch value.\n"
printf "${yellow}(leave seafile running, OFF)${norm}\n\n"
printf "${cyan}-p, --seaf, --seafdir${norm}\n"
printf "${norm}Path to your seafile PROGRAM base/root directory.\n"
printf "${yellow}(/opt/seafile)${norm}\n\n"
printf "${cyan}--seafile-service${norm}\n"
printf "The name of your seafile systemd service. Use with '--offline' "
printf "when using a\n"
printf "non-default service name.\n"
printf "${yellow}(seafile.service)${norm}\n\n"
printf "${cyan}--seahub-service${norm}\n"
printf "The name of your seahub (web) systemd service. Use with "
printf "'--offline' when using\n"
printf "a non-default service name.\n"
printf "${yellow}(seahub.service)${norm}\n\n"
printf "More details and examples of script usage can be found in the "
printf "repo wiki at\n"
printf "${yellow}https://git.asifbacchus.app/asif/seafileBackup.git"
printf "/wiki${norm}.\n"
printf "${cyan}%s${norm}\n\n" "--------------------------------------------------------------------------------"
2019-06-19 02:13:21 -06:00
}
2019-05-22 22:58:07 -06:00
# control seafile services (systemd)
seafSvc () {
if [ "$1" = "start" ]; then
2019-05-23 21:54:19 -06:00
if ! systemctl start "${seafService}" >> "$logFile" 2>&1; then
2019-05-23 23:19:53 -06:00
exitError 100 "Could not start ${seafService}"
else
printf "${cyan}[%s] -- [INFO] Started service: %s --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$seafService" >> "$logFile"
2019-05-22 22:58:07 -06:00
fi
if ! systemctl start "${seafHub}" >> "$logFile" 2>&1; then
2019-05-23 23:19:53 -06:00
exitError 101 "Could not start ${seafHub}"
else
printf "${cyan}[%s] -- [INFO] Started service: %s --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$seafHub" >> "$logFile"
2019-05-22 22:58:07 -06:00
fi
elif [ "$1" = "stop" ]; then
if ! systemctl stop "${seafHub}" >> "$logFile" 2>&1; then
2019-05-23 23:19:53 -06:00
exitError 103 "Could not stop ${seafHub}"
else
printf "${cyan}[%s] -- [INFO] Stopped service: %s --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$seafService" >> "$logFile"
2019-05-22 22:58:07 -06:00
fi
2019-05-23 21:54:19 -06:00
if ! systemctl stop "${seafService}" >> "$logFile" 2>&1; then
2019-05-23 23:19:53 -06:00
exitError 102 "Could not stop ${seafService}"
else
printf "${cyan}[%s] -- [INFO] Stopped service: %s --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$seafHub" >> "$logFile"
2019-05-22 22:58:07 -06:00
fi
fi
}
2019-05-21 17:27:10 -06:00
# generate dynamic timestamps
stamp () {
(date +%F" "%T)
}
2019-06-18 23:11:47 -06:00
# same as exitError but for signal captures
2019-06-18 22:30:22 -06:00
trapExit () {
printf "${err}[%s] -- [ERROR] 99: Caught signal --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-06-18 22:30:22 -06:00
cleanup
# note script completion with error
printf "${err}[%s] --- %s execution was terminated via signal ---${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$scriptName" >> "$logFile"
2019-06-18 22:32:33 -06:00
exit 99
2019-06-18 22:30:22 -06:00
}
2019-05-21 17:27:10 -06:00
### end of functions
### default variable values
2019-05-23 20:57:05 -06:00
## script related
2019-05-21 17:27:10 -06:00
# store logfile in the same directory as this script file using the same file
# name as the script but with the extension '.log'
scriptPath="$( CDPATH='' cd -- "$( dirname -- "$0" )" && pwd -P )"
2019-05-21 17:27:10 -06:00
scriptName="$( basename "$0" )"
logFile="$scriptPath/${scriptName%.*}.log"
warnCount=0
2019-05-23 21:06:49 -06:00
configDetails="$scriptPath/seafbackup.details"
err503Copied=0
sqlCopied=0
2019-05-27 03:13:48 -06:00
includeXtra=0
exclusions=0
# borg output verbosity -- normal
borgCreateParams='--stats'
borgPruneParams='--list'
2019-05-21 17:27:10 -06:00
2019-05-23 20:57:05 -06:00
# 503 related
2019-05-24 20:41:00 -06:00
use503=0
2019-05-23 20:57:05 -06:00
err503Path="$scriptPath/503_backup.html"
err503File="${err503Path##*/}"
webroot="/usr/share/nginx/html"
# seafile related
2019-05-22 22:58:07 -06:00
offlineBackup=0
seafService="seafile.service"
2019-05-22 22:58:07 -06:00
seafHub="seahub.service"
seafDir="/opt/seafile"
2019-05-23 20:57:05 -06:00
seafData="/var/seafile"
2019-05-23 21:06:49 -06:00
### process startup parameters
while [ $# -gt 0 ]; do
case "$1" in
-h|-\?|--help)
# display help
scriptHelp
exit 0
;;
-l|--log)
# set log file location
if [ -n "$2" ]; then
logFile="${2%/}"
shift
else
badParam empty "$@"
2019-05-23 21:06:49 -06:00
fi
;;
-v|--verbose)
# set verbose logging from borg
borgCreateParams='--list --stats'
borgPruneParams='--list'
;;
-c|--config|--details)
# location of config details file
if [ -n "$2" ]; then
if [ -f "$2" ]; then
configDetails="${2%/}"
shift
else
badParam dne "$@"
fi
2019-05-23 21:06:49 -06:00
else
badParam empty "$@"
2019-05-23 21:06:49 -06:00
fi
;;
2019-05-24 20:41:00 -06:00
-5|--use-503)
# enable copying 503 error page to webroot
use503=1
;;
--503-path)
2019-05-23 21:06:49 -06:00
# FULL path to 503 file
if [ -n "$2" ]; then
2019-05-23 21:43:30 -06:00
if [ -f "$2" ]; then
err503Path="${2%/}"
err503File="${2##*/}"
shift
else
badParam dne "$@"
fi
2019-05-23 21:06:49 -06:00
else
badParam empty "$@"
2019-05-23 21:06:49 -06:00
fi
;;
-w|--webroot)
# path to webroot (copy 503)
if [ -n "$2" ]; then
2019-05-24 21:17:35 -06:00
if [ -d "$2" ]; then
2019-05-23 21:43:30 -06:00
webroot="${2%/}"
shift
else
badParam dne "$@"
fi
2019-05-23 21:06:49 -06:00
else
badParam empty "$@"
2019-05-23 21:06:49 -06:00
fi
;;
-p|--seaf|--seafdir)
# path to seafile program directory
if [ -n "$2" ]; then
2019-05-23 21:43:30 -06:00
if [ -d "$2" ]; then
seafDir="${2%/}"
shift
else
badParam dne "$@"
fi
2019-05-23 21:06:49 -06:00
else
badParam empty "$@"
2019-05-23 21:06:49 -06:00
fi
;;
-d|--data|--datadir|--seafdata)
# path to seafile data directory
if [ -n "$2" ]; then
2019-05-23 21:43:30 -06:00
if [ -d "$2" ]; then
seafData="${2%/}"
shift
else
badParam dne "$@"
fi
2019-05-23 21:06:49 -06:00
else
badParam empty "$@"
2019-05-23 21:06:49 -06:00
fi
;;
2019-05-24 00:31:07 -06:00
--seafile-service)
# name of seafile service
if [ -n "$2" ]; then
if ! systemctl list-unit-files | grep -Eq "^$2 |^$2.service"
then
badParam svc "$@"
else
seafService="${2}"
shift
fi
else
badParam empty "$@"
fi
;;
--seahub-service)
# name of seahub service
if [ -n "$2" ]; then
if ! systemctl list-unit-files | grep -Eq "^$2 |^$2.service"
then
badParam svc "$@"
else
seafHub="${2}"
shift
fi
else
badParam empty "$@"
fi
;;
2019-05-23 21:06:49 -06:00
-o|--offline)
# shutdown seafile during backup
offlineBackup=1
;;
*)
printf "${err}\nUnknown option: %s\n" "$1"
printf "${cyan}Use '--help' for valid options.{$norm}\n\n"
exit 1
;;
esac
shift
done
2019-05-21 17:27:10 -06:00
### check pre-requisites and default values
# does the details file exist?
if [ ! -f "$configDetails" ]; then
2019-05-24 21:18:01 -06:00
badParam dne "(--details default)" "$configDetails"
fi
# is borg installed?
if ! command -v borg > /dev/null; then
printf "\n${err}ERROR: BORG is not installed on this system!${norm}\n\n"
exit 3
fi
2019-05-24 21:34:08 -06:00
# seafile directories
if [ ! -d "$seafDir" ]; then
badParam dne "(--seafdir default)" "$seafDir"
elif [ ! -d "$seafData" ]; then
badParam dne "(--seafdata default)" "$seafData"
fi
# offline backup
2019-05-25 12:37:20 -06:00
if [ "$offlineBackup" -eq 1 ]; then
2019-05-24 21:34:08 -06:00
if ! systemctl list-unit-files | \
grep -Eq "^$seafService |^$seafService.service"
then
badParam svc "(--seafile-service default)" "$seafService"
elif ! systemctl list-unit-files | \
grep -Eq "^$seafHub |^$seafHub.service"
then
badParam svc "(--seahub-service default)" "$seafHub"
fi
fi
# if 503 functionality is enabled, do 503 related files exist?
2019-05-25 12:37:20 -06:00
if [ "$use503" -eq 1 ]; then
if [ ! -f "$err503Path" ]; then
badParam dne "(--503-path default)" "$err503Path"
elif [ ! -d "$webroot" ]; then
badParam dne "(--webroot default)" "$webroot"
fi
fi
2019-05-21 17:27:10 -06:00
### start logging
2019-05-27 06:10:06 -06:00
printf "${magenta}[%s] --- Start %s execution ---${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$scriptName" >> "$logFile"
2019-05-21 17:27:10 -06:00
printf "${cyan}[%s] -- [INFO] Log located at ${yellow}%s${cyan} --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$logFile" >> "$logFile"
2019-05-21 17:27:10 -06:00
2019-05-24 23:43:28 -06:00
### 503 functionality
2019-05-25 12:37:20 -06:00
if [ "$use503" -eq 1 ]; then
2019-05-24 23:43:28 -06:00
printf "${cyan}[%s] -- [INFO] Copying 503 error page to " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-24 23:43:28 -06:00
printf "webroot -- ${norm}\n" >> "$logFile"
if ! \cp --force "${err503Path}" "${webroot}/${err503File}" 2>> "$logFile"
then
2019-05-25 02:17:18 -06:00
printf "${warn}[%s] -- [WARNING] Failed to copy 503 error page. " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-25 02:32:37 -06:00
printf "Web users will NOT be notified --${norm}\n" >> "$logFile"
warnCount=$((warnCount+1))
2019-05-24 23:43:28 -06:00
else
printf "${ok}[%s] -- [SUCCESS] 503 error page copied --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
# set cleanup flag
err503Copied=1
2019-05-24 23:43:28 -06:00
fi
fi
### stop seahub and seafile service if offline backup requested
if [ "$offlineBackup" -eq 1 ]; then
printf "${cyan}[%s] -- [INFO] Stopping seafile services --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
seafSvc stop
fi
2019-05-22 22:58:07 -06:00
2019-05-25 00:59:23 -06:00
### read details file to get variables needed to dump sql and run borg
# check if config details file was provided as a relative or absolute path
case "${configDetails}" in
/*)
# absolute path, no need to rewrite variable
. "${configDetails}"
;;
*)
# relative path, prepend './' to create absolute path
. "./${configDetails}"
;;
esac
printf "${cyan}[%s] -- [INFO] ${yellow}%s${cyan} imported --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$configDetails" >> "$logFile"
2019-05-21 17:27:10 -06:00
2019-05-25 01:55:58 -06:00
### dump sql databases
## verify SQL variables are defined in details file
# sqlServer
if [ -z "${sqlServer}" ]; then
badDetails empty 'sqlServer'
fi
# sqlUser
if [ -z "${sqlUser}" ]; then
badDetails empty 'sqlUser'
fi
# sqlPass
if [ -z "${sqlPass}" ]; then
badDetails empty 'sqlPass'
fi
# database names
2019-06-20 03:39:57 -06:00
if [ -z "${ccnetDB_name}" ] || [ -z "${seafileDB_name}" ] || \
[ -z "${seahubDB_name}" ]; then
badDetails empty 'one or more mysql database names'
fi
## create tmp directory and generate dumpfile names
2019-05-25 01:55:58 -06:00
printf "${cyan}[%s] -- [INFO] Dumping SQL databases --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-25 01:55:58 -06:00
# create temporary directory to dump files before borg backup
if ! sqlDumpDir=$( mktemp -d 2>>"$logFile" ); then
2019-05-25 01:55:58 -06:00
exitError 111 "Could not create temporary directory to dump SQL files"
fi
# set cleanup flag
sqlCopied=1
2019-05-25 01:55:58 -06:00
printf "${cyan}[%s] -- [INFO] SQL dump files will be temporarily stored in:" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-25 01:55:58 -06:00
printf "\n${yellow}%s/${cyan} --${norm}\n" "$sqlDumpDir" >> "$logFile"
# create unique names for database dump files
sqlDump_ccnet="backup-$(date +%Y%m%d_%H%M%S)_${ccnetDB_name}.sql"
sqlDump_seafile="backup-$(date +%Y%m%d_%H%M%S)_${seafileDB_name}.sql"
sqlDump_seahub="backup-$(date +%Y%m%d_%H%M%S)_${seahubDB_name}.sql"
2019-05-25 01:55:58 -06:00
## dump databases
# dump CCNET-DB
if ! mysqldump -h"${sqlServer}" -u"${sqlUser}" -p"${sqlPass}" \
2019-06-20 03:39:57 -06:00
--opt "${ccnetDB_name}" > "${sqlDumpDir}/${sqlDump_ccnet}" \
2>> "$logFile"; then
2019-05-25 01:55:58 -06:00
exitError 115 "Could not dump ${ccnetDB_name} database"
fi
# dump SEAFILE-DB
if ! mysqldump -h"${sqlServer}" -u"${sqlUser}" -p"${sqlPass}" \
2019-06-20 03:39:57 -06:00
--opt "${seafileDB_name}" > "${sqlDumpDir}/${sqlDump_seafile}" \
2>> "$logFile"; then
2019-05-25 01:55:58 -06:00
exitError 116 "Could not dump ${seafileDB_name} database"
fi
# dump CCNET-DB
if ! mysqldump -h"${sqlServer}" -u"${sqlUser}" -p"${sqlPass}" \
2019-06-20 03:39:57 -06:00
--opt "${seahubDB_name}" > "${sqlDumpDir}/${sqlDump_seahub}" 2>> "$logFile"; then
2019-05-25 01:55:58 -06:00
exitError 117 "Could not dump ${seahubDB_name} database"
fi
printf "${ok}[%s] -- [SUCCESS] SQL databases dumped successfully --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-25 01:55:58 -06:00
2019-05-27 03:08:21 -06:00
### pre-backup tasks completed -- move to borg tasks
printf "${ok}[%s] -- [SUCCESS] Pre-backup tasks completed --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 03:08:21 -06:00
### Run borg variable checks
printf "${cyan}[%s] -- [INFO] Verifying supplied borg details --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 03:08:21 -06:00
## verify borg base directory
if [ -z "${borgBaseDir}" ]; then
badDetails empty 'borgBaseDir'
elif [ ! -d "${borgBaseDir}" ]; then
badDetails dne 'borgBaseDir'
fi
printf "${magenta}details:borgBaseDir ${norm}-- ${ok}[OK]${norm}\n" \
2019-05-27 06:10:06 -06:00
>> "$logFile"
2019-05-27 03:08:21 -06:00
export BORG_BASE_DIR="${borgBaseDir%/}"
## check path to SSH keyfile
if [ -z "${borgSSHKey}" ]; then
badDetails empty 'borgSSHKey'
elif [ ! -f "${borgSSHKey}" ]; then
2019-05-27 03:08:21 -06:00
badDetails dne 'borgSSHKey'
fi
printf "${magenta}details:borgSSHKey ${norm}-- ${ok}[OK]${norm}\n" \
2019-05-27 06:10:06 -06:00
>> "$logFile"
2019-05-27 03:08:21 -06:00
export BORG_RSH="ssh -i ${borgSSHKey}"
## check borg repo connect string
if [ -z "${borgConnectRepo}" ]; then
badDetails empty 'borgConnectRepo'
fi
printf "${magenta}details:borgConnectRepo ${norm}-- ${ok}[OK]${norm}\n" \
2019-05-27 06:10:06 -06:00
>> "$logFile"
2019-05-27 03:08:21 -06:00
export BORG_REPO="${borgConnectRepo}"
## check borg repo password
if [ -n "${borgRepoPassphrase}" ]; then
printf "${magenta}details:borgRepoPassphrase ${norm}-- ${ok}[OK]${norm}\n" \
2019-05-27 06:10:06 -06:00
>> "$logFile"
2019-05-27 03:08:21 -06:00
export BORG_PASSPHRASE="${borgRepoPassphrase}"
else
# if passwd is blank intentionally, this is insecure
printf "${warn} -- [WARNING] Using a borg repo without a password is an " \
>> "$logFile"
printf "insecure configuration --${norm}\n" >> "$logFile"
warnCount=$((warnCount+1))
2019-05-27 03:08:21 -06:00
# if this was an accident, we need to provide a bogus passwd so borg fails
# otherwise it will sit forever just waiting for input
export BORG_PASSPHRASE="DummyPasswordSoBorgFails"
fi
2019-05-27 05:31:08 -06:00
## export borg remote path, if specified
if [ -n "${borgRemote}" ]; then export BORG_REMOTE_PATH="${borgRemote}"; fi
## read additional files
if [ -n "${borgXtraListPath}" ]; then
# check if file actually exists
if [ ! -f "${borgXtraListPath}" ]; then
badDetails dne 'borgXtraListPath'
fi
# read file contents into concatenated list for echo to cmdline
while read -r xtraItem; do
if [ -z "${xtraList}" ]; then
xtraList="${xtraItem}"
else
xtraList="${xtraList} ${xtraItem}"
fi
done <<EOF
$( sed -e '/^\s*#.*$/d' -e '/^\s*$/d' "${borgXtraListPath}" )
EOF
2019-05-27 03:13:48 -06:00
printf "${magenta}details:borgXtraListPath ${norm}-- ${ok}[OK]${norm}\n" \
2019-05-27 06:10:06 -06:00
>> "$logFile"
2019-05-27 03:13:48 -06:00
includeXtra=1
fi
## check if exlusion list file is specified
if [ -n "${borgExcludeListPath}" ]; then
# check if the file actually exists
if [ ! -f "${borgExcludeListPath}" ]; then
badDetails dne 'borgExcludeListPath'
fi
exclusions=1
fi
2019-05-27 03:53:43 -06:00
### create borg temp dir:
## python requires a writable temporary directory when unpacking borg and
## executing commands. This defaults to /tmp but many systems mount /tmp with
## 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
# check if BORG_BASE_DIR/tmp exists, if not, create it
if [ ! -d "${borgBaseDir}/tmp" ]; then
if ! mkdir "${borgBaseDir}/tmp"; then
exitError 132 "Unable to create borg ${borgBaseDir}/tmp directory"
else
printf "${cyan}[%s] -- [INFO] Created ${yellow}%s/tmp " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "${borgBaseDir}" >> "$logFile"
2019-05-27 03:53:43 -06:00
printf "${cyan}--${norm}\n" >> "$logFile"
fi
fi
export TMPDIR="${borgBaseDir}/tmp"
2019-05-27 04:19:21 -06:00
### execute borg depending on whether extra files and/or exclusions are defined
## construct the proper borg commandline
# base command
if [ "$exclusions" -eq 0 ]; then
borgCMD="borg --show-rc create ${borgCreateParams} \
::$(date +%Y-%m-%d_%H%M%S) \
${seafDir} \
${seafData} \
${sqlDumpDir}"
elif [ "$exclusions" -eq 1 ]; then
borgCMD="borg --show-rc create ${borgCreateParams} \
--exclude-from ${borgExcludeListPath} \
::$(date +%Y-%m-%d_%H%M%S) \
${seafDir} \
${seafData} \
${sqlDumpDir}"
fi
2019-05-27 03:53:43 -06:00
2019-05-27 04:19:21 -06:00
# add extra locations if defined
if [ "$includeXtra" -eq 1 ]; then
borgCMD="${borgCMD} ${xtraList}"
fi
2019-05-27 03:53:43 -06:00
2019-05-27 04:19:21 -06:00
# execute borg
printf "${cyan}[%s] -- [INFO] Executing borg backup operation --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 04:19:21 -06:00
${borgCMD} 2>> "$logFile"
2019-05-27 04:31:21 -06:00
borgResult="$?"
## check borg exit status
if [ "$borgResult" -eq 0 ]; then
printf "${ok}[%s] -- [SUCCESS] Borg backup completed " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 04:31:21 -06:00
printf "successfully --${norm}\n" >> "$logFile"
elif [ "$borgResult" -eq 1 ]; then
printf "${warn}[%s] -- [WARNING] Borg completed with warnings. " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 04:31:21 -06:00
printf "Review this logfile for details --${norm}\n" >> "$logFile"
warnCount=$((warnCount+1))
2019-05-27 04:31:21 -06:00
elif [ "$borgResult" -ge 2 ]; then
err_1="Borg exited with a critical error. Please review this log file"
err_2="for details."
exitError 138 "$err_1 $err_2"
2019-05-27 04:31:21 -06:00
else
2019-05-27 05:24:47 -06:00
printf "${warn}[%s] -- [WARNING] Borg exited with unknown return code. " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 04:31:21 -06:00
printf "Review this logfile for details --${norm}\n" >> "$logFile"
warnCount=$((warnCount+1))
2019-05-27 04:31:21 -06:00
fi
2019-05-25 01:55:58 -06:00
2019-05-27 05:08:09 -06:00
### execute borg prune if paramters are provided, otherwise skip with a warning
if [ -n "${borgPruneSettings}" ]; then
printf "${cyan}[%s] -- [INFO] Executing borg prune operation --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
borg prune --show-rc -v ${borgPruneParams} ${borgPruneSettings} \
2019-05-27 05:08:09 -06:00
2>> "$logFile"
borgPruneResult="$?"
else
printf "${warn}[%s] -- [WARNING] No prune parameters provided. " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 05:08:09 -06:00
printf "Your archive will continue growing with each backup --${norm}\n" \
>> "$logFile"
warnCount=$((warnCount+1))
2019-05-27 05:08:09 -06:00
fi
## report on prune operation if executed
if [ -n "${borgPruneResult}" ]; then
if [ "${borgPruneResult}" -eq 0 ]; then
printf "${ok}[%s] -- [SUCCESS] Borg prune completed --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 05:08:09 -06:00
elif [ "$borgPruneResult" -eq 1 ]; then
printf "${warn}[%s] -- [WARNING] Borg prune completed with warnings. " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 05:08:09 -06:00
printf "Review this logfile for details --${norm}\n" >> "$logFile"
warnCount=$((warnCount+1))
2019-05-27 05:08:09 -06:00
elif [ "$borgPruneResult" -ge 2 ]; then
err_1="Borg prune exited with a critical error. Please review this"
err_2="log file for details."
exitError 139 "$err_1 $err_2"
2019-05-27 05:08:09 -06:00
else
printf "${warn}[%s] -- [WARNING] Borg prune exited with an unknown " \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 05:08:09 -06:00
printf "return code. Review this logfile for details --${norm}\n" \
>> "$logFile"
warnCount=$((warnCount+1))
2019-05-27 05:08:09 -06:00
fi
fi
2019-05-27 05:40:10 -06:00
### all processes successfully completed, cleanup and exit gracefully
2019-05-27 05:08:09 -06:00
2019-05-27 05:40:10 -06:00
# note successful completion of borg commands
printf "${ok}[%s] -- [SUCCESS] Backup operations completed --${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 05:08:09 -06:00
2019-05-27 05:40:10 -06:00
# cleanup
cleanup
2019-05-27 05:08:09 -06:00
# note complete success, tally warnings and exit
printf "${ok}[%s] -- [SUCCESS] All processes completed successfully --" \
"${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" >> "$logFile"
2019-05-27 06:10:06 -06:00
printf "${magenta}[%s] --- %s execution completed ---${norm}\n" \
2019-07-20 17:06:20 -06:00
"[$(stamp)]" "$scriptName" >> "$logFile"
if [ "$warnCount" -gt 0 ]; then
printf "${warn}%s warnings issued!${norm}\n" "${warnCount}" >> "$logFile"
else
printf "${ok}0 warnings issued.${norm}\n" >> "$logFile"
fi
2019-05-27 05:40:10 -06:00
exit 0
2019-05-27 05:08:09 -06:00
2019-05-21 17:27:10 -06:00
2019-05-23 21:19:28 -06:00
### error codes
# 1: parameter error
# 2: not run as root
# 3: borg not installed
2019-06-18 22:30:22 -06:00
# 99: TERM signal trapped
2019-05-23 21:19:28 -06:00
# 100: could not start seafile service
# 101: could not start seahub service
# 102: could not stop seafile service
2019-05-25 01:55:58 -06:00
# 103: could not stop seahub service
# 111: could not create tmp dir for SQL dump files
# 115: could not dump CCNET-DB
# 116: could not dump SEAFILE-DB
2019-05-27 03:09:21 -06:00
# 117: could not dump SEAHUB-DB
# 130: null configuration variable in details file
# 131: invalid configuration variable in details file
2019-05-27 05:08:09 -06:00
# 138: borg exited with a critical error
# 139: borg prune exited with a critical error