As there still appear questions about that topic, I thought sum up all possibilities here in howto would be a good idea. There is already a good thread that handles this topic: Is there a safe and reliable way to move data directory out of web root?
First of all: Changing data directory after installation is not officially supported. Consider re-installing Nextcloud with new data directory, if you did not use it too much/added users/created shares/tags/comments etc.
Nextcloud looks up the location of the data directory inside its
/path/to/nextcloud/config/config.php by the
'datadirectory' directive: https://docs.nextcloud.com/server/13/admin_manual/configuration_server/config_sample_php_parameters.html#default-parameters
'/path/to/nextcloud/data' is default value/location, if it is not changed during Nextcloud installation process.
Nextcloud stores the data directory location and all it’s files locations inside it’s database, that can be chosen during installation as well. Therefore it creates the tables
oc_storages for the data directory and
oc_filecache for the files. Other than it’s name suggests, it is actually not a cache, more an index of all files inside the data directory. If a file is not listed inside this table, it can’t be handled by Nextcloud, nor will it be seen inside the web ui.
So by just moving the data directory to another location and change the
'datadirectory' directive inside
oc_filecache will be full of wrong entries from the old location, where the files entries for the new location are missing, thus no files will be shown inside Nextcloud.
Nextcloud itself will regularly clean
oc_filecache from old/obsolete entries (by cron job
occ files:cleanup), but the new entries would need to be manually created by running
occ files:scan --all. For apache webserver the full command would look like:
sudo -u www-data php /path/to/nextcloud/occ files:scan --all
Your files will be now accessible again through Nextcloud. The remaining problem now is, that recreating the file index will also create new file IDs. Other related information, like shares, tags, comments, previews etc., on the other hand are connected to the old file IDs, thus will be lost.
To avoid this whole file index recreation problem, the
oc_storages database table could be manually adjusted from within the database server. Where I don’t know any way to do that for sqlite, for mysql/mariadb with root access this is quite simple. But indeed manually adjusting the database always bears the risk of corruption, thus database access for Nextcloud should be prevented during the process, simply by putting nextcloud into maintenance mode, and a full database dump/backup should be done.
I refer to Is there a safe and reliable way to move data directory out of web root? where the steps are given and list/complete them here with commands where apache webserver and mysql/mariadb database is assumed:
1. sudo -u www-data php /path/to/nextcloud/occ maintenance:mode --on 2. mkdir -p /new/path/to/data 3. cp -a /path/to/data/. /new/path/to/data 4. chown -R www-data:www-data /new/path/to/data 5. nano /path/to/nextcloud/config/config.php 'datadirectory' => '/new/path/to/data', 6. mysqldump -u<rootuser> -p > /path/to/dbdump/dump.sql 7. Adjust "oc_storages"database table to reflect the new data folder location: 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; 8. sudo -u www-data php /path/to/nextcloud/occ maintenance:mode --off
After that, carefully test Nextcloud, the files inside web ui, shares, tags, comments etc. If everything is working fine and Nextcloud indeed handles the files on the new location, you could remove the backups:
1. rm -R /path/to/data //old location!! 2. rm /path/to/dbdump/dump.sql
or just keep them as regular backups ;).
This solution is far simpler and less risky in data corruption concerns, but it does NOT fulfil the security recommendations, e.g. mentioned in admin manual. So if you just need to move the data directory for storage reasons, not for security reasons, just create a symbolic link from old to new data directory location. For that you only need to be sure, that symbolic links are correctly handled by your webserver. Nginx should handle symlinks correctly by default, for apache the related option needs to be set in your Nextcloud vhost, as already given by the admin manual example: https://docs.nextcloud.com/server/13/admin_manual/installation/source_installation.html#apache-web-server-configuration
<Directory /path/to/nextcloud/> Options +FollowSymlinks ... </Directory>
If so, then do:
Steps are also given here: Is there a safe and reliable way to move data directory out of web root?
1. sudo -u www-data php /path/to/nextcloud/occ maintenance:mode --on 2. mkdir -p /new/path/to/data 3. cp -a /path/to/data/. /new/path/to/data 4. mv /path/to/data /path/to/dataBackup 5. ln -s /new/path/to/data /path/to/data 6. chown -d www-data:www-data /path/to/data # To set symlink ownership 7. chown -R www-data:www-data /new/path/to/data # To set actual data dir ownership 8. sudo -u www-data php /path/to/nextcloud/occ maintenance:mode --off
Again, if everything works fine, you could remove
/path/to/dataBackup or just keep it as regular backup.
Note: If you want to adjust permissions of your new data folder, you need to
chmod -R XYZ /new/path/to/data, as
chmod does not follow symlinks. Thanks for the hint @STrike