#!/bin/bash ## author : Dryusdan ## date : 30/09/2019 ## description : A Nextcloud Update ## usage : ./nextcloud.sh ## Bash strict mode #################################### set -o errexit # abort on nonzero exitstatus set -o nounset # abort on unbound variable set -o pipefail # don't hide errors within pipes ## Bash color ########################################## # Set colors RED='\033[0;31m' GREEN='\033[00;32m' YELLOW='\033[00;33m' BLUE='\033[00;34m' PURPLE='\033[00;35m' CYAN='\033[00;36m' LIGHTGRAY='\033[00;37m' LRED='\033[01;31m' LGREEN='\033[01;32m' LYELLOW='\033[01;33m' LBLUE='\033[01;34m' LPURPLE='\033[01;35m' LCYAN='\033[01;36m' WHITE='\033[01;37m' NC='\033[0m' # No Color ## Logs ################################################ readonly SCRIPTNAME="$(basename "$0")" info() { echo -e "${LBLUE}[INFO] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; } warning() { echo -e "${YELLOW}[WARNING] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; } error() { echo -e "${LRED}[ERROR] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; } fatal() { echo -e "${RED}[FATAL] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; exit 1 ; } ######################################################## ## Define nextcloud's folder ########################## FOLDER="/var/www/nextcloud/" ## Ask nextcloud ###################################### read -p "Nextcloud version ? " VERSION info "Check if require folders exist" if [ -d "${FOLDER}/nextcloud" ]; then warning "Aborded installation exist. Remove nextcloud/ folder" rm -rf ${FOLDER}/nextcloud || fatal "Error when removing folder. Exiting" rm "${FOLDER}"/nextcloud-* || fatal "Error when removing nextcloud-* .zip, .zip.sha256, .zip.asc. Please remove $FOLDER/nextcloud and $FOLDER/nextcloud-* and retry" fi if [ -d "${FOLDER}/www" ]; then info "www/ directory exist, continue" NEW_INSTALLATION=false else warning "www/ directory don't exist, creating" mkdir "${FOLDER}/www" || fatal "Error when creating folder. Exiting" NEW_INSTALLATION=true fi if [ -d "${FOLDER}/data" ]; then info "data/ directory exist, continue" else warning "data/ directory don't exist, creating" mkdir ${FOLDER}/data || fatal "Error when creating folder. Exiting" NEW_INSTALLATION=true fi if [ -d "${FOLDER}/config" ]; then info "config/ directory exist, continue" else warning "config/ directory don't exist, creating" mkdir ${FOLDER}/config || fatal "Error when creating folder. Exiting" NEW_INSTALLATION=true fi if [ -d "${FOLDER}/apps" ]; then info "apps/ directory exist, continue" else warning "apps/ directory don't exist, creating" mkdir ${FOLDER}/apps || fatal "Error when creating folder. Exiting" NEW_INSTALLATION=true fi # Download nextcloud info "Download Nextcloud ${VERSION}" wget -q https://download.nextcloud.com/server/releases/nextcloud-${VERSION}.zip || fatal "Download failed. Please retry" if [ ! -f "${FOLDER}/nextcloud-${VERSION}.zip" ]; then fatal "Download of Nextcloud archive failed. Please retry" fi info "Download sha256 of Nextcloud" wget -q https://download.nextcloud.com/server/releases/nextcloud-$VERSION.zip.sha256 || fatal "Download of Nextcloud checksum failed. Please retry" if [ ! -f "$FOLDER/nextcloud-$VERSION.zip.sha256" ]; then fatal "Download of Nextcloud checksum failed. Please retry" fi info "Download asc of Nextcloud" wget -q https://download.nextcloud.com/server/releases/nextcloud-${VERSION}.zip.asc || fatal "Download of Nextcloud signature failed. Please retry" if [ ! -f "${FOLDER}/nextcloud-${VERSION}.zip.asc" ]; then fatal "Download of Nextcloud signature failed. Please retry" fi # Check integrity nextcloud info "Check nextcloud integrity" sha256sum -c nextcloud-${VERSION}.zip.sha256 >/dev/null || fatal "Checksum did not match" gpg -q --verify nextcloud-${VERSION}.zip.asc 2>/dev/null|| fatal "Signature is not good" info "Start update" info "Unzip Nextcloud ${VERSION}" unzip -q nextcloud-${VERSION}.zip if [[ ${NEW_INSTALLATION} == "false" ]]; then info "Unlink folders that contain the data" cp ${FOLDER}/config/config.php ${FOLDER}/config.php unlink www/data unlink www/config unlink www/apps info "Backuping old installation" tar -zcf "www.$(date '+%Y-%m-%d').tar.gz" www tar -zcf "apps.$(date '+%Y-%m-%d').tar.gz" apps info "Removing old installation" rm -rf "${FOLDER}"/www/* info "Copying news apps files" fi info "Copying new Nextcloud" cp -rf "${FOLDER}"/nextcloud/apps/* apps/ rm -rf "${FOLDER}"/nextcloud/{config,data,apps} cp -rf "${FOLDER}"/nextcloud/* www/ cp -rf ${FOLDER}/config.php ${FOLDER}/config/config.php info "Link folders that contain the data into new installation" ln -s ${FOLDER}/data ${FOLDER}/www/ ln -s ${FOLDER}/config ${FOLDER}/www/ ln -s ${FOLDER}/apps ${FOLDER}/www/ info "Removing archive and temp folder" rm -rf ${FOLDER}/nextcloud-${VERSION}.zip* ${FOLRDER}/nextcloud/ info "Setting good permission" cd www/ find -type f -exec chmod 644 {} \; 2> /dev/null; find -type d -exec chmod 755 {} \; 2> /dev/null; info "Launch upgrade with occ" php occ upgrade info "Launch integrity check" php occ check read -n1 -p "Would you like scann all file (recommanded) [y,n]" SCAN case ${SCAN} in y|Y) info "Launch files scan" && php console.php files:scan --all ;; n|N) info "Don't launch files scan" ;; *) echo Please use y or n ;; esac ########################################################### info "exiting ${SCRIPTNAME}" exit 0