#!/bin/bash # # Periodic maintenance operations for a Nextcloud instance. # This script is usually called by cron (perhaps indirectly). # # Author: Kovács Zoltán # License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html) # 2025-11-21 v0.1 Initial release # Accepted environment variables and their defaults. PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder. # Other initialisations. COMMANDLINE="php /var/www/html/cron.php" COMMANDUSER="www-data" SERVICENAME="nextcloud" YMLFILE="docker-compose.yml" # Messages. MSG_MISSINGDEP="Fatal: missing dependency" MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file" # Checks the dependencies. TR=$(which tr 2>/dev/null) if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi for item in basename dirname docker readlink do if [ -n "$(which $item)" ] then export $(echo $item | "$TR" '[:lower:]' '[:upper:]' | "$TR" '-' '_')=$(which $item) else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi done # All dependencies are available via "$THECOMMAND" (upper case) call. # # Let's find which version of docker-compose is installed. if [ $($DOCKER compose version 2>&1 >/dev/null; echo $?) -eq 0 ]; then # We'll use v2 if it is available. DOCKER_COMPOSE="$DOCKER" commandstring="compose" else # Otherwise falling back to v1. DOCKER_COMPOSE="$(which docker-compose)" commandstring="" fi # One of the two is mandatory. if [ -z "$DOCKER_COMPOSE" ];then echo "$MSG_MISSINGDEP docker-compose" >&2; exit 1; fi # Below docker-compose should be called as "$DOCKER_COMPOSE" $commandstring sequence. # Where I'm? # https://gist.github.com/TheMengzor/968e5ea87e99d9c41782 SOURCE="$0" while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && pwd )" #" SOURCE="$("$READLINK" "$SOURCE")" # if $SOURCE was a relative symlink, we need to resolve it # relative to the path where the symlink file was located [[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE" done; SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && pwd )" #" # Searches the base folder, containing a docker-compose.yml file. # Called from the base folder (./)? BASE_DIR="$PAR_BASEDIR" TEST_DIR="$SCRPATH" [[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR" # Called from ./tools? TEST_DIR="$("$DIRNAME" "$TEST_DIR")" [[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR" # Called from ./tools/*.d? TEST_DIR="$("$DIRNAME" "$TEST_DIR")" [[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR" # On failure gives it up here silently. if [ -z "$BASE_DIR" -o ! -r "$BASE_DIR/$YMLFILE" ]; then echo "$MSG_MISSINGYML" >&2; exit 1 fi # Only if the service is running. if [ -n "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]; then # Runs command COMMANDLINE on service SERVICENAME as COMMANDUSER. SERVICENAME="$("$BASENAME" "$BASE_DIR")-$SERVICENAME-1" "$DOCKER" exec -u "$COMMANDUSER" "$SERVICENAME" $COMMANDLINE fi # That's all, Folks!