Moving DATADIR gives "An unhandled exception has been thrown"

Using nc-datadir after a fresh install (no additional user besides ncp) gives the following output:

[ nc-datadir ] (Sat Jun 28 18:33:03 CEST 2025)
Maintenance mode enabled
moving data directory from /var/www/nextcloud/data to /media/data/ncdata/data...
BTRFS filesystem detected
Create subvolume '/media/data/ncdata'
{"reqId":"obhe3d8ivfRDT20pc160","level":2,"time":"2025-06-28T16:33:03+00:00","remoteAddr":"","user":false,"app":"no app in context","method":"","url":"--","message":"Temporary directory /var/www/nextcloud/data/tmp is not present or writable","userAgent":"--","version":"31.0.2.1","data":[]}
{"reqId":"obhe3d8ivfRDT20pc160","level":2,"time":"2025-06-28T16:33:03+00:00","remoteAddr":"","user":false,"app":"no app in context","method":"","url":"--","message":"Temporary directory /var/www/nextcloud/data/tmp is not present or writable","userAgent":"--","version":"31.0.2.1","data":[]}
Nextcloud is in maintenance mode, no apps are loaded.
Commands provided by apps are unavailable.
Your data directory is invalid.
Ensure there is a file called ".ncdata" in the root of the data directory. It should have the content: "# Nextcloud data directory"

**An unhandled exception has been thrown:**
**Exception: Environment not properly prepared. in /var/www/nextcloud/lib/private/Console/Application.php:143**
Stack trace:
#0 /var/www/nextcloud/console.php(81): OC\Console\Application->loadCommands()
#1 /var/www/nextcloud/occ(33): require_once('...')
#2 {main}Nextcloud is in maintenance mode, no apps are loaded.
Commands provided by apps are unavailable.
System config value logfile set to string /media/data/ncdata/data/nextcloud.log
Nextcloud is in maintenance mode, no apps are loaded.
Commands provided by apps are unavailable.
System config value tempdirectory set to string /media/data/ncdata/data/tmp
Installing template 'php/opcache.ini.sh'...
Maintenance mode disabled
Installing template 'ncp-metrics.cfg.sh'...
The NC data directory has been moved successfully.

seams to be just a warning, but how can it be avoided?

Yes, in the end it manages to create subfolders and everything correctly in the datafolder. Question is does it work now?

If yes, then you can be happy, and perhaps just check the repository to report the issue, so they can improve the script behavior.

Path to data directory

By default, the data directory in Nextcloud is .../data!

'datadirectory' => '.../.../nextcloud/data'

So regardless which procedure is used to change or move the data directory , the path to the data directory must include the complete path including .../data because this is where the required .ocdata or .ncdata file is located.

Common error message for incorrect path in config file

Error
Your data directory is invalid.

Ensure there is a file called ".ncdata" in the root of the data directory.

After a while a made a test on a new installed system. Datadir is located at /opt/ncdata on an ext4 formatted partition.

  1. Check if system is full functional and the .ncdata file is a it’s place: o.k.
  2. Create and mount a btrfs volume. Automounted to /media/cloud-data
  3. Run the nc datadir script.

Identical error messages.

is .../data in that path? did you try adding /data to your path?

No. But I don’t remember, if I first created a subdirectory .../ncdata or if I just used the mount point of the btrfs volume for the nc-datadir command.

Current (and working) situation:

ncpadmin@ncp:~$ sudo btrfs filesystem show
Label: 'cloud-data'  uuid: 2a9bf606-c3bf-467d-a3bf-0b9d4b42ffb9
        Total devices 1 FS bytes used 131.38GiB
        devid    1 size 426.45GiB used 136.02GiB path /dev/sda3

ncpadmin@ncp:~$ sudo btrfs subvolume list /media/cloud-data
ID 257 gen 1209 top level 5 path ncdata
ID 259 gen 91 top level 5 path ncp-snapshots/daily_2025-07-07_111702
...

and the directory structure is

/media/cloud-data/ncdata/
└── data
    β”œβ”€β”€ appdata_ocq0gg6p3fof
    β”œβ”€β”€ USER1
    β”œβ”€β”€ USER2
    β”œβ”€β”€ Klaus

    β”œβ”€β”€ ncp-update-backups
    β”œβ”€β”€ .opcache
    └── tmp

For me it looks like the nc-datadir script doesn’t not pre-create the .ncdata file, but creates .opcacheand copies the userdata.

It just moves the existing data directory from what I see in the script:

There should be no need to create .ncdata since it should already exist in the previous data directory.

You are right and I checked this before using the script. Still the error/warning remains.

initialise Nextcloud first β†’ create an admin user, which will create the .ocdata or .ncdata file in the /data directory.

you’re not using the snap, but change data directory will be similar… maybe that helps?

O. K. To get further insight I’ll give a try on a VM within the next days and report here.