Migrating Nextcloud



Assumes Nextcloud instance is installed on DietPi

Run these commands on old server machine

Stop the nginx service:

sudo systemctl stop nginx.service

Put Nextcloud into maintenance mode:

cd /var/www/nextcloud
sudo -u www-data php occ maintenance:mode --on

Backup the folders

DATE=$(date +"%Y%m%d")
sudo rsync -aAX /etc/nginx /home/jas/nginx-backup_$DATE
sudo rsync -aAX /var/www/nextcloud /home/jas/nextcloud-dir-backup_$DATE
sudo rsync -aAX /mnt/dietpi_userdata/nextcloud_data /home/jas/nextcloud-data-backup_$DATE

Dump the MariaDB database:

sudo mysqldump --single-transaction --default-character-set=utf8mb4 -h localhost -u [username] -p [password] nextcloud > /home/jas/nextcloud-db-backup_$DATE.sql

Rsync the files over to the new server machine:

sudo rsync -aAX \
    /home/jas/nginx-backup_$DATE \
    /home/jas/nextcloud-dir-backup_$DATE \
    /home/jas/nextcloud-data-backup_$DATE \
    /home/jas/nextcloud-db-backup_$DATE.sql \


Run these commands on the new server machine

Assuming the web server is stopped.

Move the nextcloud-dir and nextcloud-data directories to their correct locations:

# first, ensure the default directories are removed:
sudo rm -rf /etc/nginx
sudo rm -rf /var/www/nextcloud
sudo rm -rf /mnt/dietpi_userdata/nextcloud_data

sudo mv nginx_$DATE /etc/nginx

sudo mv nextcloud-dir-backup_$DATE /var/www/nextcloud

sudo mv nextcloud-data-backup_$DATE /mnt/dietpi_userdata/nextcloud_data
# or, if not using DietPi:
sudo mv nextcloud-data-backup_$DATE /var/www/nextcloud/data

sudo chown -R www-data:www-data /var/www/nextcloud
# if using DietPi:
sudo chown -R root:root /mnt/dietpi_userdata/nextcloud_data

sudo chown -R root:root /etc/nginx

Create the nextcloud database in MariaDB:

sudo mysql -h localhost -u root -p [password] -e "DROP DATABASE nextcloud"
sudo mysql -h localhost -u root -p [password] -e "CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
sudo mysql -h localhost -u root -p [password] nextcloud < /home/jas/nextcloud-db-backup_$DATE.sql

Take Nextcloud out of maintenance mode:

You may have to change the ‘oc_admin’ database user password for occ commands to work.

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

(Re)Start the services:

sudo systemctl restart nginx mariadb redis-server php7.4-fpm

Enter your instance's address