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

Leave a Reply

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