Skip to content

Instantly share code, notes, and snippets.

@apio-sys
Last active December 13, 2023 10:18
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 apio-sys/7fea3ceb90f8d0bee2f5cbc84e033f07 to your computer and use it in GitHub Desktop.
Save apio-sys/7fea3ceb90f8d0bee2f5cbc84e033f07 to your computer and use it in GitHub Desktop.

Nextcloud update notification and optional silent upgrade

Simple script to run/cron on your Nextcloud instance to either check and notify by email if an update is available or even perform the upgrade silently. This is never without risk of course and you should maintain proper backups before running an upgrade. An improperly finished upgrade can brick your Nextcloud instance for exemple simply when running out of diskspace.

It might have been better to write - at least parts - in PHP for instance to retrive the current version from the config/config.php file but it works as is in bash but might break if updater.phar at one point alters the way return messages are displayed.

Make sure user rights are correctly applied (i.e. 664 on files and 775 on dirs) and files owned by your webserver or the upgrade process will fail (this script does not specifically catches such errors if you let it run the update rahter than notifying only).

This supposes your system should be able to handle the mail command with appropriate configuration. I.e. with exim4 or postfix. But IMHO every one of your servers/services should be able to handle that in order to send other alerts and notification...

#!/bin/bash
#
# Define variables
DOMAIN=$(hostname -d) # This would work for host www.domain.com to retrieve domain.com. You might need hostname -f for other cases.
ALERT_MAIL=user@domain.com # Where do you want to receive the notification mail.
PATH_TO_NC="/var/www/cloud.domain.com" # Full path to your NC installation WITHOUT trailing slash.
NOTIFY_ONLY=1 # Only receive notifications.
DO_UPGRADE=0 # Upgrade if new version available.
PHP_CMD="sudo -u www-data /usr/bin/php" # Command for php-cli with rights to webserver files.
# Dynamic variables
CURRENT_VERSION=$(yes N \n | $PHP_CMD --define apc.enable_cli=1 $PATH_TO_NC/updater/updater.phar | grep -i "current" | cut -d ' ' -f 4 | rev | cut -c2- | rev)
NEXT_VERSION=$(yes N \n | $PHP_CMD --define apc.enable_cli=1 $PATH_TO_NC/updater/updater.phar | grep -i "update to" | cut -d ' ' -f 4)
UPDATE_AVAIL=$(yes N \n | $PHP_CMD --define apc.enable_cli=1 $PATH_TO_NC/updater/updater.phar | grep -i "available" | cut -d ' ' -f 1)
# Check which tasks to run
if [[ $NOTIFY_ONLY == "1" ]] && [[ $DO_UPGRADE == "0" ]]
then
# Notify only commands go here.
echo "Notifying if an update is available..."
echo $UPDATE_AVAIL | grep -i "no" || echo -e 'A Nextcloud upgrade is available for server '$HOSTNAME'. \nCurrent version is '$CURRENT_VERSION'. \nAvailable version is '$NEXT_VERSION'.' \
| mail -s 'Nextcloud upgrade available for server '$HOSTNAME'' -a \
"From: '$HOSTNAME' <donotreply@$DOMAIN>" $ALERT_MAIL
elif [[ $DO_UPGRADE == "1" ]] && [[ $UPDATE_AVAIL == "Update" ]]
then
# Upgrade commands go here.
echo "Upgrading and notifying if an update is available..."
echo $UPDATE_AVAIL | grep -i "update" || echo -e $UPDATE_AVAIL
cd $PATH_TO_NC
$PHP_CMD occ maintenance:mode --on
$PHP_CMD --define apc.enable_cli=1 updater/updater.phar --no-interaction --no-backup
$PHP_CMD occ upgrade -n
$PHP_CMD occ db:add-missing-indices -n
$PHP_CMD occ db:add-missing-primary-keys -n
$PHP_CMD occ db:add-missing-columns -n
$PHP_CMD occ db:convert-filecache-bigint -n
$PHP_CMD occ app:update --all
$PHP_CMD occ maintenance:mode --off
echo -e 'Nextcloud has been upgraded on server '$HOSTNAME'. \nPrevious version was '$CURRENT_VERSION'. \nNew version is '$NEXT_VERSION'.' \
| mail -s 'Nextcloud has been upgraded on server '$HOSTNAME'' -a \
"From: '$HOSTNAME' <donotreply@$DOMAIN>" $ALERT_MAIL
else
echo "Nothing to do..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment