HowTo: Change / Move data directory after installation

can someone please clarify the correct permissions for the data folder and for the underling directorys?

Thanks.

What is your question here? In short words. The user running the webserver needs access to the data directory.

Thanks, I ran into the same issue - I am presuming that I can leave the old location in the database?

in my case **/var/www/nextcloud/data-nc/ ** was the old location during the installation and I have since migrated to a separate drive partition via /media/nextcloud-data.

Just feels a little untidy and I would like to delete the /var/www/nextcloud/data-nc/ location, if I do so will NC simpley mark it as unavailable and work fine?

MariaDB [nextcloud]> select * from oc_storages;
+------------+------------------------------------+-----------+--------------+
| numeric_id | id                                 | available | last_checked |
+------------+------------------------------------+-----------+--------------+
|          1 | local::/var/www/nextcloud/data-nc/ |         1 |         NULL |
|          2 | home::user1                        |         1 |         NULL |
|          3 | home::user2                        |         1 |         NULL |
|          4 | home::user3                        |         1 |         NULL |
|          5 | home::user4                        |         1 |         NULL |
|          6 | home::user5                        |         1 |         NULL |
|          7 | home::user6                        |         1 |         NULL |
|          8 | home::user7                        |         1 |         NULL |
|          9 | local::/media/nextcloud-data/      |         1 |         NULL |
+------------+------------------------------------+-----------+--------------+

You can simply delete it, check this out Files amount after moving of data directory is wrong (much bigger)

1 Like

I took a slightly different approach to moving data, by creating a mirrored RAID on my Pi 4 with (2) 128G SSDā€™s.

Following this a a guide I created the mirrored RAID:

With the RAID initially mounted on /mnt I copied all folders from /var/www to /mnt and changed folder permissions and ownership to match.

When I added the RIAD to /etc/fstab I referenced /var/www instead of /mnt and rebooted.

The new RAID mount obscures the original folders in /var/www (even though I renamed all prefixed with _) and used the folders copied above.

So far, everything seems ok with no glitches!

Hi all, if someone could give me a hand on how to move data out of the VM to external hard drive (shared drive from windows) and autoupload to this external hard drive then it would be great.
My SSD is running out of spaceā€¦ Sorry for waking an old postā€¦

Hello

I am running a Raspberry 4 with an USB disk which I want to replace. On the disk is only the data not the database. I am running the newest version of NextcloudPi.
So what I did:

    rsync -rav /old-disk /new-disk
    service apache2 stop
    edited config.php 'datadirectory'
    unplug old disk    
    sudo -u www-data php /var/www/nextcloud/occ files:scan --all
    service apache2 start

Now I have a big problem:

  • if I upload a small text file, eveything is ok.
  • But if I try to upload a bigger file like 1MB picture, I get an error. All bigger uploads are broken. Fron the mobile app, from the desktop client, from the browser.
  • If I plug in the old disk, everything (upload of bigger files) works again.
  • The error from the server log is very cryptic and very long. The client says ā€œconnection closedā€.

I did a file search and found the old paths in these files:
/etc/php/7.3/fpm/conf.d/10-opcache.ini
/etc/php/7.3/fpm/conf.d/10-opcache.ini
/etc/php/7.3/fpm/php.ini
/etc/php/7.3/fpm/php.ini
/etc/php/7.3/mods-available/opcache.ini
/etc/php/7.3/cli/conf.d/10-opcache.ini
/etc/php/7.3/cli/php.ini
/etc/fail2ban/jail.local
/etc/fail2ban/jail.conf
I replaced the path in there files, restarted apache, but no change. The old disk is still needed.

Has somebody a tip?

Thanks very much
Gilbert

EDIT:
I solved the problem by myself. Like always with Nextcloud and this forum. If somebody has a similar question/problem, how to replace the usb data disk on a raspberry pi, this is what I have done:

date; rsync -rav /media/myCloudDrive/ncdata/ /media/myCloudDriveNew/ncdata/; date;
service apache2 stop
sudo btrfs filesystem label '/media/myCloudDrive/' 'myCloudDriveOld'
sudo restart
service apache2 stop
sudo btrfs filesystem label '/media/myCloudDriveNew/' 'myCloudDrive'
sudo restart
unplug the old small usb disk

My disks are formatted with btrfs. If you have another file system, you have to google for the command for renaming the label.

Good luck!

Thanks for this guide - very helpful. For me, symlink didnā€™t work no matter how I tried. On Ubuntu server 20.04 LTS (raspberry pi version) and 20.0.5 nextcloud.

What did work was mount --bind, from this post: [Solved] Nextcloud, change Data folder location / Newbie Corner / Arch Linux Forums

Took a long time to finally get there but I did.

Thanks for the feedback. Probably the file system does not support symlinks, like FAT-family or NTFS (without ntfs-3g driver)? The bind mount is a good alternative in such case, but it means that an additional fstab entry or mount call is required while a symlink works on itā€™s own.

Interestingā€¦ The target is on a ext4-formatted LUKS drive while the original/default place is on an ext4-formatted SSD on a raspberry pi. I honestly donā€™t know what was wrong. Yes it will be an additional command each time I reboot. :expressionless:

