How To Manually Move a Data Directory

Introduction

If you need to move your ownCloud data directory from its current location to another location — without using a symbolic link — this section steps through how to do so.

Assumptions

This guide assumes that:

  • The current folder is: /var/www/owncloud/data

  • The new folder is: /mnt/owncloud/data

  • You’re using Apache as your webserver

  • The ownCloud database name is owncloud

Please change the paths above to reflect your environment.

Description of Steps

The following steps are necessary to move the data directory.

  1. Stop Apache

  2. Enable maintenance mode for your instance

  3. Use Rsync to sync the files from the current to the new directory

  4. Double-check the directory permissions on the new directory

  5. Change the ownCloud configuration to point to the new data directory

  6. Disable maintenance mode for your instance

  7. Restart Apache

Look at each section below for a detailed description.

Apache and Rsync

To save time, here are the commands which you can copy/paste for Apache and rsync:

sudo service apache2 stop

sudo service apache2 start

sudo rsync -avz /var/www/owncloud/data /mnt/owncloud
Check your commands for how to start or stop your webserver if you are not on Ubuntu/Debian.

Enable and Disable Maintenance Mode

It is necessary to enable maintenance mode to avoid running cron jobs. To enable maintenance mode, run the following command.

sudo -u www-data php occ maintenance:mode --on

To disable maintenance mode of your instance run the following command:

sudo -u www-data php occ maintenance:mode --off

Fix Hard-coded Database Path Variables

Open a database command line client to enter database commands and activate your ownCloud database.

use owncloud;

Update the oc_storages Table

Run the SQL below:

UPDATE oc_storages
  SET id='local::/mnt/owncloud/data/'
  WHERE id='local::/var/www/owncloud/data/';

Update the oc_accounts Table

You next need to update the home column in the oc_accounts table. This column contains the absolute path for user folders, e.g., /mnt/owncloud/data/my_user/files.

If a user does not have the path already set, you have to identify the users id and set the path with the following command, user by user. This example assumes the user name is my_user and their id is 1.

Run the SQL below:

UPDATE oc_accounts SET home='/mnt/owncloud/data/my_user/files'
  WHERE id=1;

For all users who already have a path like /var/www/owncloud/data/ in your database, you can use the REPLACE command:

UPDATE oc_accounts
  SET home = REPLACE(
    home,
    '/var/www/owncloud/data/',
    '/mnt/owncloud/data/'
  );

For more information follow the complete MySQL REPLACE command syntax.

Please don’t copy and paste this example verbatim — nor any of the others. They are examples only.

Update the oc_jobs Table

The next area to check is the oc_jobs table. The logrotate process may have hard-coded a non-standard (or old) value for the data path. To check it, run the SQL below and see if any results are returned:

SELECT * FROM oc_jobs
  WHERE class = 'OC\Log\Rotate';

If results are returned, run the SQL below to update them, changing the id value as appropriate.

UPDATE oc_jobs
  SET argument = REPLACE(
    argument,
    '\\/var\\/www\\/owncloud\\/data\\/',
    '\\/mnt\\/owncloud/data\\/'
  )
  WHERE id = <id of the incorrect record>;
The old data path will be written with \/. Therefore you must add one, additional, backslash, like this: \\/.

Fix the Application Settings

Individual apps may reference the data directory separately from the core system configuration. For those apps, you have to change the configured path. Run the following command to list app configs.

sudo -u www-data php occ config:list

Here is an example of the output which you may see:

{
    "apps": {
        "fictitious": {
            "enabled": "yes",
            "installed_version": "2.3.2",
            "types": "filesystem",
            "datadir": "/var/www/owncloud/data"
        }
    }
}

In the example above, the app "fictitious" sets the data directory to /var/www/owncloud/data. Change this value by using the following command:

sudo -u www-data php occ config:app:set --value /mnt/owncloud/data fictitious datadir
You have to repeat this for all apps found defining the data directory as key.

Fix the config.php Settings

To fix the config.php settings:

  1. Change the datadirectory key in your config.php to the new path. To do so, start an editor of your choice and open /var/www/owncloud/config/config.php

  2. Change the value of the key from 'datadirectory' ⇒ '/var/www/owncloud/data', to 'datadirectory' ⇒ '/mnt/owncloud/data',.