blob: 730e61244a2b19c46d74de9d981468e88c980ef5 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001#!/usr/bin/env bash
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +01002# renovate: datasource=github-releases depName=nextcloud/server versioning=semver extractVersion=^v(?<version>.*)$
3NEXTCLOUD_VERSION=27.1.2
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +01005echo -ne "Checking prerequisites..."
6sleep 1
7for bin in curl dirmngr tar bzip2; do
8 if [[ -z $(which ${bin}) ]]; then echo -ne "\r\033[31mCannot find ${bin}, exiting...\033[0m\n"; exit 1; fi
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01009done
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010010echo -ne "\r\033[32mFound all prerequisites! Continuing...\033[0m\n"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010011
12[[ -z ${1} ]] && NC_HELP=y
13
14while [ "$1" != '' ]; do
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010015 if [[ $# -ne 1 ]]; then
16 echo -e "\033[31mPlease use only one parameter at the same time!\033[0m" >&2
17 exit 2
18 fi
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010019 case "${1}" in
20 -p|--purge) NC_PURGE=y && shift;;
21 -i|--install) NC_INSTALL=y && shift;;
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010022 -u|--update) NC_UPDATE=y && shift;;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010023 -r|--resetpw) NC_RESETPW=y && shift;;
24 -h|--help) NC_HELP=y && shift;;
25 *) echo "Unknown parameter: ${1}" && shift;;
26 esac
27done
28
29if [[ ${NC_HELP} == "y" ]]; then
30 printf 'Usage:\n\n'
31 printf ' -p|--purge\n Purge Nextcloud\n'
32 printf ' -i|--install\n Install Nextcloud\n'
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010033 printf ' -u|--update\n Update Nextcloud\n'
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010034 printf ' -r|--resetpw\n Reset password\n\n'
35 exit 0
36fi
37
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010038SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
39cd ${SCRIPT_DIR}/../
40source mailcow.conf
41
42if [[ ${NC_PURGE} == "y" ]]; then
43 read -r -p "Are you sure you want to purge Nextcloud? [y/N] " response
44 response=${response,,}
45 if [[ ! "$response" =~ ^(yes|y)$ ]]; then
46 echo "OK, aborting."
47 exit 1
48 fi
49
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010050 echo -e "\033[33mDetecting Database information...\033[0m"
51 if [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "Show databases" | grep "nextcloud") ]]; then
52 echo -e "\033[32mFound seperate Nextcloud database (newer scheme)!\033[0m"
53 echo -e "\033[31mPurging...\033[0m"
54 docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "DROP DATABASE nextcloud;" > /dev/null
55 docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "DROP USER 'nextcloud'@'%';" > /dev/null
56 elif [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} mailcow -e "SHOW TABLES LIKE 'oc_%'") && $? -eq 0 ]]; then
57 echo -e "\033[32mFound Nextcloud (oc) tables inside of mailcow database (old scheme)!\033[0m"
58 echo -e "\033[31mPurging...\033[0m"
59 docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \
60 "$(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
61 elif [[ $(docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} mailcow -e "SHOW TABLES LIKE 'nc_%'") && $? -eq 0 ]]; then
62 echo -e "\033[32mFound Nextcloud (nc) tables inside of mailcow database (old scheme)!\033[0m"
63 echo -e "\033[31mPurging...\033[0m"
64 docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \
65 "$(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
66 else
67 echo -e "\033[31mError: No Nextcloud databases/tables found!"
68 echo -e "\033[33mNot purging anything...\033[0m"
69 exit 1
70 fi
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010071 docker exec -it $(docker ps -f name=redis-mailcow -q) /bin/sh -c ' cat <<EOF | redis-cli
72SELECT 10
73FLUSHDB
74EOF
75'
76 if [ -d ./data/web/nextcloud/config ]; then
77 mv ./data/web/nextcloud/config/ ./data/conf/nextcloud-config-folder-$(date +%s).bak
78 fi
79 [[ -d ./data/web/nextcloud ]] && rm -rf ./data/web/nextcloud
80
81 [[ -f ./data/conf/nginx/site.nextcloud.custom ]] && mv ./data/conf/nginx/site.nextcloud.custom ./data/conf/nginx/site.nextcloud.custom-$(date +%s).bak
82 [[ -f ./data/conf/nginx/nextcloud.conf ]] && mv ./data/conf/nginx/nextcloud.conf ./data/conf/nginx/nextcloud.conf-$(date +%s).bak
83
84 docker restart $(docker ps -aqf name=nginx-mailcow)
85
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010086 echo -e "\033[32mNextcloud has been uninstalled sucessfully!\033[0m"
87
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010088elif [[ ${NC_UPDATE} == "y" ]]; then
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010089 read -r -p "Are you sure you want to update Nextcloud (with Nextclouds own updater)? [y/N] " response
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010090 response=${response,,}
91 if [[ ! "$response" =~ ^(yes|y)$ ]]; then
92 echo "OK, aborting."
93 exit 1
94 fi
95
96 if [ ! -f data/web/nextcloud/occ ]; then
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010097 echo -e "\033[31mError: Nextcloud occ not found. Is Nextcloud installed?\033[0m"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010098 exit 1
99 fi
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100100 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
101 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"
102 wget -q https://raw.githubusercontent.com/nextcloud/server/v26.0.0/lib/versioncheck.php -O ./data/web/nextcloud/lib/versioncheck.php
103 echo -e "\e[33mPlease restart the update again.\e[0m"
104 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
105 echo -e "\033[31mError: Nextcloud seems not to be installed.\033[0m"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100106 exit 1
107 else
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100108 docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) bash -c "php /web/nextcloud/updater/updater.phar"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100109 fi
110
111elif [[ ${NC_INSTALL} == "y" ]]; then
112 NC_SUBD=
113 while [[ -z ${NC_SUBD} ]]; do
114 read -p "Subdomain to run Nextcloud from [format: nextcloud.domain.tld]: " NC_SUBD
115 done
116 if ! ping -q -c2 ${NC_SUBD} > /dev/null 2>&1 ; then
117 read -p "Cannot ping subdomain, continue anyway? [y|N] " NC_CONT_FAIL
118 [[ ! ${NC_CONT_FAIL,,} =~ ^(yes|y)$ ]] && { echo "Ok, exiting..."; exit 1; }
119 fi
120
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100121 echo -e "\033[33mDownloading \033[34mNextcloud ${NEXTCLOUD_VERSION}\033[33m...\033[0m"
122 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; } \
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100123 && tar -xjf nextcloud.tar.bz2 -C ./data/web/ \
124 && rm nextcloud.tar.bz2 \
125 && mkdir -p ./data/web/nextcloud/data \
126 && chmod +x ./data/web/nextcloud/occ
127
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100128 echo -e "\033[33mCreating 'nextcloud' database...\033[0m"
129 NC_DBPASS=$(</dev/urandom tr -dc A-Za-z0-9 2> /dev/null | head -c 28)
130 NC_DBUSER=nextcloud
131 NC_DBNAME=nextcloud
132
133 echo -ne "[1/3] Creating 'nextcloud' database"
134 docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "CREATE DATABASE ${NC_DBNAME};"
135 sleep 2
136 echo -ne "\r[2/3] Creating 'nextcloud' database user"
137 docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "CREATE USER '${NC_DBUSER}'@'%' IDENTIFIED BY '${NC_DBPASS}';"
138 sleep 2
139 echo -ne "\r[3/3] Granting 'nextcloud' user all permissions on database 'nextcloud'"
140 docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e "GRANT ALL PRIVILEGES ON ${NC_DBNAME}.* TO '${NC_DBUSER}'@'%';"
141 sleep 2
142
143 echo ""
144 echo -e "\033[33mInstalling Nextcloud...\033[0m"
145 ADMIN_NC_PASS=$(</dev/urandom tr -dc A-Za-z0-9 2> /dev/null | head -c 28)
146
147 echo -ne "[1/4] Setting correct permissions for www-data"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100148 docker exec -it $(docker ps -f name=php-fpm-mailcow -q) /bin/bash -c "chown -R www-data:www-data /web/nextcloud"
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100149 sleep 2
150 echo -ne "\r[2/4] Running occ maintenance:install to install Nextcloud"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100151 docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) /web/nextcloud/occ --no-warnings maintenance:install \
152 --database mysql \
153 --database-host mysql \
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100154 --database-name ${NC_DBNAME} \
155 --database-user ${NC_DBUSER} \
156 --database-pass ${NC_DBPASS} \
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100157 --admin-user admin \
158 --admin-pass ${ADMIN_NC_PASS} \
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100159 --data-dir /web/nextcloud/data > /dev/null 2>&1
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100160
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100161 echo -ne "\r[3/4] Setting custom parameters inside the Nextcloud config file"
162 echo ""
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100163 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; \
164 /web/nextcloud/occ --no-warnings config:system:set redis port --value=6379 --type=integer; \
165 /web/nextcloud/occ --no-warnings config:system:set redis timeout --value=0.0 --type=integer; \
166 /web/nextcloud/occ --no-warnings config:system:set redis dbindex --value=10 --type=integer; \
167 /web/nextcloud/occ --no-warnings config:system:set memcache.locking --value='\OC\Memcache\Redis' --type=string; \
168 /web/nextcloud/occ --no-warnings config:system:set memcache.local --value='\OC\Memcache\Redis' --type=string; \
169 /web/nextcloud/occ --no-warnings config:system:set trusted_domains 1 --value=${NC_SUBD}; \
170 /web/nextcloud/occ --no-warnings config:system:set trusted_proxies 0 --value=${IPV6_NETWORK}; \
171 /web/nextcloud/occ --no-warnings config:system:set trusted_proxies 1 --value=${IPV4_NETWORK}.0/24; \
172 /web/nextcloud/occ --no-warnings config:system:set overwritehost --value=${NC_SUBD}; \
173 /web/nextcloud/occ --no-warnings config:system:set overwriteprotocol --value=https; \
174 /web/nextcloud/occ --no-warnings config:system:set overwritewebroot --value=/; \
175 /web/nextcloud/occ --no-warnings config:system:set mail_smtpmode --value=smtp; \
176 /web/nextcloud/occ --no-warnings config:system:set mail_smtpauthtype --value=LOGIN; \
177 /web/nextcloud/occ --no-warnings config:system:set mail_from_address --value=nextcloud; \
178 /web/nextcloud/occ --no-warnings config:system:set mail_domain --value=${MAILCOW_HOSTNAME}; \
179 /web/nextcloud/occ --no-warnings config:system:set mail_smtphost --value=postfix; \
180 /web/nextcloud/occ --no-warnings config:system:set mail_smtpport --value=588; \
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100181 /web/nextcloud/occ --no-warnings config:system:set mail_smtpstreamoptions ssl verify_peer --value=false --type=boolean
182 /web/nextcloud/occ --no-warnings config:system:set mail_smtpstreamoptions ssl verify_peer_name --value=false --type=boolean
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100183 /web/nextcloud/occ --no-warnings db:convert-filecache-bigint -n"
184
185 # Not installing by default, broke too often
186 #/web/nextcloud/occ --no-warnings app:install user_external; \
187 #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 arguments 0 --value={dovecot:143/imap/tls/novalidate-cert}; \
188 #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 class --value=OC_User_IMAP; \
189
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100190 echo -e "\r[4/4] Enabling Nginx Configuration"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100191 cp ./data/assets/nextcloud/nextcloud.conf ./data/conf/nginx/
192 sed -i "s/NC_SUBD/${NC_SUBD}/g" ./data/conf/nginx/nextcloud.conf
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100193 sleep 2
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100194
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100195 echo ""
196 echo -e "\033[33mFinalizing installation...\033[0m"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100197 docker restart $(docker ps -aqf name=nginx-mailcow)
198
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +0100199 echo ""
200 echo "******************************************"
201 echo "* SAVE THESE CREDENTIALS *"
202 echo "* INSTALL DATE: $(date +%Y-%m-%d_%H-%M-%S) *"
203 echo "******************************************"
204 echo ""
205 echo -e "\033[36mDatabase name: ${NC_DBNAME}\033[0m"
206 echo -e "\033[36mDatabase user: ${NC_DBUSER}\033[0m"
207 echo -e "\033[36mDatabase password: ${NC_DBPASS}\033[0m"
208 echo ""
209 echo -e "\033[31mUI admin password: ${ADMIN_NC_PASS}\033[0m"
210 echo ""
211
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100212
213elif [[ ${NC_RESETPW} == "y" ]]; then
214 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'
215 read -r -p "Continue? [y/N] " response
216 response=${response,,}
217 if [[ ! "$response" =~ ^(yes|y)$ ]]; then
218 echo "OK, aborting."
219 exit 1
220 fi
221
222 NC_USER=
223 while [[ -z ${NC_USER} ]]; do
224 read -p "Enter the username: " NC_USER
225 done
226 docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) /web/nextcloud/occ user:resetpassword ${NC_USER}
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100227fi