Is there a safe and reliable way to move data directory out of web root?

Be aware, moving the data dir after installation is not supported and may cause issues.

Edit 2017 (as this is a popular topic):

If you have a fresh install, consider reinstalling with your preferred directory location.

Unofficially moving the data directory can be done as follows:

  1. Make sure no cron jobs are running
  2. Stop apache
  3. Move /data to the new location
  4. Change the config.php entry
  5. Edit the database: In oc_storages change the path on the local::/old-data-dir/ entry
  6. Ensure permissions are still correct
  7. Restart apache

However this is not supported and you risk breaking your database. It’d be better if instead you:

  1. Make sure no cron jobs are running
  2. Stop apache
  3. Move /data to the new location
  4. Create a symlink from the original location to the new location
  5. Ensure permissions are still correct
  6. Restart apache

(Note, you may need to configure your webserver to support symlinks)

Which is supported by Nextcloud.

7 Likes

If I encrypt the data directory, is this something I should even be concerned about? There will be personal data stored here that needs to be protected such as tax returns and insurance policies.

The config is not the only place where you need to change the path.
Also in the storages database table.

So if you want to try it, I’d advice to make a backup before, so you can revert to the old status in case it doesn’t work.

@robertb

I’m not sure about encryption. I’ve only tested it with the encryption module turned off. Does the new location of the data directory still have the same permissions/ownership that your old directory had? I apologize if I lead you incorrectly. I was not aware there were database changes required.

@nickvergessen

I am a little confused here. I’ve changed the data directory multiple times without editing the database, and everything continues to work. For testing, the data directory location changes went like this,

1: /private_html/data
2: /private_html/datastorage
3: /private_html/iamgroot
4: /private_html/racoonsarecool

Here are my exact steps.

1: Log out of Nextcloud’s web interface.
2: Move the folder to the new location.
3: Edit config/config.php and adjust the data directory path
4: Run ./occ files:scan --all
5: Log back into the web interface.

My oc_storage table looks like this,

For reference, this is Nextcloud 10. The config.php version says, ‘version’ => ‘9.1.0.16’. Would you mind clarifying the oc_storage changes? I don’t want to be giving people bad advice.

Well basically the ./occ files:scan --all fixes the storage problem. But it also breaks shares, tags, comments, … etc.
Adjusting the local::/.... id entry in storages should allow you to change the path, without breaking all the other info.

@nickvergessen

Thank you. That does clarify things. So, if you wanted to do this, the steps would be,

0: Back everything up first.
1: Disable access to Nextcloud. (maintenance mode)
2: Move the data folder.
3: Edit the database table oc_storage. Change the local::/path/to/data to local::/new/path/to/data.
4: Edit the config.php datadirectory line to reflect the new path.
5: Turn off maintenance mode.

I get the impression this is not supported, but it should work?

Thanks to both of you. I will try your suggested steps this evening and hopefully report success.

exactly
(Post must be at least 20 characters)

I’m at a total loss. I should have a database named nextcloud, but I opened up phpmyadmin and see:

-information_schema
-MySQL
-performance_schema
-phpmyadmin

I’m checking the right server, am I missing something? Sorry for my lack of experience, I may just end up having to reinstall Nextcloud. Hope not, though, it’s working.

@robertb

Hi,

That’s odd. If Nextcloud is working, there has to be a database. Check your config/config.php file for the database type, location, and credentials. It looks something like this,

‘dbtype’ => ‘mysql’,
‘version’ => ‘9.1.0.16’,
‘dbname’ => ‘your database name’,
‘dbhost’ => ‘your database server’,
‘dbport’ => ‘’,
‘dbtableprefix’ => ‘oc_’,
‘dbuser’ => ‘login name’,
‘dbpassword’ => ‘password’,

That should help you locate it.

EDIT* I see the database type is sqlite3 instead of MySQL, I’m guessing this probably has something to do with it?


It’s REALLY odd. I followed these installation instructions:

https://docs.nextcloud.com/server/10/admin_manual/installation/index.html
=> Manual Installation in Linux

/var/www/nextcloud/config/config.php doesn’t even specify a database name. I know it’s working though because I added maintenance mode, switched it to “true”, and the site properly locked me out.

