diff --git a/occ_maintenance.sh b/occ_maintenance.sh new file mode 100755 index 0000000..95fea6a --- /dev/null +++ b/occ_maintenance.sh @@ -0,0 +1,537 @@ +#!/usr/bin/env bash + +script_name="$(basename $(realpath $0))" +script_dir="$(dirname $(realpath $0))" + +conf_dir="${script_dir}/conf" + +declare -a unsorted_website_arr +declare -a website_arr + +log_file="$(mktemp)" + +backup_date=$(date +%Y-%m-%d-%H%M) + + +# ============= +# --- Some functions +# ============= + +usage() { + + [[ -n "$1" ]] && error "$1" + + [[ $terminal ]] && echo -e " +\033[1mUsage:\033[m + + $(basename $0) -s + +\033[1mDescription\033[m + + Running a set off 'occ' commands for maintenance nextcloud installation + + occ maintenance:repair + occ files:cleanup + occ files:scan --all + occ files:scan-app-data + occ trashbin:cleanup --all-users + + for all existing users + +\033[1mOptions\033[m + + -s + The site of the nextcloud instance. + +\033[1mExample:\033[m + + Runs this set of scripts on system 'cloud-01.oopen.de' + + $(basename $0) -s cloud-01.oopen.de +" + + clean_up 1 + +} + + +clean_up() { + + # Perform program exit housekeeping + rm -f $log_file + blank_line + exit $1 +} + +is_number() { + + return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); + + # - also possible + # - + #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 + #return $([[ ! -z "${1##*[!0-9]*}" ]]) +} + +echononl(){ + if $terminal ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n "$*\\c" 1>&2 + else + echo -e -n "$*" 1>&2 + fi + rm /tmp/shprompt$$ + fi +} +echo_done() { + if $terminal ; then + echo -e "\033[75G[ \033[32mdone\033[m ]" + fi +} +echo_ok() { + if $terminal ; then + echo -e "\033[75G[ \033[32mok\033[m ]" + fi +} +echo_warning() { + if $terminal ; then + echo -e "\033[75G[ \033[33m\033[1mwarn\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[75G[ \033[37mskipped\033[m ]" + fi +} + +fatal (){ + echo "" + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: \033[37m\033[1m$*\033[m" + echo "" + echo -e " \033[31m\033[1mScript will be interrupted..\033[m!" + else + echo " [ Fatal ]: $*" + echo "" + echo " Script was terminated...." + fi + clean_up 1 +} + +error(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ]: $*" + else + echo " [ Error ]: $*" + fi + echo "" +} + +warn (){ + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + else + echo " [ Warning ]: $*" + fi + echo "" +} + +info (){ + if $terminal ; then + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + fi +} + +# - Remove leading/trailling whitespaces +# - +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +## - Check if a given array (parameter 2) contains a given string (parameter 1) +## - +containsElement () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + + + +# ------------- +# - Read in Commandline arguments +# ------------- +while getopts hs: opt ; do + case $opt in + h) usage ;; + s) WEBSITE=$OPTARG ;; + \?) usage + esac +done + +if [[ -z "$WEBSITE" ]] ; then + + while IFS='' read -r -d '' _conf_file ; do + source $_conf_file + if [[ -n "$WEBSITE" ]] ; then + unsorted_website_arr+=("${WEBSITE}:$_conf_file") + fi + WEBSITE="" + done < <(find "${conf_dir}" -maxdepth 1 -type f -name "*.conf" -print0) + + # - Sort array + # - + IFS=$'\n' website_arr=($(sort <<<"${unsorted_website_arr[*]}")) + + WEBSITE= + _OK=false + echo "" + echo "Which site would you like to update?" + echo "" + declare -i i=0 + for _site in ${website_arr[@]} ; do + IFS=':' read -a _arr <<< ${_site} + echo " [$i] ${_arr[0]}" + ((i++)) + done + echo + echononl " Eingabe: " + while ! $_OK ; do + read _IN + if is_number "$_IN" && [[ -n ${website_arr[$_IN]} ]]; then + IFS=':' read -a _arr <<< ${website_arr[$_IN]} + conf_file=${_arr[1]} + _WEBSITE="${_arr[0]}" + _OK=true + else + echo "" + echo -e "\tFalsche Eingabe !" + echo "" + echononl " Eingabe: " + fi + done + + WEBSITE="$_WEBSITE" + + #fatal "No website ios given on commandline - Missing Parameter '-s '" +fi + +WEB_BASE_DIR="/var/www/${WEBSITE}" + +if [[ ! -d ${WEB_BASE_DIR} ]] ; then + fatal "Web base directory '$WEB_BASE_DIR' not found!" +fi + +DATA_DIR="$(realpath ${WEB_BASE_DIR}/data)" +INSTALL_DIR="$(realpath ${WEB_BASE_DIR}/nextcloud)" +CURRENT_VERSION="$(basename $INSTALL_DIR | cut -d"-" -f2)" + + +# ============= +# --- Some +# ============= + +# - Support systemd ? +# - +if [[ "X$(which systemd)" = "X" ]]; then + SYSTEMD_EXISTS=false +else + SYSTEMD_EXISTS=true +fi + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + +#clear + +if $terminal ; then + echo "" + echo "" + echo "" + echo -e "\033[32m-----\033[m" + echo -e "Trigger a set of occ comands on system \033[1m${WEB_BASE_DIR}\033[m" + echo -e "\033[1m + occ maintenance:repair + occ files:cleanup + occ files:scan --all + occ files:scan-app-data + occ trashbin:cleanup --all-users\033[m" + echo -e "\033[32m-----\033[m" +fi + + +# ============= +# --- Some checks +# ============= + +DEFAULT_HTTP_USER="www-data" +DEFAULT_HTTP_GROUP="www-data" + +NGINX_IS_ENABLED=false +APACHE2_IS_ENABLED=false + +# Check if NGINX webserver is ctive +# +if $(systemctl -q is-enabled nginx 2> /dev/null) ; then + + NGINX_IS_ENABLED=true + + # - Determin user of the webserver + # - + nginx_binary="$(which nginx)" + if [[ -z "$nginx_binary" ]] ; then + nginx_binary="$(ps -axu | grep -E "nginx:.*master" | grep -v grep | grep -o -E "\S+/nginx")" + if [[ -z "$nginx_binary" ]] ; then + if [[ -x "/usr/local/nginx/bin/nginx" ]]; then + nginx_binary="/usr/local/nginx/bin/nginx" + elif [[ -x "/usr/local/nginx/sbin/nginx" ]]; then + nginx_binary="/usr/local/nginx/sbin/nginx" + fi + fi + fi + + if [[ -x "$nginx_binary" ]] ; then + _HTTP_USER="$($nginx_binary -T 2> /dev/null | grep -E "^\s*user\s+\S+;" | grep -o -E "\S+;$" | sed -e 's/;$//')" + fi + + # - Is webserver running ? + # - + PID=$(ps aux | grep "$(realpath $nginx_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') + if [[ "X${PID}X" = "XX" ]] ;then + IS_HTTPD_RUNNING=false + else + IS_HTTPD_RUNNING=true + fi + + +elif $(systemctl -q is-enabled apache2 2> /dev/null) ; then + + APACHE2_IS_ENABLED=true + + # - Determin user of the webserver + # - + httpd_binary="`which httpd`" + if [ -z "$httpd_binary" ]; then + httpd_binary="$(ps -axu | grep httpd | grep -e "^root" | grep -v grep | awk '{print$11}')" + if [ -z "$httpd_binary" ]; then + if [ -x "/usr/local/apache2/bin/httpd" ]; then + httpd_binary="/usr/local/apache2/bin/httpd" + fi + fi + fi + if [ -x "$httpd_binary" ];then + + # - Determin websever user + # - + _HTTP_USER="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^User" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + _HTTP_GROUP="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^Group" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + + # - Is webserver running ? + # - + PID=$(ps aux | grep "$(realpath $httpd_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') + if [[ "X${PID}X" = "XX" ]] ;then + IS_HTTPD_RUNNING=false + else + IS_HTTPD_RUNNING=true + fi + fi + +else + error "Neither \033[1mapache2\033[m nor \033[1mnginx\033[m is enabled on this machine" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/nno]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user" +fi + +if [[ -n "$_HTTP_USER" ]] ; then + if [[ -n "$HTTP_USER" ]] && [[ "$_HTTP_USER" != "$HTTP_USER" ]]; then + warn "The script has determined \033[1;37m${_HTTP_USER}\033[m as Webservers user. This\n value differs from the value given in your configuration file, \n which is \033[1;37m${HTTP_USER}\033[m and takes precedence." + else + HTTP_USER=$_HTTP_USER + fi +else + [[ -n "$HTTP_USER" ]] || HTTP_USER=$DEFAULT_HTTP_USER +fi +if [[ -n "$_HTTP_GROUP" ]] ; then + if [[ -n "$HTTP_GROUP" ]] && [[ "$_HTTP_GROUP" != "$HTTP_GROUP" ]]; then + warn "The script has determined \033[1;37m${_HTTP_GROUP}\033[m as Webservers group. This\n value differs from the value given in your configuration file, \n which is \033[1;37m${HTTP_GROUP}\033[m and takes precedence." + else + HTTP_GROUP=$_HTTP_GROUP + fi +else + [[ -n "$HTTP_GROUP" ]] || HTTP_GROUP=$DEFAULT_HTTP_GROUP +fi + +if [[ -x "$(realpath /usr/local/php/bin/php)" ]]; then + PHP_BIN="/usr/local/php/bin/php" +else + PHP_BIN="$(which php)" +fi + +if [[ ! -x "$PHP_BIN" ]]; then + fatal "No PHP binary found!" +fi + + + +if $terminal ; then + echo "" + echo -e "\033[1;32mStarting Script for \033[1;37m${WEBSITE}\033[m" + echo "" + echo -e " Cloud instance to be changed.........: $WEBSITE" + echo "" + echo -e " Current version of nextcloud.........: $CURRENT_VERSION" + echo "" + echo -e " Web base directory...................: $WEB_BASE_DIR" + echo -e " Install directory....................: $INSTALL_DIR" + echo -e " Data directory.......................: $DATA_DIR" + echo "" + echo -e " Webserver user.......................: $HTTP_USER" + echo -e " Webserver group......................: $HTTP_GROUP" + echo "" + echo -e " PHP command..........................: $PHP_BIN" + echo "" + + echo "" + echo -n " Type upper case 'YES' to continue executing with this parameters: " + read OK + if [[ "$OK" = "YES" ]] ; then + echo "" + echo "" + echo -e "\033[1;32mGoing to run \033[1;37mfiles:scan\033[1;32m for each user on \033[1;37m$WEBSITE \033[m" + else + fatal "Abort by user request - Answer as not 'YES'" + fi +fi + + +# ----- +# - Main part of the script +# ----- + +if $terminal ; then + echo "" + echo "" + echo -e "\033[37m\033[1mMain part of the script\033[m" + echo "" +fi + +# Maintenace mode must be off !! +# +su -c "$PHP_BIN ${INSTALL_DIR}/occ maintenance:mode --off" -s /bin/bash $HTTP_USER > $log_file 2>&1 + +echononl " Running 'occ maintenance:repair'.." +su -c "$PHP_BIN ${INSTALL_DIR}/occ maintenance:repair" -s /bin/bash $HTTP_USER > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat "$log_file")" +fi + +blank_line + +echononl " Running 'occ files:cleanup'.." +su -c "$PHP_BIN ${INSTALL_DIR}/occ files:cleanup" -s /bin/bash $HTTP_USER > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat "$log_file")" +fi + +blank_line + +echononl " Running 'occ files:scan --all'.." +su -c "$PHP_BIN ${INSTALL_DIR}/occ files:scan --all" -s /bin/bash $HTTP_USER > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat "$log_file")" +fi + +blank_line + +echononl " Running 'occ files:scan-app-data'.." +su -c "$PHP_BIN ${INSTALL_DIR}/occ files:scan-app-data" -s /bin/bash $HTTP_USER > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat "$log_file")" +fi + +blank_line + +echononl " Running 'occ trashbin:cleanup --all-users'.." +su -c "$PHP_BIN ${INSTALL_DIR}/occ trashbin:cleanup --all-users" -s /bin/bash $HTTP_USER > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat "$log_file")" +fi + +if $terminal ; then + echo "" + echo "" + echo -e "\033[37m\033[1mUpdate Apps\033[m" + echo "" +fi + +echononl " Running 'occ app:update --all'.." +su -c "$PHP_BIN ${INSTALL_DIR}/occ app:update --all" -s /bin/bash $HTTP_USER > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat "$log_file")" +fi + + + +blank_line + +clean_up 0