Migrating to a New Server on Docker Nextcloud


Migrating your Docker Nextcloud to a new server, from backup and restoring.

Table of Contents

Migrating to a New Server

For official documentation you can see official nextcloud pages for migrating to new server.1 Here, I’m going to show how to migrate under docker environment. Assuming you have set up your new system, and nextcloud using docker is installed. Initially, you have to back up your data and database on your old system, it consists of 4 things:

  • Config Folder
  • Data Folder
  • Theme Folder
  • Database

Depending on your setup, whether you put your volume using default volume mounts, or bind mounts. If you put default configuration under docker documentation, using volume mounts the nextcloud app created volumes under /var/lib/docker/volumes/, it consisted of config, data and theme folder. If you configured it using bind mounts, under different directory, for example /path/to/your/dir you can backup accordingly.

Backup

Backup documentation also available under official nextcloud page for backup2

1. Put your old system into maintenance mode

There are two methods of putting your nextcloud into maintenance mode, using php occ or edit directly your config.php file.

docker exec --user www-data nextcloud php occ maintenance:mode --on

or you can edit your config.php under your data directory or under default nextcloud data

sudo nano /var/lib/docker/volumes/nc_nextcloud/_data/config/config.php

add this on your config.php

 "maintenance" => true,

2. Backup your data

All of my data folders are under two volumes, nextcloud/data. Backup using rsync to your any backup directory folder to make it easier to navigate. I’m using sudo then the owner of the original folder is not changing based on the user, in this case www-data.

  • Backup config, data, and theme folder
sudo rsync -Aavx /var/lib/docker/volumes/nextcloud/data/ /your/backup/folder/nc_`date +"%Y%m%d"`

3. Backup your database

Here I’m using MySQL/MariaDB, you can backup according to your database choice.

docker exec nextcloud sh -c 'exec mysqldump --single-transaction -h localhost -u (username) -p(db-password) (db_name)' > /path/your/backup-folder/db_`date +"%Y%m%d"`.bak

nextcloud is container name, if you don’t name it, you can use docker ps command to get your container name, username and db_name are supposed to be nextcloud as well. It’s important Put your db-password after dash - and p before your actual password, without a space.

4. Copy your backup folder to your new system

I’m using rsync and ssh to my new server. -p 2222 is just the example if you are using port 2222.

sudo rsync -avz -e "ssh -p 2222" /your/backup/folder/ user@remote:/path/to/your/destination

We have finished what we have to do on our old server. Next we’re moving to our new server.

Restore

Assuming you have copied your backup folder to your new server, installed docker, and nextcloud on your new server, now we can proceed. You can see nextcloud official docs for restoring backup.3 The example here is where I’m gonna put my backup folder to

 nextcloud/
 +--data/
    +--config/
    +--data/
    +--theme/

1. Restore folder

  • Restore according to your setup, restore nc_nextcloud volumes folder
sudo rsync -Aax /your/backup/folder/nc_`date +"%Y%m%d"` /path/to/your/volumes/nextcloud/data/

2. Restore database

docker exec nextcloud sh -c 'exec mysql -h localhost -u (username) -p(db-password) (db-name)' < /your/backup/folder_$xxx.bak

nextcloud is container name, -h argument is your host, localhost is the default, username and db_name are supposed to be nextcloud as well. Yet again, put your db-password after dash - and p before your actual password, without a space.

3. Synchronising with clients after data recovery

Check the config.php file of the OLD server to see if it has the data-fingerprint set to a non-empty value. If this is the case, make sure to also run the maintenance:data-fingerprint command on the NEW server, by doing

docker exec --user www-data nextcloud php occ maintenance:data-fingerprint

Finally, we can turn off maintenance mode

docker exec --user www-data nextcloud php occ maintenance:mode --off

4. Make sure to see the maintenance mode notice

While still having Nextcloud in maintenance mode (confirm!) and BEFORE changing the CNAME record in the DNS start up the database, Web server / application server on the new machine and point your web browser to the migrated Nextcloud instance. Confirm that you see the maintenance mode notice, that a logfile entry is written by both the Web server and Nextcloud and that no error messages occur. Then take Nextcloud out of maintenance mode and repeat. Log in as admin and confirm normal function of Nextcloud. See the official docs for migrating. After all set, you can change your CNAME or point your domain to your new server.


  1. Nextcloud Official docs, Migrating to New Server↩︎

  2. Nextcloud Official docs, Backup↩︎

  3. Nextcloud Official docs, Restoring Backup↩︎


Posted by : Tags:  Notes , Security , Web , Self-Hosting Migrating to a New Server on Docker Nextcloud