diff --git a/restore_nextcloud.sh b/restore_nextcloud.sh index 5ba4131..7e5333c 100755 --- a/restore_nextcloud.sh +++ b/restore_nextcloud.sh @@ -260,21 +260,25 @@ echo "" # --- Some checks # ============= -DEFAULT_WEB_BASE_DIR="/var/www/$WEBSITE" DEFAULT_HTTP_USER="www-data" -DEFAULT_HTTP_GROUP="www-data" +DEFAULT_HTTP_USER="www-data" if [[ -z ${WEBSITE} ]] ; then fatal "No website given (parameter 'WEBSITE')" fi +DEFAULT_WEB_BASE_DIR="/var/www/$WEBSITE" +DEFAULT_PHP_ENGINE="FPM" + [[ -n "$WEB_BASE_DIR" ]] || WEB_BASE_DIR=$DEFAULT_WEB_BASE_DIR if [[ ! -d ${WEB_BASE_DIR} ]] ; then fatal "Web base directory not found (parameter 'WEB_BASE_DIR')" fi +[[ -n "$PHP_ENGINE" ]] || PHP_ENGINE=$DEFAULT_PHP_ENGINE + if [[ "$DATABASE_TYPE" != "postgres" ]] && [[ "$DATABASE_TYPE" != "mysql" ]]; then fatal "Wrong or missing database type (parameter 'DATABASE_TYPE')" fi @@ -293,34 +297,88 @@ if [[ "$DATABASE_TYPE" = "postgres" ]]; then fi fi -# - Determin user/group 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" +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 -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 '"'`" + 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 $httpd_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') + 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 '"'`" + + # - 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." @@ -330,17 +388,61 @@ if [[ -n "$_HTTP_USER" ]] ; then 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 + + +# Check PHP Version +# +if [[ "$PHP_ENGINE" = "FPM" ]] ; then + if [[ -z "$PHP_VERSION" ]] ; then + if [[ -z "$VHOST_CONFIG_FILE" ]] ; then + if $NGINX_IS_ENABLED ; then + + if [[ -f "/etc/nginx/sites-enabled/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="" + fi + + elif $APACHE2_IS_ENABLED ; then + + if [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" ]] ; then + VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" + elif [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" + elif [[ -f "/etc/apache2/sites-enabled/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="/etc/apache2/sites-enabled/${WEBSITE}.conf" + fi + + fi + fi + PHP_VERSION="$(grep -o -E "php-?.{1}\..{1}-fpm" $VHOST_CONFIG_FILE | grep -o -E ".{1}\..{1}")" + if [[ -z "$PHP_VERSION" ]] ; then + fatal "PHP Version must be givven if running PHP-FPM engine (parameter 'PHP_VERSION')" + fi fi -else - [[ -n "$HTTP_GROUP" ]] || HTTP_GROUP=$DEFAULT_HTTP_GROUP fi + +# - Determin PHP binary +# - +if [[ -x "/usr/local/php-${PHP_VERSION}/bin/php" ]] ; then + php_binary="/usr/local/php-${PHP_VERSION}/bin/php" +else + php_binary="$(realpath "$(which php)")" + if [[ -z "$php_binary" ]]; then + if [[ -x "/usr/local/php/bin/php" ]]; then + php_binary="/usr/local/php/bin/php" + else + fatal "No PHP binary present" + fi + else + if [[ ! -x "$php_binary" ]]; then + fatal "Found PHP binary '$php_binary', but this file is not executable!" + fi + fi +fi + + + # - Get available backup versions # - for _dir in $(ls -d ${WEB_BASE_DIR}/nextcloud-*) ; do @@ -389,8 +491,9 @@ read _IN done CURRENT_INSTALL_DIR=`realpath ${WEB_BASE_DIR}/nextcloud` -CURRENT_DATA_DIR=`realpath ${WEB_BASE_DIR}/data` -CURRENT_VERSION=`basename $CURRENT_INSTALL_DIR | cut -d"-" -f2` +CURRENT_VERSION="$(sudo -u $HTTP_USER $php_binary ${WEB_BASE_DIR}/htdocs/occ --version | cut -d' ' -f2)" + +DATA_DIR=${WEB_BASE_DIR}/data echo "" @@ -400,6 +503,14 @@ echo -e " Website to restore...................: $WEBSITE" echo "" echo -e " Current version of nextcloud.........: $CURRENT_VERSION" echo "" +echo -e " Web base directory...................: $WEB_BASE_DIR" +echo "" +echo -e " Webserver user.......................: $HTTP_USER" +echo "" +echo -e " PHP version..........................: $PHP_VERSION" +echo -e " PHP Engine...........................: $PHP_ENGINE" +echo -e " PHP binary...........................: $php_binary" +echo "" echo -e " Version to rstore....................: $RESTORE_VERSION" echo -e " Backup date was......................: $DATE_RESTORE_VERSION" echo "" @@ -426,9 +537,6 @@ fi echo "" -DATA_DIR=${WEB_BASE_DIR}/data-${VERSION} -INSTALL_DIR=${WEB_BASE_DIR}/nextcloud-${VERSION} - # - Deaktiviere Cronjobs # - @@ -465,24 +573,102 @@ echo -e "\033[37m\033[1mDoing some pre-script tasks..\033[m" # - echo "" echononl " Stop Apache Webserver.." -if $SYSTEMD_EXISTS ; then - systemctl stop apache2 - if [[ $? -eq 0 ]]; then - echo_ok +if $APACHE2_IS_ENABLED ; then + if $IS_HTTPD_RUNNING ; then + if $SYSTEMD_EXISTS ; then + systemctl stop apache2 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + + fi + else + /etc/init.d/apache2 stop + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + + fi + fi else - echo_failed - fatal "$(cat $log_file)" + echo_skipped fi else - /etc/init.d/apache2 stop - if [[ $? -eq 0 ]]; then - echo_ok + echo_skipped +fi + +# - Stop Nginx Webservice +# - +echo "" +echononl " Stop Nginx Webserver.." +if $NGINX_IS_ENABLED ; then + if $IS_HTTPD_RUNNING ; then + if $SYSTEMD_EXISTS ; then + systemctl stop nginx + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + + fi + else + /etc/init.d/nginx stop + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + + fi + fi else - echo_failed - fatal "$(cat $log_file)" + echo_skipped fi fi +echo "" + # ----- # - Backup/Save current Installation @@ -570,8 +756,7 @@ fi # - Backup current data directory # - echononl " Backup current data directory" -_current_data_dir="$(realpath ${WEB_BASE_DIR}/data)" -mv "$_current_data_dir" "${_current_data_dir}.$backup_date" > $log_file 2>&1 +mv "$DATA_DIR" "${DATA_DIR}.${CURRENT_VERSION}.$backup_date" > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -606,16 +791,16 @@ echo -e "\033[37m\033[1mRestore to Version '$RESTORE_VERSION'..\033[m" echo "" -# - Delete symlink for current data directory -# - -echononl " Remove symlink from current data dir 'data-${CURRENT_VERSION}'.." -rm ${WEB_BASE_DIR}/data > $log_file 2>&1 -if [[ $? -eq 0 ]]; then - echo_ok -else - echo_failed - fatal "$(cat $log_file)" -fi +## - Delete symlink for current data directory +## - +#echononl " Remove symlink from current data dir 'data-${CURRENT_VERSION}'.." +#rm ${WEB_BASE_DIR}/data > $log_file 2>&1 +#if [[ $? -eq 0 ]]; then +# echo_ok +#else +# echo_failed +# fatal "$(cat $log_file)" +#fi # - Remove symlink from current installation directory @@ -650,7 +835,7 @@ fi # - echononl " Move backuped data dir to current data dir" mv "${WEB_BASE_DIR}/data-${RESTORE_VERSION}.${DATE_RESTORE_VERSION}" \ - "${WEB_BASE_DIR}/data-${RESTORE_VERSION}" > $log_file 2>&1 + "${DATA_DIR}" > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -688,16 +873,16 @@ fi echo "" -# - Set symlink for data directory -# - -echononl " Set symlink for data dir 'data-${RESTORE_VERSION}'.." -ln -s data-${RESTORE_VERSION} ${WEB_BASE_DIR}/data > $log_file 2>&1 -if [[ $? -eq 0 ]]; then - echo_ok -else - echo_failed - fatal "$(cat $log_file)" -fi +## - Set symlink for data directory +## - +#echononl " Set symlink for data dir 'data-${RESTORE_VERSION}'.." +#ln -s data-${RESTORE_VERSION} ${WEB_BASE_DIR}/data > $log_file 2>&1 +#if [[ $? -eq 0 ]]; then +# echo_ok +#else +# echo_failed +# fatal "$(cat $log_file)" +#fi # - Set symlink (documentroot) for installation directory # - @@ -728,7 +913,17 @@ if [[ "$PHP_ENGINE" = "FPM" ]]; then echo_ok else echo_failed - fatal "$(cat $log_file)" + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + fi else /etc/init.d/php-${PHP_VERSION}-fpm restart > $log_file 2>&1 @@ -736,7 +931,17 @@ if [[ "$PHP_ENGINE" = "FPM" ]]; then echo_ok else echo_failed - fatal "$(cat $log_file)" + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + fi fi else @@ -747,29 +952,105 @@ fi # - echo "" echononl " Start Apache Webserver.." -if $IS_HTTPD_RUNNING ; then - if $SYSTEMD_EXISTS ; then - systemctl start apache2 > $log_file 2>&1 - if [[ $? -eq 0 ]]; then - echo_ok +if $APACHE2_IS_ENABLED ; then + if $IS_HTTPD_RUNNING ; then + if $SYSTEMD_EXISTS ; then + systemctl start apache2 > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + + fi else - echo_failed - fatal "$(cat $log_file)" + /etc/init.d/apache2 start > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + + fi fi else - /etc/init.d/apache2 start > $log_file 2>&1 - if [[ $? -eq 0 ]]; then - echo_ok - else - echo_failed - fatal "$(cat $log_file)" - fi + echo_skipped + warn "The webserver was not running, so it will be keept down!" fi else echo_skipped - warn "The webserver was not running, so it will be keept down!" fi + +# - Start NGINX Webservise +# - +echo "" +echononl " Start Nginx Webserver.." +if $NGINX_IS_ENABLED ; then + if $IS_HTTPD_RUNNING ; then + if $SYSTEMD_EXISTS ; then + systemctl start nginx > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + + fi + else + /etc/init.d/nginx start > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user." + fi + fi + else + echo_skipped + warn "The NGINX is not configured as active - so nothing to do." + fi +else + echo_skipped +fi + + _redis_cli_bin="$(which redis-cli)" if [[ -z "$_redis_cli_bin" ]]; then if [[ -x "/usr/local/bin/redis-cli" ]]; then @@ -813,53 +1094,3 @@ fi blank_line clean_up 0 - - -cd $WEB_BASE_DIR - - ------------------------------------------------------------------------- - -## - Alternativly enable maintenance mode -## - -su -c "/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ maintenance:mode --on" -s /bin/bash $HTTP_USER - - - -## - Deactivate third party apps -## - -## - for _app in $THIRD_PARTY_APPS ; do -## - #su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ app:disable $_app" -s /bin/bash $HTTP_USER -## - su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/nextcloud/occ app:disable $_app" -s /bin/bash $HTTP_USER -## - done -for _app in $THIRD_PARTY_APPS ; do - #su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ app:disable $_app" -s /bin/bash $HTTP_USER - su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/nextcloud/occ app:disable $_app" -s /bin/bash $HTTP_USER -done - - - - -## - ## - Enable third party apps -## - ## - -## - for _app in $THIRD_PARTY_APPS ; do -## - su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ app:enable $_app" -s /bin/bash $HTTP_USER -## - done -for _app in $THIRD_PARTY_APPS ; do - su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ app:enable $_app" -s /bin/bash $HTTP_USER -done - - ------------------------------------------------------------------------- - -https://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html - -while IFS= read -r -d '' n; do - #if [[ "$n" == "mb" ]]; then - # continue - #fi - su -c "/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/console.php files:scan --path `basename $n`" -s /bin/bash $HTTP_USER -done < <(find $WEB_BASE_DIR/data/ -mindepth 1 -maxdepth 1 -type d -print0) - -su -c "/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/console.php files:scan --all" -s /bin/bash $HTTP_USER -su -c "/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ maintenance:repair" -s /bin/bash $HTTP_USER