Snap/Docker/VM users click here!
Note that this HowTo does not apply to Nextcloud containers/appliances or the official Nextcloud VM from Hansson IT.
- If you use Nextcloud Snap, follow their official simple instructions: Change data directory to use another disk partition ¡ nextcloud-snap/nextcloud-snap Wiki ¡ GitHub
- If you use the Nextcloud Docker container, the GitHub page might give some hints as well: GitHub - nextcloud/docker: â´ Docker image of Nextcloud
- The official Nextcloud VM has the data already located on a separate ZFS partition, which you can easily raise in size or even add a new drive to the ZFS pool. Thanks for the hint @jeremy_fritzen.
- Also NextCloudPi might provide own solutions: GitHub - nextcloud/nextcloudpi: đŚ Build code for NextcloudPi: Raspberry Pi, Odroid, Rock64, Docker, curl installer...
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.
Background
Nextcloud looks up the location of the data directory inside its /path/to/nextcloud/config/config.php
by the 'datadirectory'
directive: Configuration Parameters â Nextcloud latest Administration Manual latest documentation
As seen '/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 config.php
, the 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.
Solution 1
I refer to Is there a safe and reliable way to move data directory out of web root? - #17 by robertb 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: echo "Your Nextcloud dbname is: $(awk -F\' "/'dbname'/{print \$4;exit}" /path/to/nextcloud/config/config.php)" dbuser=$(awk -F\' "/'dbuser'/{print \$4;exit}" /path/to/nextcloud/config/config.php) dbpassword=$(awk -F\' "/'dbpassword'/{print \$4;exit}" /path/to/nextcloud/config/config.php) mysql -u"$dbuser" -p"$dbpassword" // Inside the MySQL console: use <dbname>; 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; // Again outside the MySQL console unset -v dbuser dbpassword 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 ;).
Solution 2
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: Installation on Linux â Nextcloud latest Administration Manual latest documentation
<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? - #4 by JasonBayton
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 -h 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