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