Strange indeed then. Does the symlink creation produce any error and does it show up correctly in the file system? E.g.:

ln -s /mnt/external/ncdata /var/www/nextcloud/testlink
ls -l /var/www/nextcloud/testlink

If you use Apache2, is Options +FollowSymLinks allowed for the Nextcloud web dir? Although if not, also rewrites would be broken.

Just my two cents: I was struggeling with the error message as well about the .ocdata file not found.
Based on this topic and the comment by @nachoparker about checking the output of ls -la of the /media directory, I found my problem. The owner of the directory and group settings were blocking access to the directory for the www user.
So in my case, the /media/ needed additional access rights, so www user can access this directory and the subfolder the nextcloud data folder is placed.

i donā€™t understand step 6 and 7
6. mysqldump -u -p > /path/to/dbdump/dump.sql
7. Adjust "oc_storages"database table to reflect the new data folder location:
dbuser=$(awk -Fā€™ ā€œ/ā€˜dbuserā€™/{print $4;exit}ā€ /path/to/nextcloud/config/config.php)
dbpassword=$(awk -Fā€™ ā€œ/ā€˜dbpasswordā€™/{print $4;exit}ā€ /var/www/nextcloud/config/config.php)
mysql -u$dbuser -p$dbpassword
// Inside the MySQL console:
use ;
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 dbuser dbpassword

please help, it hard to follow for me (newbie). i donā€™t know path /path/to/dbdump/dump.sql

You can use any path you think is good for a backup. When unsure use your current users home directory:

mysqldump -uroot -p > ~/dump.sql

Step 7 is mostly copy&paste. Only replace <nextclouddb> with the database name, usually itā€™s simply nextcloud. Ah this can be found in the config.php as well, aside of database user and password. And replace the old and new data path of course.

Is this topic still relevant?
Iā€™m on Nextcloud 21 and found this topic after already moving the datadirectory by simply moving it and editing the path in config.php. It seems to work fine (not shure about shares, I donā€™t think I had any active ones). Also, filepaths in the oc_filecache sql table seem to be relative to something (files/ā€¦). In fact, searching the database dump for the old location gets me only one entry which is the one in oc_storages. Besides that, latest documentation for ā€œMigrating to a different serverā€ states ā€œIf you change any paths, make sure to adapt the paths in the Nextcloud config.php fileā€.
https://docs.nextcloud.com/server/latest/admin_manual/maintenance/migrating.html
Therefore I suppose it has changed at some point and moving the data directory is supported now? Or is that a wrong conclusion?

Please see the discussion above that it indeed works without changing the database, but wasnā€™t advised to rely on by a NC engineer: HowTo: Change / Move data directory after installation - #15 by MichaIng

Sad to see that the migration docs are still inconsistent with this. If someone finds time, would be good to send a PR, updating this page to include updating the database storage entry. The bonus of this is that we may have some more NC devs discussing the question :wink: .

1 Like

does anybody knows if i can change the database after moving the datadirectory?
Last week i changed the datadirectory and everything seems to be fine but afterwards i realized that the shares are not working anymoreā€¦
my other solution would be to write a script that goes in the database to change the new fileids to the old onesā€¦ but dont know if this would workā€¦

Strange that (only?) the shares were lost. I wonder if some metadata is associated to the storage index as well. So Nextcloud adds the required new storage to the database by itself, but it is a new row while the old one with related index remains (invalid).

Did you do any changes to the files/shares etc since you moved the data? As when we change the storages table now, while old shares may be recovered, new shares may be lost. The new storages entry would need to be removed to avoid a duplication. The following query will give an overview:

select * from nextcloud.oc_storages;

yes i think that metadatas are affected as wellā€¦

files have been changed since thenā€¦ but we have not created any new sharesā€¦ and if anyone created new shares it is not a problem if they are lost afterwardsā€¦

in oc_storages i have the old storages at the top /nextclouddata and three new storages at the bottom /mnt/nextclouddata/nextclouddata & /mnt/nextclouddata/nextcouddata (typoā€¦) & /mnt/nextclouddata

our new datadirectory is /mnt/nextclouddata/nextclouddata

Okay (after enabling maintenance mode, of course!), first you need to remove the new row, to avoid a duplicate, then replace the old entry:

remove from nextcloud.oc_storages where id='local::/mnt/nextclouddata/nextclouddata/';
update nextcloud.oc_storages set id='local::/mnt/nextclouddata/nextclouddata/' where id='local::/nextclouddata/';

And of course you can remove the additional two entries, if those are not used (anymore):

remove from nextcloud.oc_storages where id='local::/mnt/nextclouddata/nextcouddata/';
remove from nextcloud.oc_storages where id='local::/mnt/nextclouddata/';

Btw, every query should have an output as result, indicating that 1 row was affected, so you know it worked as expected.

Letā€™s see if that helps. Would be a strong argument then to fix the official migration documentation, or otherwise having a review into the code whether shares should be valid for a specific storage entry only or not.

1 Like