Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 2 | NEXTCLOUD_VER="25" |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 3 | |
| 4 | for bin in curl dirmngr; do |
| 5 | if [[ -z $(which ${bin}) ]]; then echo "Cannot find ${bin}, exiting..."; exit 1; fi |
| 6 | done |
| 7 | |
| 8 | [[ -z ${1} ]] && NC_HELP=y |
| 9 | |
| 10 | while [ "$1" != '' ]; do |
| 11 | case "${1}" in |
| 12 | -p|--purge) NC_PURGE=y && shift;; |
| 13 | -i|--install) NC_INSTALL=y && shift;; |
| 14 | -r|--resetpw) NC_RESETPW=y && shift;; |
| 15 | -h|--help) NC_HELP=y && shift;; |
| 16 | *) echo "Unknown parameter: ${1}" && shift;; |
| 17 | esac |
| 18 | done |
| 19 | |
| 20 | if [[ ${NC_HELP} == "y" ]]; then |
| 21 | printf 'Usage:\n\n' |
| 22 | printf ' -p|--purge\n Purge Nextcloud\n' |
| 23 | printf ' -i|--install\n Install Nextcloud\n' |
| 24 | printf ' -r|--resetpw\n Reset password\n\n' |
| 25 | exit 0 |
| 26 | fi |
| 27 | |
| 28 | [[ ${NC_PURGE} == "y" ]] && [[ ${NC_INSTALL} == "y" ]] && { echo "Cannot use -p and -i at the same time!"; exit 1; } |
| 29 | [[ ${NC_PURGE} == "y" ]] && [[ ${NC_RESETPW} == "y" ]] && { echo "Cannot use -p and -r at the same time!"; exit 1; } |
| 30 | |
| 31 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 32 | cd ${SCRIPT_DIR}/../ |
| 33 | source mailcow.conf |
| 34 | |
| 35 | if [[ ${NC_PURGE} == "y" ]]; then |
| 36 | read -r -p "Are you sure you want to purge Nextcloud? [y/N] " response |
| 37 | response=${response,,} |
| 38 | if [[ ! "$response" =~ ^(yes|y)$ ]]; then |
| 39 | echo "OK, aborting." |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | docker exec -it $(docker ps -f name=mysql-mailcow -q) mysql -uroot -p${DBROOT} -e \ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 44 | "$(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)" |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 45 | docker exec -it $(docker ps -f name=redis-mailcow -q) /bin/sh -c ' cat <<EOF | redis-cli |
| 46 | SELECT 10 |
| 47 | FLUSHDB |
| 48 | EOF |
| 49 | ' |
| 50 | if [ -d ./data/web/nextcloud/config ]; then |
| 51 | mv ./data/web/nextcloud/config/ ./data/conf/nextcloud-config-folder-$(date +%s).bak |
| 52 | fi |
| 53 | [[ -d ./data/web/nextcloud ]] && rm -rf ./data/web/nextcloud |
| 54 | |
| 55 | [[ -f ./data/conf/nginx/site.nextcloud.custom ]] && mv ./data/conf/nginx/site.nextcloud.custom ./data/conf/nginx/site.nextcloud.custom-$(date +%s).bak |
| 56 | [[ -f ./data/conf/nginx/nextcloud.conf ]] && mv ./data/conf/nginx/nextcloud.conf ./data/conf/nginx/nextcloud.conf-$(date +%s).bak |
| 57 | |
| 58 | docker restart $(docker ps -aqf name=nginx-mailcow) |
| 59 | |
| 60 | elif [[ ${NC_UPDATE} == "y" ]]; then |
| 61 | exit; |
| 62 | read -r -p "Are you sure you want to update Nextcloud? [y/N] " response |
| 63 | response=${response,,} |
| 64 | if [[ ! "$response" =~ ^(yes|y)$ ]]; then |
| 65 | echo "OK, aborting." |
| 66 | exit 1 |
| 67 | fi |
| 68 | |
| 69 | if [ ! -f data/web/nextcloud/occ ]; then |
| 70 | echo "Nextcloud occ not found. Is Nextcloud installed?" |
| 71 | exit 1 |
| 72 | fi |
| 73 | if ! 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 |
| 74 | echo "Nextcloud seems not to be installed." |
| 75 | exit 1 |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 76 | elif ! grep -q 'version: 20\.' <<<$(docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) bash -c "/web/nextcloud/occ --no-warnings status"); then |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 77 | echo "Cannot upgrade to new major version, please update manually." |
| 78 | exit 1 |
| 79 | else |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 80 | curl -L# -o nextcloud.tar.bz2 "https://download.nextcloud.com/server/releases/latest-$NEXTCLOUD_VER.tar.bz2" || { echo "Failed to download Nextcloud archive."; exit 1; } \ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 81 | && tar -xjf nextcloud.tar.bz2 -C ./data/web/ \ |
| 82 | && rm nextcloud.tar.bz2 \ |
| 83 | && mkdir -p ./data/web/nextcloud/data \ |
| 84 | && chmod +x ./data/web/nextcloud/occ \ |
| 85 | docker exec -it $(docker ps -f name=php-fpm-mailcow -q) bash -c "chown www-data:www-data -R /web/nextcloud" \ |
| 86 | docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) bash -c "/web/nextcloud/occ --no-warnings upgrade" |
| 87 | fi |
| 88 | |
| 89 | elif [[ ${NC_INSTALL} == "y" ]]; then |
| 90 | NC_SUBD= |
| 91 | while [[ -z ${NC_SUBD} ]]; do |
| 92 | read -p "Subdomain to run Nextcloud from [format: nextcloud.domain.tld]: " NC_SUBD |
| 93 | done |
| 94 | if ! ping -q -c2 ${NC_SUBD} > /dev/null 2>&1 ; then |
| 95 | read -p "Cannot ping subdomain, continue anyway? [y|N] " NC_CONT_FAIL |
| 96 | [[ ! ${NC_CONT_FAIL,,} =~ ^(yes|y)$ ]] && { echo "Ok, exiting..."; exit 1; } |
| 97 | fi |
| 98 | |
| 99 | ADMIN_NC_PASS=$(</dev/urandom tr -dc A-Za-z0-9 | head -c 28) |
| 100 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 101 | curl -L# -o nextcloud.tar.bz2 "https://download.nextcloud.com/server/releases/latest-$NEXTCLOUD_VER.tar.bz2" || { echo "Failed to download Nextcloud archive."; exit 1; } \ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 102 | && tar -xjf nextcloud.tar.bz2 -C ./data/web/ \ |
| 103 | && rm nextcloud.tar.bz2 \ |
| 104 | && mkdir -p ./data/web/nextcloud/data \ |
| 105 | && chmod +x ./data/web/nextcloud/occ |
| 106 | |
| 107 | docker exec -it $(docker ps -f name=php-fpm-mailcow -q) /bin/bash -c "chown -R www-data:www-data /web/nextcloud" |
| 108 | docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) /web/nextcloud/occ --no-warnings maintenance:install \ |
| 109 | --database mysql \ |
| 110 | --database-host mysql \ |
| 111 | --database-name ${DBNAME} \ |
| 112 | --database-user ${DBUSER} \ |
| 113 | --database-pass ${DBPASS} \ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 114 | --admin-user admin \ |
| 115 | --admin-pass ${ADMIN_NC_PASS} \ |
| 116 | --data-dir /web/nextcloud/data |
| 117 | |
| 118 | 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; \ |
| 119 | /web/nextcloud/occ --no-warnings config:system:set redis port --value=6379 --type=integer; \ |
| 120 | /web/nextcloud/occ --no-warnings config:system:set redis timeout --value=0.0 --type=integer; \ |
| 121 | /web/nextcloud/occ --no-warnings config:system:set redis dbindex --value=10 --type=integer; \ |
| 122 | /web/nextcloud/occ --no-warnings config:system:set memcache.locking --value='\OC\Memcache\Redis' --type=string; \ |
| 123 | /web/nextcloud/occ --no-warnings config:system:set memcache.local --value='\OC\Memcache\Redis' --type=string; \ |
| 124 | /web/nextcloud/occ --no-warnings config:system:set trusted_domains 1 --value=${NC_SUBD}; \ |
| 125 | /web/nextcloud/occ --no-warnings config:system:set trusted_proxies 0 --value=${IPV6_NETWORK}; \ |
| 126 | /web/nextcloud/occ --no-warnings config:system:set trusted_proxies 1 --value=${IPV4_NETWORK}.0/24; \ |
| 127 | /web/nextcloud/occ --no-warnings config:system:set overwritehost --value=${NC_SUBD}; \ |
| 128 | /web/nextcloud/occ --no-warnings config:system:set overwriteprotocol --value=https; \ |
| 129 | /web/nextcloud/occ --no-warnings config:system:set overwritewebroot --value=/; \ |
| 130 | /web/nextcloud/occ --no-warnings config:system:set mail_smtpmode --value=smtp; \ |
| 131 | /web/nextcloud/occ --no-warnings config:system:set mail_smtpauthtype --value=LOGIN; \ |
| 132 | /web/nextcloud/occ --no-warnings config:system:set mail_from_address --value=nextcloud; \ |
| 133 | /web/nextcloud/occ --no-warnings config:system:set mail_domain --value=${MAILCOW_HOSTNAME}; \ |
| 134 | /web/nextcloud/occ --no-warnings config:system:set mail_smtphost --value=postfix; \ |
| 135 | /web/nextcloud/occ --no-warnings config:system:set mail_smtpport --value=588; \ |
| 136 | /web/nextcloud/occ --no-warnings db:convert-filecache-bigint -n" |
| 137 | |
| 138 | # Not installing by default, broke too often |
| 139 | #/web/nextcloud/occ --no-warnings app:install user_external; \ |
| 140 | #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 arguments 0 --value={dovecot:143/imap/tls/novalidate-cert}; \ |
| 141 | #/web/nextcloud/occ --no-warnings config:system:set user_backends 0 class --value=OC_User_IMAP; \ |
| 142 | |
| 143 | cp ./data/assets/nextcloud/nextcloud.conf ./data/conf/nginx/ |
| 144 | sed -i "s/NC_SUBD/${NC_SUBD}/g" ./data/conf/nginx/nextcloud.conf |
| 145 | |
| 146 | echo "Restarting Nginx..." |
| 147 | docker restart $(docker ps -aqf name=nginx-mailcow) |
| 148 | |
| 149 | echo "Login as admin with password: ${ADMIN_NC_PASS}" |
| 150 | |
| 151 | elif [[ ${NC_RESETPW} == "y" ]]; then |
| 152 | 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' |
| 153 | read -r -p "Continue? [y/N] " response |
| 154 | response=${response,,} |
| 155 | if [[ ! "$response" =~ ^(yes|y)$ ]]; then |
| 156 | echo "OK, aborting." |
| 157 | exit 1 |
| 158 | fi |
| 159 | |
| 160 | NC_USER= |
| 161 | while [[ -z ${NC_USER} ]]; do |
| 162 | read -p "Enter the username: " NC_USER |
| 163 | done |
| 164 | docker exec -it -u www-data $(docker ps -f name=php-fpm-mailcow -q) /web/nextcloud/occ user:resetpassword ${NC_USER} |
| 165 | |
| 166 | fi |