2019-05-21 17:27:10 -06:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
#######
|
|
|
|
### Backup Seafile/Seafile Pro server on bare-metal (NOT DOCKER)
|
|
|
|
#######
|
|
|
|
|
|
|
|
|
|
|
|
### text formatting presents
|
|
|
|
norm="\e[0m"
|
|
|
|
bold="\e[1m"
|
|
|
|
err="\e[1;31m"
|
|
|
|
warn="\e[1;93m"
|
|
|
|
ok="\e[32m"
|
|
|
|
yellow="\e[93m"
|
|
|
|
cyan="\e[96m"
|
|
|
|
mag="\e[95m"
|
|
|
|
|
|
|
|
|
|
|
|
### functions
|
|
|
|
|
2019-05-23 21:24:29 -06:00
|
|
|
# bad parameter passed to script
|
|
|
|
badParam () {
|
2019-05-24 00:30:34 -06:00
|
|
|
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
|
2019-05-24 00:30:34 -06:00
|
|
|
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
|
2019-05-23 21:42:35 -06:00
|
|
|
elif [ "$1" = "user" ]; then
|
|
|
|
printf "${err}\nError: '%s %s': User does not exist!${norm}\n\n" \
|
2019-05-23 21:24:29 -06:00
|
|
|
"$2" "$3"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-05-23 23:07:44 -06:00
|
|
|
exitError () {
|
2019-05-23 23:18:35 -06:00
|
|
|
printf "${err}[%s] -- [ERROR] %s: %s --${norm}\n" \
|
|
|
|
"$(stamp)" "$1" "$2" >> "$logFile"
|
2019-05-23 23:07:44 -06:00
|
|
|
exit "$1"
|
|
|
|
}
|
|
|
|
|
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}"
|
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}"
|
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}"
|
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}"
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
### 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'
|
2019-05-23 21:46:34 -06:00
|
|
|
scriptPath="$( CDPATH='' cd -- "$( dirname -- "$0" )" && pwd -P )"
|
2019-05-21 17:27:10 -06:00
|
|
|
scriptName="$( basename "$0" )"
|
|
|
|
logFile="$scriptPath/${scriptName%.*}.log"
|
2019-05-23 20:57:05 -06:00
|
|
|
# borg output verbosity -- normal
|
|
|
|
borgCreateParams='--stats'
|
|
|
|
borgPruneParams='--list'
|
2019-05-23 21:06:49 -06:00
|
|
|
configDetails="$scriptPath/seafbackup.details"
|
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
|
2019-05-23 21:54:37 -06:00
|
|
|
seafService="seafile.service"
|
2019-05-22 22:58:07 -06:00
|
|
|
seafHub="seahub.service"
|
2019-05-23 20:57:05 -06:00
|
|
|
seafUser="seafile"
|
2019-05-22 22:58:07 -06:00
|
|
|
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
|
2019-05-23 21:24:29 -06:00
|
|
|
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
|
2019-05-23 21:24:29 -06:00
|
|
|
if [ -f "$2" ]; then
|
|
|
|
configDetails="${2%/}"
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
badParam dne "$@"
|
|
|
|
fi
|
2019-05-23 21:06:49 -06:00
|
|
|
else
|
2019-05-23 21:24:29 -06:00
|
|
|
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
|
2019-05-23 21:24:29 -06:00
|
|
|
badParam empty "$@"
|
2019-05-23 21:06:49 -06:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
-w|--webroot)
|
|
|
|
# path to webroot (copy 503)
|
|
|
|
if [ -n "$2" ]; then
|
2019-05-23 21:43:30 -06:00
|
|
|
if [ -f "$2" ]; then
|
|
|
|
webroot="${2%/}"
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
badParam dne "$@"
|
|
|
|
fi
|
2019-05-23 21:06:49 -06:00
|
|
|
else
|
2019-05-23 21:24:29 -06:00
|
|
|
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
|
2019-05-23 21:24:29 -06:00
|
|
|
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
|
2019-05-23 21:24:29 -06:00
|
|
|
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
|
|
|
|
;;
|
|
|
|
-u|--user|--seafuser)
|
|
|
|
# name of seafile system user
|
|
|
|
if [ -n "$2" ]; then
|
2019-05-23 21:43:12 -06:00
|
|
|
if ! id "$2" > /dev/null 2>&1; then
|
|
|
|
badParam user "$@"
|
|
|
|
else
|
|
|
|
seafUser="${2%/}"
|
|
|
|
shift
|
|
|
|
fi
|
2019-05-23 21:06:49 -06:00
|
|
|
else
|
2019-05-23 21:24:29 -06:00
|
|
|
badParam empty "$@"
|
2019-05-23 21:06:49 -06:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
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
|
|
|
|
|
|
|
### start logging
|
|
|
|
printf "${mag}[%s] --- Start %s execution ---${norm}\n" \
|
|
|
|
"$(stamp)" "$scriptName" >> "$logFile"
|
|
|
|
printf "${cyan}[%s] -- [INFO] Log located at ${yellow}%s${cyan} --${norm}\n" \
|
|
|
|
"$(stamp)" "$logFile" >> "$logFile"
|
|
|
|
|
|
|
|
|
2019-05-23 21:54:57 -06:00
|
|
|
### stop seahub and seafile service if offline backup requested
|
|
|
|
if [ "$offlineBackup" -eq 1 ]; then
|
|
|
|
seafSvc stop
|
|
|
|
fi
|
2019-05-22 22:58:07 -06:00
|
|
|
|
|
|
|
|
2019-05-21 17:27:10 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-23 21:19:28 -06:00
|
|
|
exit 0
|
|
|
|
|
|
|
|
### error codes
|
|
|
|
# 1: parameter error
|
|
|
|
# 2: not run as root
|
|
|
|
# 100: could not start seafile service
|
|
|
|
# 101: could not start seahub service
|
|
|
|
# 102: could not stop seafile service
|
|
|
|
# 103: could not stop seahub service
|