Nextcloud backup and restore


Backup and Restore


Backup Nextcloud

Ensure to have regulary backups from _0 (Sunday) to _6 (Saturday)  of your Nextcloud, including:
– the webfolder: /var/www/nextcloud
– the datafolder: /var/nc_data
– and the database: nextcloud.

Please substitute ubuntuusername with your Ubuntu username.

1. Turn maintenance mode on:

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

2. Backup the webfolder:

tar -cpzf /home/ubuntuusername/nc_`date +"%w"`.tar.gz -C /var/www/nextcloud .

3. Backup the datafolder:

tar -cpzf /home/ubuntuusername/ncdata_`date +"%w"`.tar.gz -C /var/nc_data .

4. Backup the database:

mysqldump --single-transaction -h localhost -u nextcloud -p nextcloud > /home/ubuntuusername/ncdb_`date +"%w"`.sql

5. Turn maintenance mode off:

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

Done.


Restore Nextcloud

A restore of your Nextcloud even consists of
– the webfolder: /var/www/nextcloud
– the datafolder: /var/nc_data
– the database: nextcloud.

We demonstrate a restore exemplarily from a Monday backup (_1) what have to be ammended properly.

1. Turn maintenance mode on:

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

2. Stop services:

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

3. Delete the old directories:

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

4. Create the directories new again:

mkdir -p /var/www/nextcloud/
mkdir -p /var/nc_data/
tar -xpzf /home/ubuntuusername/nc_1.tar.gz -C /var/www/nextcloud/
tar -xpzf /home/ubuntuusername/ncdata_1.tar.gz -C /var/nc_data/

5. Apply the permissions:

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

6. Restore the datase:

– Delete the old database

mysql -h localhost -u nextcloud -p -e "DROP DATABASE nextcloud"

– Create an empty one

mysql -h localhost -u nextcloud -p -e "CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"

– Grant access

mysql -h localhost -u nextcloud -p -e "GRANT ALL PRIVILEGES on nextcloud.* to nextcloud@localhost"

– Restore the database

mysql -h localhost -u nextcloud -p nextcloud < /home/ubuntuusername/ncdb_1.sql

7. Restart services:

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

8. Fingerprint:

After restoring a backup of your data directory or the database, you should always call maintenance:data-fingerprint once. This changes the ETag for all files in the communication with sync clients, allowing them to realize a file was modified.

sudo -u www-data php /var/www/nextcloud/occ maintenance:data-fingerprint

9. Turn maintenance mode off:

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

Done.


Carsten Rieger

Leave a Reply

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