Enable/disable maintenance mode from remote client

Nextcloud version (eg, 12.0.2): 15.0.2
Operating system and version (eg, Ubuntu 17.04): Debian 9 (stretch)
Apache or nginx version (eg, Apache 2.4.25): nginx/1.10.3
PHP version (eg, 7.1): 7.0.33

The issue you are facing: I am trying to enenable maintenance mode from remote machine using occ command. Therefore I wrote the script beneath.

#!/bin/bash

#
# Name:OnMaintenanceMode.sh
# Description: This script connects to source server. It changes directory to /var/www/nextcloud and activates
#                          maintenance mode on Nextcloud server.
# Use:  JOB level -> Pre-Script
#       It should be used before backup jobs will start to cover Nextcloud instance and integrity of data quality.
#

ELKARBACKUP_URL=root@sub.domain.tld:/var/www/nextcloud
URL=`echo $ELKARBACKUP_URL | cut -d ":" -f1`    # user@serverip
USER="${URL%@*}"                                # user
HOST="${URL#*@}"                                # host
DIR=`echo $ELKARBACKUP_URL | cut -d ":" -f2`    # path
SSHPARAMS='-i /var/lib/elkarbackup/.ssh/id_rsa -o StrictHostKeyChecking=no'

ssh $SSHPARAMS $USER@$HOST "cd /var/www/nextcloud | sudo -u www-data php occ maintenance:mode --on"

exit 0

When I now run the script on remote client, the system returns:

pi@nextcloud-bkp:/var/spool/elkarbackup/uploads $ sudo ./0005.script
Could not open input file: occ
pi@nextcloud-bkp:/var/spool/elkarbackup/uploads $

Any idea what is wrong?

Kind regards
//neph

try a “;” instead of the pipe “|”
"cd /var/www/nextcloud ; sudo -u www-data php occ maintenance:mode --on"
or
"sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on"

Replace | with &&, so that the ssh command is:

ssh $SSHPARAMS $USER@$HOST "cd /var/www/nextcloud && sudo -u www-data php occ maintenance:mode --on"

Another thing: why don‘t you use bash builtin for splitting up URL and dir like (there is no need open a subshell und running external a command):

URL="${ELKARBACKUP%:*}"    # user@serverip
USER="${URL%@*}"                   # user
HOST="${URL#*@}"                     # host
DIR="${ELKARBACKUP#*:}"        # path