Skip to content

Instantly share code, notes, and snippets.

@noerw
Created September 9, 2016 19:42
Show Gist options
  • Save noerw/e4f5e71926514dee6830b262ce9fba25 to your computer and use it in GitHub Desktop.
Save noerw/e4f5e71926514dee6830b262ce9fba25 to your computer and use it in GitHub Desktop.
automated owncloud upgrade (mysql)
#!/bin/bash
# create a backup of owncloud & upgrade to the latest version
# requires root. depends on mysqldump, wget, tar
# should run on any linux machine
#
# - disables all apps
# - performs a mysqldump
# - backups the current installation
# - gets the new archive & installs it
# - fixes file permissions
# - performs DB schema upgrade
#
# license: GPL-3.0
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
# directories
OC_PATH="/srv/owncloud"
OC_BACKUP_PATH="/srv/owncloud_backup_${TIMESTAMP}"
# filename of the release on https://download.owncloud.org/community/{...}
OC_RELEASE="owncloud-8.2.5.tar.bz2"
# mysql config
MYSQL_DB="owncloud"
MYSQL_CREDENTIALS="--user=XXXXXXXX --password=XXXXXXXXXXXXXXXXXX"
# 3rdparty apps which have to be disabled before the upgrade & re-enabled
ENABLED_APPS=( calendar galleryplus tasks )
ROOT_USER="root"
HTTP_USER="www-data"
HTTP_GROUP="www-data"
#################
OCC="sudo -u ${HTTP_USER} php $OC_PATH/occ"
# check if script is run as root
if [ "$(id -u)" != "0" ]; then
echo "Please run the upgrade script as root."
exit 1
fi
# exit the script when an error occurs
trap "echo an error occured, stopping upgrade: " ERR
set -e
set -o pipefail
# turn on maintenance & disable 3rdparty apps
$OCC maintenance:mode --on
for i in ${ENABLED_APPS[@]}; do
$OCC app:disable $i
done
# back up installation files
mv $OC_PATH $OC_BACKUP_PATH/
mkdir -p $OC_PATH/config
mkdir -p $OC_PATH/data
# back up database
mysqldump $MYSQL_DB $MYSQL_CREDENTIALS > $OC_BACKUP_PATH/owncloud_backup_$TIMESTAMP.sql
# get the new version & unpack
wget https://download.owncloud.org/community/$OC_RELEASE
tar -xf $OC_RELEASE --strip 1 -C $OC_PATH
# apply the old config & data dir
cp $OC_BACKUP_PATH/config/config.php $OC_PATH/config/
mv $OC_BACKUP_PATH/data $OC_PATH/
# set directory permissions (see https://doc.owncloud.org/server/8.2/go.php?to=admin-dir_permissions)
sudo chown -R www-data:www-data $OC_PATH;
printf "chmod Files and Directories\n"
find ${OC_PATH}/ -type f -print0 | xargs -0 chmod 0640
find ${OC_PATH}/data/ -type f -print0 | xargs -0 chmod 0640
find ${OC_PATH}/ -type d -print0 | xargs -0 chmod 0750
find ${OC_PATH}/data/ -type d -print0 | xargs -0 chmod 0750
printf "chown Directories\n"
chown -R ${ROOT_USER}:${HTTP_GROUP} ${OC_PATH}/
chown -R ${HTTP_USER}:${HTTP_GROUP} ${OC_PATH}/apps/
chown -R ${HTTP_USER}:${HTTP_GROUP} ${OC_PATH}/config/
chown -R ${HTTP_USER}:${HTTP_GROUP} ${OC_PATH}/data/
chown -R ${HTTP_USER}:${HTTP_GROUP} ${OC_PATH}/themes/
chown -R ${HTTP_USER}:${HTTP_GROUP} ${OC_PATH}/assets/
chmod +x ${OC_PATH}/occ
printf "chmod/chown .htaccess\n"
if [ -f ${OC_PATH}/.htaccess ]
then
chmod 0644 ${OC_PATH}/.htaccess
chown ${rootuser}:${HTTP_GROUP} ${OC_PATH}/.htaccess
fi
if [ -f ${OC_PATH}/data/.htaccess ]
then
chmod 0644 ${OC_PATH}/data/.htaccess
chown ${rootuser}:${HTTP_GROUP} ${OC_PATH}/data/.htaccess
fi
# perform db upgrade, enable apps, then disable maintenance mode
$OCC upgrade
for i in ${ENABLED_APPS[@]}; do
$OCC app:enable $i
done
$OCC upgrade # upgrade the just enabled apps
$OCC maintenance:mode --off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment