HowTo: Change / Move data directory after installation

@b-pfl
The methods here are still valid. Please see the discussion, where it is mentioned that editing the database entry is indeed not required (as far as it was tested). But see this post of nickvergessen that it is still the recommended way to keep the database entries clean and consistent.

@nickvergessen
Indeed your statement (hence this HowTo) and Nextcloud docs do not match here. If altering the database on data dir transfer is recommended, then this should be just the same when migrating a Nextcloud instance to a different server and having a different data dir path there. But a definit solution to rule out any issues would be if Nextcloud altered the database automatically according to config.txt entry, to have a single matching entry for local data dir only.

@MichaIng I assume you mean config.php?

The config.php entry must be changed. If Nextcloud finds no matching database entry, it creates an additional one, which then works fine according to quick tests and user reports above. But to be sure and not mess up the database with obsolete entries it is recommended to replace the old entry with the new one manually as mentioned in solution 1.

Well the question is which storage to update. Sure I guess hacking could take place to achieve that, but Iā€™d like to rather not do this.

The data dir entry/entries should be identifyable, although I know there are ā€œlocal::ā€ entries as well for every user. Iā€™ll dig into it by times to check for safe ways to identify the correct entries. Basically of config.php does not contain a matching entry, Nextcloud could remove all obsolete ones before adding the correct new one.

Hi All, I am a newbie here and really appreciate this thriving community. I have Rpi 4 with 2gb and 2 5TB HDD on RAID 1. My challenge was that I couldnā€™t get NCPi to target to RAID as I was doing a fresh install and RAID was built ā€œafterā€ NCPi was installed. Hence, intense search on changing Data after install.

I was going to go for the easy method but read the warning from @nickvergessen on DB integrity topic and decided against it.

I followed all the steps for Solution 1 from @MichaIng and it went well. I am able to navigate NCPi and all featuresā€¦ at least till now.

The only problem that I faced was to understand how ā€œoc_storagesā€ DB table can be edited. I have no clue on DBs so this was a challenge. After searching forever (3 days), I stumbled upon phpMyadmin, installed it and used the user and password from config fileā€¦ lo behold, I was able to edit the DB. I am very impressed with this forum! Great work, all!

1 Like

Although phpMyAdmin provides a nice GUI, it is not required to alter the database. The mysql command line tool can be used from any shell/terminal/emulator/SSH, according to the HowTo:

mysql -u<rootuser> -p
# enter mysql root password, then within mysql console:
use <nextclouddb>;
update oc_storages set id='local::/new/path/to/data/' where id='local::/path/to/data/'; //take care about backslash at the end of path!!
quit;

Usually <rootuser> is ā€œrootā€, with MariaDB on modern Debian at least the -p option must or can be omitted when you logged in as root UNIX user. You could also use the Nextcloud database user, where /path/to/nextcloud/config/config.php provides its name and password.

Actually using Nextcloud database user is the much better way, since it has only permissions to alter the Nextcloud database and no other, hence one cannot by accident break something within other databases. Something Iā€™ll edit in the HowTo another day :thinking:.

Thanks a lot, @MichaIng for the clarification. This is really helpful. I am just scared to make changes in places where I have very little or no experience. Hence for me, phpMyAdmin was a boon.
But I get your point. Will try CLI the next time I need itā€¦ hopefully never, as I am still scared. :grimacing:

If you still have trouble remember your nextcloud.conf

Did you set open_basedir? If so then add your new data-folder!

ā€œ:/new/path/data/ā€

Just to mention (because i was thinking of thatā€¦) there is no need to name your new path ā€œdataā€ at the very end.

Hello everyone. Sorry for posting on such an old post, but this is fairly relevant to what Iā€™m trying to do so I thought this was better than creating a new post.

Iā€™m installing Nextcloud 18.0.2 manually onto a Raspberry Pi 4 (Raspian 10 - Buster), using MariaDB. My first plan was to install it directly with a data directory (/mnt/MyData/nextcloud/data). I performed the following steps:

1. Download/extract Nextcloud 18.0.2.
2. mkdir -p /mnt/MyData/nextcloud/data
3. sudo chown -R www-data:www-data /var/www/html/nextcloud/
4. sudo chown -R www-data:www-data /mnt/MyData/nextcloud/
5. sudo chmod 750 /mnt/MyData/nextcloud/data
6. Go to the IP address of my Pi (ipaddress/nextcloud)
7. Create Admin account, select MariaDB as database, enter in /mnt/MyData/nextcloud/data/ for the data directory.
8. Finish.

