Clone your Nextcloud


Assuming you have two Nextcloud instances (in my example Nextcloud 13.0.5 from here) called

(1) https://source

(2) https://destination

and one folder to change your data (in my example a NFS share called “/share”)

(3) /share

First we will mount the NFS-share on both server:

sudo -s
apt install nfs-common
mount 192.168.2.3:/volume1/share/ /share

On the first server (called “source“) we create the backups to clone your destination server to. Set your (“source“) Nextcloud in maintenance mode and stop the relevant services:

sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
/usr/sbin/service nginx stop
/usr/sbin/service php7.2-fpm stop
/usr/sbin/service redis-server stop

Change into your exchange folder

cd /share

and create the three backups:

  1. Nextcloud www
    tar -cpzf /share/ncserver.tar.gz -C /var/www/nextcloud .
  2. Nextcloud data
    tar -cpzf /share/ncdata.tar.gz -C /var/nc_data .
  3. Nextcloud database
    mysqldump --single-transaction -h localhost -umynextcloud -pmynextcloud nextcloud > /share/ncdb.sql

Disable the maintenance mode and restart all relevant services:

sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
/usr/sbin/service nginx restart
/usr/sbin/service php7.2-fpm restart
/usr/sbin/service redis-server restart

Switch over to your second server called “destination” and stop all relevant services:

sudo -s
/usr/sbin/service nginx stop
/usr/sbin/service php7.2-fpm stop
/usr/sbin/service redis-server stop

Change into the “/share” folder and delete the old webserver and data directories:

cd /share
rm -r /var/www/nextcloud/
rm -r /var/nc_data/

Create new and empty directories

mkdir -p /var/www/nextcloud/
mkdir -p /var/nc_data/

and restore the backup data to the “destination” server:

tar -xpzf /share/ncserver.tar.gz -C /var/www/nextcloud/
tar -xpzf /share/ncdata.tar.gz -C /var/nc_data/

Apply the proper Nextcloud permissions

chown -R www-data:www-data /var/nc_data /var/www

and prepare the destination database server

  1. delete the existing database, create an empty database called “nextcloud” and grant the user “Nextcloud” access:
    mysql -h localhost -uroot -pmynextcloud -e "DROP DATABASE nextcloud; CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; GRANT ALL PRIVILEGES on nextcloud.* to mynextcloud@localhost"
  2. restore the source database to the destination database
    mysql -h localhost -umynextcloud -pmynextcloud nextcloud < /share/ncdb.sql

Disable the maintenance mode and adjust the values for the trusted domain and overwrite.cli.url:

sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
sudo -u www-data php /var/www/nextcloud/occ config:system:set trusted_domains 0 --value=destination
sudo -u www-data php /var/www/nextcloud/occ config:system:set overwrite.cli.url --value=https://destination

Optimize the Redis databse on your destination server

/usr/sbin/service redis-server restart
redis-cli -s /var/run/redis/redis-server.sock <<EOF
FLUSHALL
quit
EOF
sudo -u www-data php /var/www/nextcloud/occ files:scan --all
sudo -u www-data php /var/www/nextcloud/occ files:scan-app-data

and restart the remaining Nextcloud services

/usr/sbin/service php7.2-fpm restart
/usr/sbin/service nginx restart

Your “source” Nextcloud (https://source) was cloned successfully to the “destination” server and is already reachable at

https://destination

with the same credentials and, if configured, with the same second factor etc.



Carsten Rieger

Carsten Rieger

Carsten Rieger is a senior system engineer in full-time and also working as an IT freelancer. He is working with linux environments for more than 13 years, an Open Source enthusiast and highly motivated on linux installation and troubleshooting. Mostly working with Debian/Ubuntu Linux, Nginx and Apache web server, MariaDB/MySQL/PostgreSQL, PHP, Cloud infrastructure (e.g. Nextcloud) and other open source projects (e.g. Roundcube) and in voluntary work for the Dr. Michael & Angela Jacobi Stiftung for more than 6 years.

Leave a Reply

Your email address will not be published. Required fields are marked *