HowTo: Change / Move data directory after installation

But also note that when following Solution 1, the doubled entry is avoided by this step:

update oc_storages set id='local::/new/path/to/data/' where id='local::/path/to/data/';

But aside from that obviously, having a look into this database table generally makes sense from time to time, when heavily changing data dir and/or external storages.

1 Like

hey I have try your tutorial but i have some probleme. First I don’t find the occ folder in my server second when i enter the first command the console display “php:comand not found”
(I have install nextcloud on the ubuntu server setup)

occ is not a folder, but a file, located in the root of your Nextcloud install dir, e.g. /var/www/nextcloud/occ.

Is the error message from php that it does not find the occ command, or is the php command itself not found (which seems to be the case based on error message)?
Please check which php if it gives any output. Assure that you have php-cli installed (which ships the command).

Which OS do you use? Sometimes the command is shipped with PHP version string inside, e.g. php7.3. It should be located in /usr/bin/ or /usr/local/bin/ depending on your install method. Or you run some system-wise search: find / -type f,l -name php

2 Likes

Is there also a step by step guide for Apache/PostgreSQL? Thanks!

Solution 2 works with postgres just the same way. Solution 1 should as well, since the SQL syntax is the same, AFAIK. Besides of course there is another command than mysqldump to create a database backup. pgsqldump? :wink:

Got it: Nextcloud Daten-Ordner verschieben (Apache, PostgreSQL)

Are these instructions still up to date for NC17 and NC18?
When looking at the official instructions on how to migrate to a different server (e.g., https://docs.nextcloud.com/server/18/admin_manual/maintenance/migrating.html), that page states about potential path changes:
“On the new system make sure to pick the appropriate locations. If you change any paths, make sure to adopt the paths in the Nextcloud config.php file”
However, it does not mention that the database needs to be adjusted. I would have assumed that I had to follow the steps mentioned for “Solution 1” in that case as well.

To me it feels like either this howto is outdated or the manual is incomplete. Thus, can someone provide an update on this issue and either
a) update this howto article with details for current NC versions (16-18) or
b) stimulate an update for the manual page mentioned above.

@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.