diff --git a/helper-scripts/nextcloud.sh b/helper-scripts/nextcloud.sh index 37c0d5f30..849a30167 100755 --- a/helper-scripts/nextcloud.sh +++ b/helper-scripts/nextcloud.sh @@ -9,9 +9,14 @@ done [[ -z ${1} ]] && NC_HELP=y while [ "$1" != '' ]; do + if [[ $# -ne 1 ]]; then + echo -e "\033[31mPlease use only one parameter at the same time!\033[0m" >&2 + exit 2 + fi case "${1}" in -p|--purge) NC_PURGE=y && shift;; -i|--install) NC_INSTALL=y && shift;; + -u|--update) NC_UPDATE=y && shift;; -r|--resetpw) NC_RESETPW=y && shift;; -h|--help) NC_HELP=y && shift;; *) echo "Unknown parameter: ${1}" && shift;; @@ -22,13 +27,11 @@ if [[ ${NC_HELP} == "y" ]]; then printf 'Usage:\n\n' printf ' -p|--purge\n Purge Nextcloud\n' printf ' -i|--install\n Install Nextcloud\n' + printf ' -u|--update\n Update Nextcloud\n' printf ' -r|--resetpw\n Reset password\n\n' exit 0 fi -[[ ${NC_PURGE} == "y" ]] && [[ ${NC_INSTALL} == "y" ]] && { echo "Cannot use -p and -i at the same time!"; exit 1; } -[[ ${NC_PURGE} == "y" ]] && [[ ${NC_RESETPW} == "y" ]] && { echo "Cannot use -p and -r at the same time!"; exit 1; } - SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" cd ${SCRIPT_DIR}/../ source mailcow.conf @@ -41,8 +44,27 @@ if [[ ${NC_PURGE} == "y" ]]; then exit 1 fi - docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \ - "$(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "SELECT IFNULL(GROUP_CONCAT('DROP TABLE ', TABLE_SCHEMA, '.', TABLE_NAME SEPARATOR ';'),'SELECT NULL;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'oc_%' AND TABLE_SCHEMA = '${DBNAME}';" -BN)" + echo -e "\033[33mDetecting Database information...\033[0m" + if [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "Show databases" | grep "nextcloud") ]]; then + echo -e "\033[32mFound seperate nextcloud Database (newer scheme)!\033[0m" + echo -e "\033[31mPurging...\033[0m" + docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "DROP DATABASE nextcloud;" > /dev/null + docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "DROP USER 'nextcloud'@'%';" > /dev/null + elif [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} mailcow -e "SHOW TABLES LIKE 'oc_%'") && $? -eq 0 ]]; then + echo -e "\033[32mFound nextcloud (oc) tables inside of mailcow Database (old scheme)!\033[0m" + echo -e "\033[31mPurging...\033[0m" + docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \ + "$(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "SELECT IFNULL(GROUP_CONCAT('DROP TABLE ', TABLE_SCHEMA, '.', TABLE_NAME SEPARATOR ';'),'SELECT NULL;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'oc_%' AND TABLE_SCHEMA = '${DBNAME}';" -BN)" > /dev/null + elif [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} mailcow -e "SHOW TABLES LIKE 'nc_%'") && $? -eq 0 ]]; then + echo -e "\033[32mFound nextcloud (nc) tables inside of mailcow Database (old scheme)!\033[0m" + echo -e "\033[31mPurging...\033[0m" + docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \ + "$(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "SELECT IFNULL(GROUP_CONCAT('DROP TABLE ', TABLE_SCHEMA, '.', TABLE_NAME SEPARATOR ';'),'SELECT NULL;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'nc_%' AND TABLE_SCHEMA = '${DBNAME}';" -BN)" > /dev/null + else + echo -e "\033[31mError: No Nextcloud Databases/Tables found!" + echo -e "\033[33mNot purging anything...\033[0m" + exit 1 + fi docker exec -it $(docker ps -f name=redis-mailcow -q) /bin/sh -c ' cat <&1 /dev/null + echo -ne "\r[3/4] Setting custom parameters inside the nextcloud config file" + echo "" docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) bash -c "/web/nextcloud/occ --no-warnings config:system:set redis host --value=redis --type=string; \ /web/nextcloud/occ --no-warnings config:system:set redis port --value=6379 --type=integer; \ /web/nextcloud/occ --no-warnings config:system:set redis timeout --value=0.0 --type=integer; \ @@ -141,13 +178,28 @@ elif [[ ${NC_INSTALL} == "y" ]]; then #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 arguments 0 --value={dovecot:143/imap/tls/novalidate-cert}; \ #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 class --value=OC_User_IMAP; \ + echo -e "\r[4/4] Enabling NGINX Configuration" cp ./data/assets/nextcloud/nextcloud.conf ./data/conf/nginx/ sed -i "s/NC_SUBD/${NC_SUBD}/g" ./data/conf/nginx/nextcloud.conf + sleep 2 - echo "Restarting Nginx..." + echo "" + echo -e "\033[33mFinalizing installation...\033[0m" docker restart $(docker ps -aqf name=nginx-mailcow) - echo "Login as admin with password: ${ADMIN_NC_PASS}" + echo "" + echo "******************************************" + echo "* SAVE THESE CREDENTIALS *" + echo "* INSTALL DATE: $(date +%Y-%m-%d_%H-%M-%S) *" + echo "******************************************" + echo "" + echo -e "\033[36mDatabase Name: ${NC_DBNAME}\033[0m" + echo -e "\033[36mDatabase User: ${NC_DBUSER}\033[0m" + echo -e "\033[36mDatabase Password: ${NC_DBPASS}\033[0m" + echo "" + echo -e "\033[31mUI Admin Password: ${ADMIN_NC_PASS}\033[0m" + echo "" + elif [[ ${NC_RESETPW} == "y" ]]; then printf 'You are about to set a new password for a Nextcloud user.\n\nDo not use this option if your Nextcloud is configured to use mailcow for authentication.\nSet a new password for the corresponding mailbox in mailcow, instead.\n\n'