Backup with rsync in cron job ends up with an empty data directory

Support intro
  • Nextcloud Server version (e.g., 29.x.x):

  • Operating system and version (e.g., Ubuntu 24.04):
    *

    Linux 6.18.22-i1-hp x86_64
  • Web server and version (e.g, Apache 2.4.25):

    • Apache 2.4.
  • Reverse proxy and version _(e.g. nginx 1.27.2)

    • no idea how to check
  • PHP version (e.g, 8.3):

    • 8.4.20
  • Is this the first time you’ve seen this error? (Yes / No):

    • no
  • When did this problem seem to first start?

    • from the begining
  • Installation method (e.g. AlO, NCP, Bare Metal/Archive, etc.)

    • manual installation
  • Are you using CloudfIare, mod_security, or similar? (Yes / No)

    • not that I would know

Summary of the issue you are facing

I have created a script to backup nextcloud. This script is planned to be started from a cron job. My installation is hosted at TELEKOM with an business contract on a virtual server. I do think I have troubles with the user rights.

When I start the script from an ssh connection to the server evertyhing went well. When I start the script via cron the script will be interrupted ba an fault. The scripte is nat allowed to copy the data directory. I think the user that is used by cron is not able to touch the data directory.

drwxrwx— 20 www-data www-data 4096 31. Mär 11:18 data

This are the rights at the data directory.
I have tried to start the rsync command directly with cron an end up with an empty data directory.

I fear to change the user rights of the data directory because of the data safety.

is ther a posibillity to find out the user of cron and add them to the user group of www-data to give cron the necessayr rights to the datat directory.
any other hint how I can overcoem this situation.

This is the script I’m rying to use for the backup

#!/bin/bash -e

#Das ist das Backup der Nextcloud Seite als script

0.Schritt Maintenance Mode ein

echo “Jetzt geht es los: $(date)”>/home/www/scripts/next.txt
cd /home/www/public_html/nextcloud
php occ maintenance:mode --on

1.Schritt Daten kopieren

rsync -avz --delete -b --backup-dir=/home/www/backupalt /home/www/public_html/nextcloud /home/www/backup

echo “Daten sind kopiert: $(date)”>>/home/www/scripts/next.txt

1.1 Schritt Maintenance Mode aus

cd /home/www/public_html/nextcloud
php occ maintenance:mode --off

2.Schritt Datenbank backup anlegen

mysqldump -h mysqla.service -u HTO01FLQKVOF_3 -pJuqoaPpqWqU9ehWW HTO01FLQKVOF_3 > /home/www/datenbank/NEXTCLOUD.sql

echo “Datenbank Abzug ist erfolgt: $(date)”>>/home/www/scripts/next.txt

3.Schritt Datenbankkopie in das backup Verzeichnis kopieren

rsync -avz --delete -b --backup-dir=/home/www/backupalt /home/www/datenbank /home/www/backup

echo “Alles erledigt: $(date)”>>/home/www/scripts/next.txt

Steps to replicate it (hint: details matter!): Non is an continuous problem

Log entries Non

Nextcloud

Please provide the log entries from your Nextcloud log that are generated during the time of problem (via the Copy raw option from Administration settings->Logging screen or from your nextcloud.log located in your data directory). Feel free to use a pastebin/gist service if necessary.

PASTE HERE

Web Browser

If the problem is related to the Web interface, open your browser inspector Console and Network tabs while refreshing (reloading) and reproducing the problem. Provide any relevant output/errors here that appear.

PASTE

Web server / Reverse Proxy

The output of your Apache/nginx/system log in /var/log/____:

PASTE HERE

Configuration

Nextcloud

The output of occ config:list system or similar is best, but, if not possible, the contents of your config.php file from /path/to/nextcloud is fine (make sure to remove any identifiable information!):

PASTE HERE

Apps

The output of occ app:list (if possible).

Tips for increasing the likelihood of a response

  • Use the preformatted text formatting option in the editor for all log entries and configuration output.
  • If screenshots are useful, feel free to include them.
    • If possible, also include key error output in text form so it can be searched for.
  • Try to edit log output only minimally (if at all) so that it can be ran through analyzers / formatters by those trying to help you.

Do not change the Nextcloud folder permissions, it is a security risk! The problem with cron jobs is maybe due to two things:

Missing paths: Cron does not recognize standard commands. You can use absolute paths in the script (e.g., /usr/bin/php instead of php and /usr/bin/rsync instead of rsync).

Maybe wrong user: Use echo $(whoami) in the script to check whether the Cron job is actually running under your SSH user or is being started by the system as a restricted user.

rsyn -v -n --stats --progress should give you some more infos.

Maybe you show the whole script and the cron -l output?

Please let me know if this helps.