nextcloud.sh 13 KB

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