Owncloud upgrade when data directory is not in standard htdocs folder


When I installed owncloud on our Debian Jessie server I didn’t like the way the data directory was under the htdocs path, so I did a custom install and created an owncloud-specific apache config file (owncloud.conf) in the apache sites-available directory (note we are running Apache 2.4) like this:

<VirtualHost *:443>
        ServerName myowcloud.mydomain.net

        DocumentRoot /var/mysites/owncloud/htdocs

        <Directory "/var/mysites/owncloud/htdocs">
                Options +FollowSymLinks
                AllowOverride All
        </Directory>

        SSLEngine On
        SSLCertificateFile /path/to/mydomain.cer
        SSLCertificateChainFile /path/to/optional/rapidsslca.cer
        SSLCertificateKeyFile /path/to/mydomain.key.2014

        ErrorLog ${APACHE_LOG_DIR}/error-mydomain-ssl.log

        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access-mydomain-ssl.log combined
</VirtualHost>

Then you can use a2ensite owncloud to enable the site and apache2ctl graceful to restart apache with the new server fired up. I recommend apache2ctl configtest first. And apache2ctl -S tells you if the new config file has been used successfully.

The problem is that this breaks apt-get update owncloud which tries to install in the standard directory – and ends up just installing a fresh copy of owncloud and breaking Apache.

I would recommend you back up your data files first.

Below is the main shell script (which I stress I accept NO warranty for, etc!) that I wrote to automate this. You will need to have some unix skills to adapt this for your own solution, but here it is.

#!/bin/bash

clear
echo "This script will upgrade owncloud if you have it installed in a non-standard directory"
echo "This assumes the data directory is outside of the install folder"


HTDOCS_DIRECTORY="/var/sites/owncloud/htdocs"
HTDOCS_BACKUP_ROOT_DIRECTORY="/var/sites/owncloud"
HTDOCS_BACKUP_DIRECTORY="$HTDOCS_BACKUP_ROOT_DIRECTORY/htdocs-old"
DOWNLOAD_URL="https://download.owncloud.org/community/owncloud-8.1.1.tar.bz2"
TMPDIR="/var/sites/owncloud-new"
TMPFILENAME="owncloud.tar.bz2"
APACHE_SITE_CONFIG_FILENAME="owncloud.conf"
APACHE_PATH="/etc/apache2"
MYSQL_USER="owncloud"
MYSQL_DATABASE="owncloud"
MYSQL_BACKUP_FILENAME=/tmp/owncloud-sqlbkp_`date +"%Y%m%d"`.sql

CheckRunningAsRoot() {
  if [ "$(id -u)" != "0" ]; then
      echo "Error: This script must be run as root"
      exit 2
  fi
}

CheckPathExists() {
  if [ ! -d "$1" ]; then
      echo "Error: Directory $1 does not exist."
      exit 2
  fi
}

CheckPathDoesNotExist() {
  if [ -d "$1" ]; then
      echo "Error: Directory $1 already exists."
      exit 2
  fi
}

CheckFileExists() {
  if [ ! -f "$1" ]; then
      echo "Error: File $1 does not exist."
      exit 2
  fi  
}

CheckFileDoesNotExist() {
  if [ -f "$1" ]; then
	echo "Error: File $1 already exists."
	exit 2
  fi
}

CheckRemoteFileExists() {
  if curl --output /dev/null --silent --head --fail "${1}"
  then
      echo "URL ${1} exists"
  else
      echo "Error: URL ${1} does not exist"
      exit 2
  fi  
}

CheckDataFileNotInSubfolder() {
  if [ $(grep -c "'datadirectory'\s*=>\s*'" $1 ) -ne 0 ]
  then
    if [ $(grep -c "'datadirectory'\s*=>\s*'$2" $1 ) -ne 0 ]
    then
      echo "Error: it looks like the datadirectory file path is inside $2"
      exit 2
    fi
  else
    echo "Error: could not find datadirectory directive in $1"
    exit 2
  fi
}

CallOcc() {
	echo "Calling occ with $@"
	sudo -u www-data php ${HTDOCS_DIRECTORY}/occ $@
}

CheckRunningAsRoot
CheckPathExists $HTDOCS_DIRECTORY
CheckPathExists $HTDOCS_DIRECTORY/config
CheckFileExists $HTDOCS_DIRECTORY/occ
CheckPathExists $HTDOCS_BACKUP_ROOT_DIRECTORY
CheckPathDoesNotExist $HTDOCS_BACKUP_DIRECTORY
CheckRemoteFileExists $DOWNLOAD_URL
CheckFileExists $APACHE_PATH/sites-enabled/$APACHE_SITE_CONFIG_FILENAME
CheckFileDoesNotExist $MYSQL_BACKUP_FILENAME

echo "Checking data file is not in subfolder of $HTDOCS_DIRECTORY"
CONFIG_FILE="$HTDOCS_DIRECTORY/config/config.php"
CheckFileExists $CONFIG_FILE
CheckDataFileNotInSubfolder $CONFIG_FILE $HTDOCS_DIRECTORY

echo ""
read -s -p "Enter mysql password for user $MYSQL_USER: " MYSQL_PASSWORD
echo ""
echo "Checking user $MYSQL_USER access to the $MYSQL_DATABASE database"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD --database=$MYSQL_DATABASE -e "SELECT COUNT(*) FROM oc_users"
if [ $? -ne 0 ]; then
	echo "Error: MySQL problem"
	exit 1
fi

echo ""
echo "Removing any existing temp file and downloading the installation"
rm -r $TMPDIR/$TMPFILENAME
rm -r $TMPDIR/owncloud
wget -O $TMPDIR/$TMPFILENAME $DOWNLOAD_URL
tar xjf $TMPDIR/$TMPFILENAME -C $TMPDIR
CheckPathExists $TMPDIR/owncloud

echo ""
echo "Putting Owncloud into maintenance mode"
CallOcc maintenance:mode --on

echo ""
echo "Backing up database"
mysqldump --lock-tables -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE > $MYSQL_BACKUP_FILENAME

echo ""
echo "Taking down the Owncloud site"
a2dissite $APACHE_SITE_CONFIG_FILENAME
apache2ctl graceful

echo ""
echo "Backing up the existing htdocs directory and copying over the new scripts"
mv $HTDOCS_DIRECTORY $HTDOCS_BACKUP_DIRECTORY
cp -rp $TMPDIR/owncloud $HTDOCS_DIRECTORY

echo ""
echo "Setting permissions on folders and reinstating the original config.php"
chown -R root:root $HTDOCS_DIRECTORY
chown -R www-data:www-data $HTDOCS_DIRECTORY/apps
chown -R www-data:www-data $HTDOCS_DIRECTORY/config
cp -p $HTDOCS_BACKUP_DIRECTORY/config/config.php $HTDOCS_DIRECTORY/config/config.php

echo ""
echo "Running the upgrade script"
CallOcc upgrade

echo ""
echo "Ending maintenance mode and booting up the Owncloud site"
CallOcc maintenance:mode --off
a2ensite $APACHE_SITE_CONFIG_FILENAME
apache2ctl graceful
Advertisements

About saasmd

I am an experienced software-as-a-service entrepreneur, based in London, UK. I love building interesting software businesses. My current venture is StorIQ, a platform to help bricks-and-mortar retailers manage their operations more effectively. This blog is a space to share low-level techie stuff that I think other people will find useful.
This entry was posted in Techy Stuff and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s