I was given an error message, stating that Nextcloud could not read nor write to the given directory.

So I tried the original setup (putting data in /var/www/html/nextcloud/data), and got through the installation.

Then I tried to follow the instructions from @MichaIng to change the data directory (specifically Solution 1). I managed to get to step 8, but as soon as I try to switch off maintenance mode, I get the following error:

Your data directory is invalid
Ensure there is a file called ".ocdata" in the root of the data directory.

Cannot create "data" directory
This can usually be fixed by giving the webserver write access to the root directory. See https://docs.nextcloud.com/server/18/go.php?to=admin-dir_permissions

An unhandled exception has been thrown:
Exception: Environment not properly prepared. in /var/www/html/nextcloud/lib/private/Console/Application.php:167
Stack trace:
#0 /var/www/html/nextcloud/console.php(98): OC\Console\Application->loadCommands(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#1 /var/www/html/nextcloud/occ(11): require_once('/var/www/html/n...')
#2 {main}PHP Warning:  fileperms(): stat failed for /mnt/MyData/nextcloud/data/nextcloud.log in /var/www/html/nextcloud/lib/private/Log/File.php on line 83
{"reqId":"kzyq8Uali4fwAG02lyFo","level":3,"time":"2020-03-15T20:01:46+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"fileperms(): stat failed for /mnt/MyData/nextcloud/data/nextcloud.log at /var/www/html/nextcloud/lib/private/Log/File.php#83","userAgent":"--","version":"18.0.2.2"}
{"reqId":"kzyq8Uali4fwAG02lyFo","level":3,"time":"2020-03-15T20:01:46+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"You are using a fallback implementation of the intl extension. Installing the native one is highly recommended instead. at /var/www/html/nextcloud/3rdparty/patchwork/utf8/src/Patchwork/Utf8/Bootup/intl.php#18","userAgent":"--","version":"18.0.2.2"}

Does anybody know why this is happening? I have all the right permissions and there should be no issue with reading and writing, but it still occurs. Here is the output of sudo ls -la in my /var/www/html/nextcloud directory and my /mnt/MyData/nextcloud directory:

/var/www/html/nextcloud:
total 164
drwxr-xr-x 14 www-data www-data 4096 Mar 15 15:39 .
drwxr-xr-x 3 root root 4096 Mar 15 15:38 ā€¦
drwxr-xr-x 33 www-data www-data 4096 Mar 11 10:56 3rdparty
drwxr-xā€” 49 www-data www-data 4096 Mar 15 15:47 apps
-rw-rā€“r-- 1 www-data www-data 15752 Mar 11 10:52 AUTHORS
drwxr-xā€” 2 www-data www-data 4096 Mar 15 15:53 config
-rw-rā€“r-- 1 www-data www-data 3910 Mar 11 10:52 console.php
-rw-rā€“r-- 1 www-data www-data 34520 Mar 11 10:52 COPYING
drwxr-xr-x 23 www-data www-data 4096 Mar 11 10:57 core
-rw-rā€“r-- 1 www-data www-data 5048 Mar 11 10:52 cron.php
drwxr-xā€” 5 www-data www-data 4096 Mar 15 15:46 data
-rw-rā€“r-- 1 www-data www-data 2647 Mar 15 15:46 .htaccess
-rw-rā€“r-- 1 www-data www-data 156 Mar 11 10:52 index.html
-rw-rā€“r-- 1 www-data www-data 2976 Mar 11 10:52 index.php
drwxr-xr-x 6 www-data www-data 4096 Mar 11 10:52 lib
-rw-rā€“r-- 1 www-data www-data 283 Mar 11 10:52 occ
drwxr-xr-x 2 www-data www-data 4096 Mar 11 10:52 ocm-provider
drwxr-xr-x 2 www-data www-data 4096 Mar 11 10:52 ocs
drwxr-xr-x 2 www-data www-data 4096 Mar 11 10:52 ocs-provider
-rw-rā€“r-- 1 www-data www-data 3056 Mar 11 10:52 public.php
-rw-rā€“r-- 1 www-data www-data 5235 Mar 11 10:52 remote.php
drwxr-xr-x 4 www-data www-data 4096 Mar 11 10:52 resources
-rw-rā€“r-- 1 www-data www-data 26 Mar 11 10:52 robots.txt
-rw-rā€“r-- 1 www-data www-data 2381 Mar 11 10:52 status.php
drwxr-xr-x 3 www-data www-data 4096 Mar 11 10:52 themes
drwxr-xr-x 2 www-data www-data 4096 Mar 11 10:53 updater
-rw-rā€“r-- 1 www-data www-data 101 Mar 11 10:52 .user.ini
-rw-rā€“r-- 1 www-data www-data 362 Mar 11 10:56 version.php

/mnt/MyData/nextcloud
total 12
drwxr-xr-x 3 www-data www-data 4096 Mar 15 15:41 .
drwxrā€“r-- 4 pi pi 4096 Mar 15 15:51 ā€¦
drwxr-xā€” 5 www-data www-data 4096 Mar 15 15:46 data

/mnt/MyData/nextcloud/data (identical to /var/www/html/nextcloud/data)
total 52
drwxr-xā€” 5 www-data www-data 4096 Mar 15 15:46 .
drwxr-xr-x 3 www-data www-data 4096 Mar 15 15:41 ā€¦
drwxr-xr-x 8 www-data www-data 4096 Mar 15 15:47 appdata_octf179zqtl4
drwxr-xr-x 2 www-data www-data 4096 Mar 15 15:46 files_external
-rw-rā€“r-- 1 www-data www-data 542 Mar 15 15:46 .htaccess
-rw-rā€“r-- 1 www-data www-data 0 Mar 15 15:46 index.html
drwxr-xr-x 3 www-data www-data 4096 Mar 15 15:46 KimAdmin
-rw-r----- 1 www-data www-data 25937 Mar 15 15:49 nextcloud.log
-rw-rā€“r-- 1 www-data www-data 0 Mar 15 15:46 .ocdata

Additionally, I have tried Solution 2 (symlinking), but I get stuck again on the same step (turning maintenance mode off) with the same exact error messageā€¦

Thanks for the help!

UPDATE: I think Iā€™ve finally managed to get it workingā€¦ The permissions of the parent directories were likely insufficient. The entire /mnt/MyData now have permissions (set with sudo chmod -R 777 /mntā€¦ Iā€™m not sure how secure this is but it works.

2 Likes

Hi nextcloud forum,
I also tried Solution 2 to change a data folder just for a single user (user1) with a Symlink. Thatā€™s my config:

/var/ncData/user1
New data location: /var/ncNewData/user1

My standard permissions for folders are 750 and for files 640. I also set symlink ownership and set the actual data folder ownership (like described in solution2).
But with this setting no file access r/w for user1 was possible. I always received permissions errors for user1.
Then I tried to adjust the permissions. The following permission are working now for user1:

/var/ncData/user1 >> Symlink 777 (standard)
/var/ncNewData >> 777
/var/ncNewData/user1/ >> 750 etcā€¦

Key element is the 777 permission on the ncNewData folder!

The solution describes the update from brrrlinguist very well.
Iā€™m also not sure how secure this is but itā€˜s works for me too. I donā€™t like 777 permissions, so perhaps somebody can give us an advice if this is a security issue or not.

Thanks guys.

You mean the UNIX user1? You should generally not touch the the files directly outside of Nextcloud.

No, not an unix user. I mean a dummy nextcloud user. The folder /var/ncData/user1 is the data folder from the dummy nextcloud user1. Touching files only with the webserver unix user www-data.

The new root nextcloud user data location for this single nextcloud user ist now /var/ncNewData. And here I have little concerns about the 777 permission.

Hmm, as long as the www-data has R/W access, everything else should not matter. E.g. my permissions:

2020-03-18 11:00:15 root@micha:/tmp# ls -al /mnt/sda/ncdata/
total 64
drwx------  9 www-data www-data  4096 Oct 22 14:03 .
drwxr-xr-x  7 root     root      4096 Mar 15 22:50 ..
-rw-------  1 www-data www-data   542 Mar 14 12:47 .htaccess
-rw-------  1 www-data www-data     0 Mar 14 12:47 .ocdata
drwx------  7 www-data www-data  4096 Sep  7  2019 User2
drwx------  8 www-data www-data  4096 Aug 11  2019 Micha
drwx------ 10 www-data www-data  4096 Aug 25  2019 appdata_<ID>
drwx------  2 www-data www-data  4096 Mar 14 12:47 files_external
-rw-------  1 www-data www-data     0 Mar 14 12:47 index.html
-rw-------  1 www-data www-data     0 Mar 18 11:00 nextcloud.log
drwx------  2 www-data www-data 12288 Mar 18 04:20 ownbackup
drwx------  2 www-data www-data  4096 Mar 15 14:40 tmp
drwx------  4 www-data www-data  4096 Mar 14 12:47 updater-<ID>
-rw-------  1 www-data www-data 12893 Mar 14 12:46 updater.log

Especially if your non-dummy admin Nextcloud user can access the files, from UNIX permissions side, all Nextcloud users must be able, as long as they have Nextcloud-internal permissions of course.

Does the webserver log, php-fpm.log (in case) or Nextcloud log show any related entry?

I changed the data folder just for one user in my example. Admin or other Ćŗsers will use the user folder whats stated in the nextcloud config (/var/ncData/). So, other nextcloud users will not have a problem.

Thatsā€™s the nextcloud error message, when I change the permission from the ā€œnewā€ user folder (/var/ncNewData) to 750 and open a file:

fopen(/var/nc_data/user1/files/files.pdf): failed to open stream: Permission denied at /var/www/nextcloud/lib/private/Files/Storage/Local.php#302

If I changed the permission back to 777 and everything works fine. Thats a bit strange for me because also my standard data folder (/var/ncData/) has 750 and thats work fine. Perhaps thatā€™s a behavior because I created a Symlink to the new data folder. One question for me if this is a security issue or notā€¦ :thinking:

Oh damn. I found this stupid issue. Thanks for advice, MichaIng. I rechecked the ownership from the new data folder

chown -R www-data:www-data /var/ncNewData/

and this solved it. I could have sworn Iā€™ve done this before :roll_eyes:

2 Likes

This is not possible btw. there is only one data dir for all users (of course subdirs each user) and there should be a protection against symlinks from inside the data dir to outside. This would explain why moving a single users data outside via symlink leads to permissions denied, because it would follow a symlink.
This protection is important, otherwise users could access the whole system by uploading symlinks, in theory.

So, Iā€™m asking just to make sure.

Iā€™ve got Nextcloud installed onto a Raspberry Pi 4 with 2 GB of RAM, running Arch Linux ARM with a LEMP stack.

My data folder is on an external hard drive, at the root of a partition on it. The respective partition is mounted at /var/nextcloud, and thatā€™s where Nextcloud looks for its data folder.

I want to mount that partition somewhere else since I want to use it for something else as well, say Iā€™m going to mount it on /mnt/external_hdd. Everything should work if I create a folder on that partition, /mnt/external_hdd/nextcloud as an example, then I move everything from that partition into that folder, and then I run ln -s /mnt/external_hdd/nextcloud /var/nextcloud, right?

Also I think itā€™s best if I do this with the whole thing turned off, do all the things on the external hard drive, then take the SD card out of the Raspberry Pi, create the symlink and modify the fstab this way. Is there any difference?

No.

You set the correct rights for your nextcloud user for /mnt/external_hdd/nextcloud (use a linux formatted file system like ext4 or btfs).
Then you switch the data-dir in config/config.php
And then you must rescan all with ā€œoccā€

sudo -u www-data php occ files:scan --all

https://docs.nextcloud.com/server/19/admin_manual/configuration_server/occ_command.html

Wyh not NextcloudPi https://ownyourbits.com/nextcloudpi/

1 Like

Ƅhm sorry but while your solution works (not great, see below), @Andy3153 works as well (ā€œSolution 2ā€ of this HowTo) and the occ re-scan can be skipped when altering the database entry according to ā€œSolution 1ā€ of this HowTo. To not mess things, within this thread, lets stay with the provided two solutions or, if you find yours the better (3.) one, do a suggestion to have it added. But the issue with yours is that ALL old oc_filescache entries will stay, hence the database will grow enormouslyā€¦

@Andy3153
So yes, your idea works fine, as of ā€œSolution 1ā€.

This is not required. Again, read the HowTo, putting Nextcloud in maintenance mode assures that no data R/W is done, hence is absolutely sufficient to have a safe data transfer.
fstab modification has not much to do with it. You can mount the drive manually first (mount command), copy data over, then modify fstab to have the mount persistent for next reboot, or modify fstab first, reboot to have the drive mounted automatically, then copy data, as you like.

1 Like