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: https://github.com/nextcloud/docker
- 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, curl installer...
As there still appear questions about that topic, I thought to describe all possibilities here as HowTo would be a good idea. Most information was originally taken from this topic: Is there a safe and reliable way to move data directory out of web root?
For the record: Changing the 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
Click to show some background information
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 can be manually adjusted within the database. But indeed manually adjusting the database as inexperienced users always bears the risk of mistakes, hence a full database dump/backup should be done.
Solution 1: Move data dir with database change
Solution 2: Link data dir without database change
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. Instead of prefixing all (non-www-data) commands with sudo
, you could also start an interactive root session:
sudo -s
-
For simplicity, we store all relevant paths into variables to be used later. Replace those placeholder paths to match your setup.
ncdir='/path/to/nextcloud' olddata='/old/path/to/data' newdata='/new/path/to/data'
-
Put Nextcloud into maintenance mode
sudo -u www-data php "$ncdir/occ" maintenance:mode --on
-
Copy Nextcloud data to new location. The below command copies the whole directory, including owner and modes, so there is no need to pre-create the new directory, change ownership or permissions.
sudo cp -a "$olddata/." "$newdata"
-
Update the data dir in
config.php
to
'datadirectory' => '/new/path/to/data',
. Thesed
command below should do it, but at best verify this afterwards, or edit the file manually.sudo sed -i "s|$olddata|$newdata|" "$ncdir/config/config.php"
-
Create a database dump/backup. Depending on the root userâs database authentication method, you might need to add the
-p
option (mysqldump -A -p ...
) to get a password prompt. Replace/path/to/backup
with any directory, suitable to keep a backup of your database.sudo mysqldump -A > /path/to/backup/dump.sql
-
Adjust
oc_storages
database table to reflect the new data folder location. We store the required database name and credentials, obtained from theconfig.php
, into variables, to avoid typos. You might want to verify that those were obtained correctly, but be careful to print such sensitive information to the console, when others may watch, and exit the shell session afterwards.dbname=$(sudo awk -F\' "/'dbname'/{print \$4;exit}" "$ncdir/config/config.php") dbuser=$(sudo awk -F\' "/'dbuser'/{print \$4;exit}" "$ncdir/config/config.php") dbpass=$(sudo awk -F\' "/'dbpassword'/{print \$4;exit}" "$ncdir/config/config.php") #echo "$dbname $dbuser $dbpass" mysql -u"$dbuser" -p"$dbpass" -e "update $dbname.oc_storages set id='local::$newdata/' where id='local::$olddata/';" unset -v dbname dbuser dbpass
-
Disable maintenance mode
sudo -u www-data php "$ncdir/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:
sudo rm -R "$olddata"
rm /path/to/backup/dump.sql
or just keep them as regular backups ;).
Solution 2
This solution is simpler and less risky, as it does not require to edit the database, but it does NOT fulfil security recommendations, as e.g. mentioned in the admin manual. So if you just need to move the data directory for storage reasons only, 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 given by the admin manual example: Installation on Linux â Nextcloud latest Administration Manual latest documentation
<Directory /path/to/nextcloud>
Options +FollowSymlinks
...
</Directory>
The steps were originally taken from here: Is there a safe and reliable way to move data directory out of web root? - #4 by JasonBayton
Instead of prefixing all (non-www-data) commands with sudo
, you could also start an interactive root session:
sudo -s
-
For simplicity, we store all relevant paths into variables to be used later. Replace those placeholder paths to match your setup.
ncdir='/path/to/nextcloud' olddata='/old/path/to/data' newdata='/new/path/to/data'
-
Put Nextcloud into maintenance mode
sudo -u www-data php "$ncdir/occ" maintenance:mode --on
-
Copy Nextcloud data to new location. The below command copies the whole directory, including owner and modes, so there is no need to pre-create the new directory, change ownership or permissions.
sudo cp -a "$olddata/." "$newdata"
-
Better safe than sorry, move the original data directory to a backup location, before creating the symlink to the new one. If you do not need the backup, replace, remove the old data dir instead.
sudo mv "$olddata" /path/to/backup/data #sudo rm -R "$olddata" sudo ln -s "$newdata" "$olddata"
-
Disable maintenance mode
sudo -u www-data php "$ncdir/occ" maintenance:mode --off
Again, if everything works fine, you could remove the backup or just keep it as regular backup.
sudo rm -R /path/to/backup/data
Note: If you want to adjust permissions of your new data directory, you need to chmod -R XYZ /new/path/to/data
, as chmod
does not follow symlinks. Thanks for the hint @STrike