Prerequisites:

  • Ubuntu
  • MYSQL database
  • Apache

Set your variables:

nc_path='/var/www/nextcloud'
nc_old="${nc_path}-old"
backup_root='/somewhere/backups'
htuser='www-data'
db_name='your-nextcloud-database-name'

date=`date +%F`
version=`grep VersionString ${nc_path}/version.php | awk -F\' '{print $2}'`
backup_path="${backup_root}/nc_${version}_${date}"
db_backup="${backup_root}/nc_${version}_${date}.sql"

Enable maintenance mode:

# put server in maintenance mode
cd ${nc_path}
sudo -u ${htuser} php occ maintenance:mode --on

Verify the current version:

# version
grep VersionString ${nc_path}/version.php | awk -F\' '{print $2}'

Make backups:

# backup nextcloud server files
mkdir -pv ${backup_path}
cp -prv ${nc_path}/* ${backup_path}

# backup nextcloud database
mysqldump -u root -p ${db_name} > ${db_backup}
gzip ${db_backup}

If your data folder is outside of your /nextcloud directory, backup your data files separately. I prefer using rsync-time-backup which provides a wrapper around rsync.

rsync_tmbackup.sh /source/data /destination/backup

Stop the web server:

# stop web server
service apache2 stop

Download the latest release:

You can use this PHP script to easily get the URL to the latest release and automatically download the archive. Name this PHP script get-update-url.php.

#!/usr/bin/php
<?php
        include("nextcloud/version.php");

        $updaterUrl = 'https://updates.nextcloud.com/updater_server/';

        $version = $OC_Version;
        $version['installed'] = '';
        $version['updated'] = '';
        $version['updatechannel'] = $OC_Channel;
        $version['edition'] = '';
        $version['build'] = '';
        $version['php_major'] = PHP_MAJOR_VERSION;
        $version['php_minor'] = PHP_MINOR_VERSION;
        $version['php_release'] = PHP_RELEASE_VERSION;
        $versionString = implode('x', $version);

        //fetch xml data from updater
        $url = $updaterUrl . '?version=' . $versionString;
        echo $url;

        # Example update url:
        # https://updates.nextcloud.com/updater_server/?version=18x0x1x3xxxstablexxx7x4x3
?>

See the source of versionCheck.php to determine the correct format of the update URL.

Change to the directory where Nextcloud is installed:

cd $nc_path; cd ..

Download the latest release using the get-update-url.php script:

wget `php -f get-update-url.php | xargs curl 2> /dev/null | grep url | awk -F "[<>]" '{print $3}'`

Move the current installation:

mv ${nc_path} ${nc_old}

Unpack Nextcloud archive:

unzip nextcloud-*.zip

Restore the configuration file:

cp -pv ${backup_path}/config/config.php ${nc_path}/config/.

Set the permissions and owner:

#!/bin/bash
nc_path='/var/www/nextcloud'
data_path='/somewhere/data'  # if located in nextcloud /var/www/nextcloud/data
htuser='www-data'

find ${nc_path}/ -type f -print0 | xargs -0 chmod 0640
find ${nc_path}/ -type d -print0 | xargs -0 chmod 0750

chown -R root:${htuser} ${nc_path}/
chown -R ${htuser}:${htuser} ${nc_path}/apps/
chown -R ${htuser}:${htuser} ${nc_path}/config/
chown -R ${htuser}:${htuser} ${data_path}
chown -R ${htuser}:${htuser} ${nc_path}/themes/

chown root:${htuser} ${nc_path}/.htaccess
chown root:${htuser} ${data_path}/.htaccess

chmod 0644 ${nc_path}/.htaccess
chmod 0644 ${data_path}/.htaccess

Find the permissions.sh script here.

Restart the web server:

service apache2 start

Perform the upgrade:

cd ${nc_path}
sudo -u ${htuser} php occ upgrade

Disable maintenance mode:

sudo -u ${htuser} php occ maintenance:mode --off

Check the installation:

Check the installed Apps:

sudo -u ${htuser} php occ app:list

Check the two factor state of a user:

sudo -u ${htuser} php occ twofactorauth:state <username>

On one occasion, I’ve had to reinstall twofactor_totp:

sudo -u ${htuser} php occ app:install twofactor_totp