<?php $CONFIG = array ( 'instanceid' => 'zzzzzzz', 'passwordsalt' => 'zzzzzzzzzz', 'secret' => 'zzzzzzzzzzzzzzzzzz', 'trusted_domains' => array ( 0 => 'xxx.xxx.xxx.xxx', 1 => 'share.mydomain.com', ), 'datadirectory' => '/var/www/nextcloud/data/', 'overwrite.cli.url' => 'http://xxx.xxx.xxx.xxx/nextcloud', 'dbtype' => 'sqlite3', 'version' => '9.1.0.16', 'logtimezone' => 'UTC', 'installed' => true, 'mail_smtpmode' => 'smtp', 'mail_from_address' => 'user', 'mail_domain' => 'mydomain.com', 'mail_smtpauthtype' => 'LOGIN', 'mail_smtpauth' => 1, 'mail_smtphost' => 'mail.mydomain.com', 'mail_smtpsecure' => 'tls', 'mail_smtpname' => 'user@mydomain.com', 'mail_smtppassword' => 'mypassword', 'mail_smtpport' => '25', "maintenance" => false, ); I also did a SHOW DATABASES and received exactly the same as what phpmyadmin was showing: MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | phpmyadmin | +--------------------+ 4 rows in set (0.00 sec) But Nextcloud is honestly working just fine. This seems crazy. ANY ideas on what might be happening?

It’s using sqlite instead of Mysql/MariaDB. I think the database will be located in your $data directory and will be a file ending with .db. I’m not entirely sure, as I’ve never configured sqlite before.

I just realized that. Sigh… I think I’ll just try to reinstall

Success!

So with a little digging, I learned that I could convert my database from sqlite3 to MySQL using the following:

https://docs.nextcloud.com/server/10/admin_manual/configuration_database/db_conversion.html

At that point my Nextcloud database was accessible through phpmyadmin. Then I followed grouchysysadmin’s steps above:

0: Back everything up first.
1: Disable access to Nextcloud. (maintenance mode)
2: Move the data folder.
3: Edit the database table oc_storage. Change the local::/path/to/data to local::/new/path/to/data.
4: Edit the config.php datadirectory line to reflect the new path.
5: Turn off maintenance mode.

In my case, I move the data directory to /etc/nextclouddata/nextcoud/data. When trying to load my login page for the first time, I got the following error:

Data directory (/etc/nextclouddata/nextcloud/data/) is invalid. Please check that the data directory contains a file “.ocdata” in its root.

Running…

chown -R www-data:www-data /etc/nextclouddata/nextcoud/data

…fixed that error and everything looks to be running perfectly again.

Many thanks to @grouchysysadmin and @nickvergessen for helping me through this.

2 Likes

will there be an procedure to do that nativly?

2 Likes

running nextcloud 11.0.1 stable PHP7.0 and MySQL 5.5.53 on webmin/virtualmin raspberry pi3 with debian 8.0

today I moved my data folder to an USB disk ext4

mounted the disk and changed the path accordingly in config.php

‘datadirectory’ => ‘/home/domain/public_html/data’, into ‘datadirectory’ => ‘/mnt/disk/data’,

then using PHPMyAdmin changed the path in OC_STORAGES

(local::/home/domain/public_html/data/ into local::/mnt/disk/data/) just samples

It works like a charm and gave my NextCloud a big boost… not only in space but also in speed.

Please note, in the database the path has a trailing / (forward slash)

1 Like

please guide me how to modify oc_storages as i am unable to find anything as’local’
i am trying t add it by webmin.
Please guide

If you do not have PHPmyAdmin running on your webmin, you can always manage the database under servers->mysql database-> select your db->manage db->select oc_storages in edit db->click view data->select the row with the local:: and edit->set the new value and save.

1 Like

So I’ve been struggling with moving my data directory, as well, in NC v11. My vm test went well, then a trial run on real host to tinker with; and finally, a working, live install…and then I realized I didn’t give enough room for data storage…I have a 2TB external drive.

I have made several attempts, but ran into all the same issues you have with permissions…any idea if this will work in v11?
(This instance is very young: it could be wiped and reinstalled, pointing to the new storage, but I’d like to figure out how to move the data manually…at some point in the future, somebody is going to have to upgrade their storage… might as well find out how).

THNX!

When moving the data folder to an external drive, make sure you used the correct user to mount the drive…
(Even format as the user depending on the filesystem.)