nextcloud.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/env bash
  2. # renovate: datasource=github-releases depName=nextcloud/server versioning=semver extractVersion=^v(?<version>.*)$
  3. NEXTCLOUD_VERSION=27.1.2
  4. echo -ne "Checking prerequisites..."
  5. sleep 1
  6. for bin in curl dirmngr tar bzip2; do
  7. if [[ -z $(which ${bin}) ]]; then echo -ne "\r\033[31mCannot find ${bin}, exiting...\033[0m\n"; exit 1; fi
  8. done
  9. echo -ne "\r\033[32mFound all prerequisites! Continuing...\033[0m\n"
  10. [[ -z ${1} ]] && NC_HELP=y
  11. while [ "$1" != '' ]; do
  12. if [[ $# -ne 1 ]]; then
  13. echo -e "\033[31mPlease use only one parameter at the same time!\033[0m" >&2
  14. exit 2
  15. fi
  16. case "${1}" in
  17. -p|--purge) NC_PURGE=y && shift;;
  18. -i|--install) NC_INSTALL=y && shift;;
  19. -u|--update) NC_UPDATE=y && shift;;
  20. -r|--resetpw) NC_RESETPW=y && shift;;
  21. -h|--help) NC_HELP=y && shift;;
  22. *) echo "Unknown parameter: ${1}" && shift;;
  23. esac
  24. done
  25. if [[ ${NC_HELP} == "y" ]]; then
  26. printf 'Usage:\n\n'
  27. printf ' -p|--purge\n Purge Nextcloud\n'
  28. printf ' -i|--install\n Install Nextcloud\n'
  29. printf ' -u|--update\n Update Nextcloud\n'
  30. printf ' -r|--resetpw\n Reset password\n\n'
  31. exit 0
  32. fi
  33. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  34. cd ${SCRIPT_DIR}/../
  35. source mailcow.conf
  36. if [[ ${NC_PURGE} == "y" ]]; then
  37. read -r -p "Are you sure you want to purge Nextcloud? [y/N] " response
  38. response=${response,,}
  39. if [[ ! "$response" =~ ^(yes|y)$ ]]; then
  40. echo "OK, aborting."
  41. exit 1
  42. fi
  43. echo -e "\033[33mDetecting Database information...\033[0m"
  44. if [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "Show databases" | grep "nextcloud") ]]; then
  45. echo -e "\033[32mFound seperate Nextcloud database (newer scheme)!\033[0m"
  46. echo -e "\033[31mPurging...\033[0m"
  47. docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "DROP DATABASE nextcloud;" > /dev/null
  48. docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "DROP USER 'nextcloud'@'%';" > /dev/null
  49. elif [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} mailcow -e "SHOW TABLES LIKE 'oc_%'") && $? -eq 0 ]]; then
  50. echo -e "\033[32mFound Nextcloud (oc) tables inside of mailcow database (old scheme)!\033[0m"
  51. echo -e "\033[31mPurging...\033[0m"
  52. docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \
  53. "$(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
  54. elif [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} mailcow -e "SHOW TABLES LIKE 'nc_%'") && $? -eq 0 ]]; then
  55. echo -e "\033[32mFound Nextcloud (nc) tables inside of mailcow database (old scheme)!\033[0m"
  56. echo -e "\033[31mPurging...\033[0m"
  57. docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \
  58. "$(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
  59. else
  60. echo -e "\033[31mError: No Nextcloud databases/tables found!"
  61. echo -e "\033[33mNot purging anything...\033[0m"
  62. exit 1
  63. fi
  64. docker exec -it $(docker ps -f name=redis-mailcow -q) /bin/sh -c ' cat <<EOF | redis-cli
  65. SELECT 10
  66. FLUSHDB
  67. EOF
  68. '
  69. if [ -d ./data/web/nextcloud/config ]; then
  70. mv ./data/web/nextcloud/config/ ./data/conf/nextcloud-config-folder-$(date +%s).bak
  71. fi
  72. [[ -d ./data/web/nextcloud ]] && rm -rf ./data/web/nextcloud
  73. [[ -f ./data/conf/nginx/site.nextcloud.custom ]] && mv ./data/conf/nginx/site.nextcloud.custom ./data/conf/nginx/site.nextcloud.custom-$(date +%s).bak
  74. [[ -f ./data/conf/nginx/nextcloud.conf ]] && mv ./data/conf/nginx/nextcloud.conf ./data/conf/nginx/nextcloud.conf-$(date +%s).bak
  75. docker restart $(docker ps -aqf name=nginx-mailcow)
  76. echo -e "\033[32mNextcloud has been uninstalled sucessfully!\033[0m"
  77. elif [[ ${NC_UPDATE} == "y" ]]; then
  78. read -r -p "Are you sure you want to update Nextcloud (with Nextclouds own updater)? [y/N] " response
  79. response=${response,,}
  80. if [[ ! "$response" =~ ^(yes|y)$ ]]; then
  81. echo "OK, aborting."
  82. exit 1
  83. fi
  84. if [ ! -f data/web/nextcloud/occ ]; then
  85. echo -e "\033[31mError: Nextcloud occ not found. Is Nextcloud installed?\033[0m"
  86. exit 1
  87. fi
  88. if grep -Pq 'This version of Nextcloud is not compatible with (?:PHP)?(?>=?)(?:PHP)?(?>.+)' <<<$(docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) bash -c "/web/nextcloud/occ --no-warnings status"); then
  89. echo -e "\033[31mError: This version of Nextcloud is not compatible with the current PHP version of php-fpm-mailcow, we'll fix it\033[0m"
  90. wget -q https://raw.githubusercontent.com/nextcloud/server/v26.0.0/lib/versioncheck.php -O ./data/web/nextcloud/lib/versioncheck.php
  91. echo -e "\e[33mPlease restart the update again.\e[0m"
  92. elif ! grep -q 'installed: true' <<<$(docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) bash -c "/web/nextcloud/occ --no-warnings status"); then
  93. echo -e "\033[31mError: Nextcloud seems not to be installed.\033[0m"
  94. exit 1
  95. else
  96. docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) bash -c "php /web/nextcloud/updater/updater.phar"
  97. fi
  98. elif [[ ${NC_INSTALL} == "y" ]]; then
  99. NC_SUBD=
  100. while [[ -z ${NC_SUBD} ]]; do
  101. read -p "Subdomain to run Nextcloud from [format: nextcloud.domain.tld]: " NC_SUBD
  102. done
  103. if ! ping -q -c2 ${NC_SUBD} > /dev/null 2>&1 ; then
  104. read -p "Cannot ping subdomain, continue anyway? [y|N] " NC_CONT_FAIL
  105. [[ ! ${NC_CONT_FAIL,,} =~ ^(yes|y)$ ]] && { echo "Ok, exiting..."; exit 1; }
  106. fi
  107. echo -e "\033[33mDownloading \033[34mNextcloud ${NEXTCLOUD_VERSION}\033[33m...\033[0m"
  108. curl -L# -o nextcloud.tar.bz2 "https://download.nextcloud.com/server/releases/nextcloud-$NEXTCLOUD_VERSION.tar.bz2" || { echo "Failed to download Nextcloud archive."; exit 1; } \
  109. && tar -xjf nextcloud.tar.bz2 -C ./data/web/ \
  110. && rm nextcloud.tar.bz2 \
  111. && mkdir -p ./data/web/nextcloud/data \
  112. && chmod +x ./data/web/nextcloud/occ
  113. echo -e "\033[33mCreating 'nextcloud' database...\033[0m"
  114. NC_DBPASS=$(</dev/urandom tr -dc A-Za-z0-9 2> /dev/null | head -c 28)
  115. NC_DBUSER=nextcloud
  116. NC_DBNAME=nextcloud
  117. echo -ne "[1/3] Creating 'nextcloud' database"
  118. docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "CREATE DATABASE ${NC_DBNAME};"
  119. sleep 2
  120. echo -ne "\r[2/3] Creating 'nextcloud' database user"
  121. docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "CREATE USER '${NC_DBUSER}'@'%' IDENTIFIED BY '${NC_DBPASS}';"
  122. sleep 2
  123. echo -ne "\r[3/3] Granting 'nextcloud' user all permissions on database 'nextcloud'"
  124. docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "GRANT ALL PRIVILEGES ON ${NC_DBNAME}.* TO '${NC_DBUSER}'@'%';"
  125. sleep 2
  126. echo ""
  127. echo -e "\033[33mInstalling Nextcloud...\033[0m"
  128. ADMIN_NC_PASS=$(</dev/urandom tr -dc A-Za-z0-9 2> /dev/null | head -c 28)
  129. echo -ne "[1/4] Setting correct permissions for www-data"
  130. docker exec -it $(docker ps -f name=php-fpm-mailcow -q) /bin/bash -c "chown -R www-data:www-data /web/nextcloud"
  131. sleep 2
  132. echo -ne "\r[2/4] Running occ maintenance:install to install Nextcloud"
  133. docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) /web/nextcloud/occ --no-warnings maintenance:install \
  134. --database mysql \
  135. --database-host mysql \
  136. --database-name ${NC_DBNAME} \
  137. --database-user ${NC_DBUSER} \
  138. --database-pass ${NC_DBPASS} \
  139. --admin-user admin \
  140. --admin-pass ${ADMIN_NC_PASS} \
  141. --data-dir /web/nextcloud/data > /dev/null 2>&1
  142. echo -ne "\r[3/4] Setting custom parameters inside the Nextcloud config file"
  143. echo ""
  144. 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; \
  145. /web/nextcloud/occ --no-warnings config:system:set redis port --value=6379 --type=integer; \
  146. /web/nextcloud/occ --no-warnings config:system:set redis timeout --value=0.0 --type=integer; \
  147. /web/nextcloud/occ --no-warnings config:system:set redis dbindex --value=10 --type=integer; \
  148. /web/nextcloud/occ --no-warnings config:system:set memcache.locking --value='\OC\Memcache\Redis' --type=string; \
  149. /web/nextcloud/occ --no-warnings config:system:set memcache.local --value='\OC\Memcache\Redis' --type=string; \
  150. /web/nextcloud/occ --no-warnings config:system:set trusted_domains 1 --value=${NC_SUBD}; \
  151. /web/nextcloud/occ --no-warnings config:system:set trusted_proxies 0 --value=${IPV6_NETWORK}; \
  152. /web/nextcloud/occ --no-warnings config:system:set trusted_proxies 1 --value=${IPV4_NETWORK}.0/24; \
  153. /web/nextcloud/occ --no-warnings config:system:set overwritehost --value=${NC_SUBD}; \
  154. /web/nextcloud/occ --no-warnings config:system:set overwriteprotocol --value=https; \
  155. /web/nextcloud/occ --no-warnings config:system:set overwritewebroot --value=/; \
  156. /web/nextcloud/occ --no-warnings config:system:set mail_smtpmode --value=smtp; \
  157. /web/nextcloud/occ --no-warnings config:system:set mail_smtpauthtype --value=LOGIN; \
  158. /web/nextcloud/occ --no-warnings config:system:set mail_from_address --value=nextcloud; \
  159. /web/nextcloud/occ --no-warnings config:system:set mail_domain --value=${MAILCOW_HOSTNAME}; \
  160. /web/nextcloud/occ --no-warnings config:system:set mail_smtphost --value=postfix; \
  161. /web/nextcloud/occ --no-warnings config:system:set mail_smtpport --value=588; \
  162. /web/nextcloud/occ --no-warnings config:system:set mail_smtpstreamoptions ssl verify_peer --value=false --type=boolean
  163. /web/nextcloud/occ --no-warnings config:system:set mail_smtpstreamoptions ssl verify_peer_name --value=false --type=boolean
  164. /web/nextcloud/occ --no-warnings db:convert-filecache-bigint -n"
  165. # Not installing by default, broke too often
  166. #/web/nextcloud/occ --no-warnings app:install user_external; \
  167. #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 arguments 0 --value={dovecot:143/imap/tls/novalidate-cert}; \
  168. #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 class --value=OC_User_IMAP; \
  169. echo -e "\r[4/4] Enabling Nginx Configuration"
  170. cp ./data/assets/nextcloud/nextcloud.conf ./data/conf/nginx/
  171. sed -i "s/NC_SUBD/${NC_SUBD}/g" ./data/conf/nginx/nextcloud.conf
  172. sleep 2
  173. echo ""
  174. echo -e "\033[33mFinalizing installation...\033[0m"
  175. docker restart $(docker ps -aqf name=nginx-mailcow)
  176. echo ""
  177. echo "******************************************"
  178. echo "* SAVE THESE CREDENTIALS *"
  179. echo "* INSTALL DATE: $(date +%Y-%m-%d_%H-%M-%S) *"
  180. echo "******************************************"
  181. echo ""
  182. echo -e "\033[36mDatabase name: ${NC_DBNAME}\033[0m"
  183. echo -e "\033[36mDatabase user: ${NC_DBUSER}\033[0m"
  184. echo -e "\033[36mDatabase password: ${NC_DBPASS}\033[0m"
  185. echo ""
  186. echo -e "\033[31mUI admin password: ${ADMIN_NC_PASS}\033[0m"
  187. echo ""
  188. elif [[ ${NC_RESETPW} == "y" ]]; then
  189. 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'
  190. read -r -p "Continue? [y/N] " response
  191. response=${response,,}
  192. if [[ ! "$response" =~ ^(yes|y)$ ]]; then
  193. echo "OK, aborting."
  194. exit 1
  195. fi
  196. NC_USER=
  197. while [[ -z ${NC_USER} ]]; do
  198. read -p "Enter the username: " NC_USER
  199. done
  200. docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) /web/nextcloud/occ user:resetpassword ${NC_USER}
  201. fi