Skip to content

Instantly share code, notes, and snippets.

@WolfgangFahl
Last active May 16, 2021 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WolfgangFahl/372af33777e17abc869f2b111dc2d8ae to your computer and use it in GitHub Desktop.
Save WolfgangFahl/372af33777e17abc869f2b111dc2d8ae to your computer and use it in GitHub Desktop.
Nextcloud upgrade script
#!/bin/bash
# WF 2017-12-28
# automates upgrade procedure for nextcloud
#
# see
# http://wiki.bitplan.com/index.php/Nextcloud
# for details
#
# see
# https://intux.de/2016/08/upgrade-nextcloud-9-0-50-10-0-0/
# for the manual procedure
# adapt to the settings of your server
download="/usr/local/src/nextcloud"
instroot="/usr/share"
ncroot="$instroot/nextcloud"
ncconfig="$ncroot/config/config.php"
ncowner="www-data.www-data"
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show an error message and exit
#
# params:
# 1: l_msg - the message to display
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error: $l_msg" 1>&2
exit 1
}
#
# download the given version
#
dodownload() {
cd $download
local l_version="$1"
local l_target=latest-$l_version.zip
if [ -f $l_target ]
then
color_msg $green "nextcloud version $l_version file $l_target already downloaded"
else
color_msg $blue "downloading nextcloud version $l_version"
wget https://download.nextcloud.com/server/releases/$l_target
fi
}
#
# do upgrade to the next version
# without further ado / asking
#
doupgrade() {
local l_current="$1"
local l_next="$2"
dodownload $l_current
dodownload $l_next
if [ ! -d $ncroot.$l_current ]
then
mv $ncroot $ncroot.$l_current
cd $instroot
unzip $download/latest-$l_next.zip
fi
# check whether config file has been copied already
if [ ! -f "$ncconfig" ]
then
color_msg $blue "copying config.php from old version $l_current to new version $l_next"
cp -p $ncroot.$l_current/config/config.php $ncconfig
fi
# check whether data directory has been copied already
if [ ! -d "$ncroot/data" ]
then
color_msg $blue "copying data from old version $l_current to new version $l_next"
cp -pr $ncroot.$l_current/data $ncroot/data/
fi
chown -R $ncowner $ncroot
}
#
# upgrade to the next version
# after confirmation
#
upgrade() {
local l_current="$1"
local l_next="$((l_current+1))"
color_msg $blue "Would you like to upgrade nextcloud from version $l_current to $l_next (yes/no)?"
read answer
case $answer in
y|yes)
doupgrade $l_current $l_next
;;
*) error "upgrade aborted on user request"
esac
}
#
# check the next cloud version
# and auto upgrade to the next major release
#
autoupgrade() {
color_msg $blue "checking nextcloud version"
if [ -d "$ncroot" ]
then
color_msg $green "found nextcloud installation in $ncroot"
if [ -f "$ncconfig" ]
then
color_msg $green "found nextcloud config file at $ncconfig"
version=$(getVersion)
major=$(getMajorVersion)
color_msg $green "nextcloud version $major => $version is installed"
upgrade $major
else
error "missing nextcloud config file $l_ncconfig"
fi
else
error "could not find nextcloud installation in $ncroot"
fi
}
#
# get the version
#
occVersion() {
sudo -u www-data php $ncroot/occ --version
}
#
# get the nextcloud version
#
getVersion() {
occVersion | cut -f3 -d" "
}
#
# get the major version
#
getMajorVersion() {
getVersion | cut -f1 -d"."
}
#
# automatic major version upgrade
# comment out if you would like to control the upgrade manually
autoupgrade
#doupgrade 9 10
#doupgrade 10 11
#doupgrade 11 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment