Nextcloud backup and restore

Backup Nextcloud

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

Please substitute ubuntuusername and the database password nextcloud with yours. Switch into sudo mode:

sudo -s

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/ncserver_`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 -unextcloud -pnextcloud nextcloud > /home/ubuntuusername/ncdb_`date +"%w"`.sql

5. Turn maintenance mode off:

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


6. A simple backup example ( based on an Ubuntu OS:

It won’t backup your personal (user) data, but you will be able to restore the entire Nextcloud instance.
Please don’t forget to backup your perosonal (user) data either!

sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
echo "START: $(date)"
if [ -d "$FOLDER" ];
echo "Copying $FOLDER..."
rsync -AaRx --delete $FOLDER $BACKUP_STORE
echo "Skipping $FOLDER (since it does not exist)"
cp /etc/fstab $BACKUP_STORE/etc/
mysqldump --single-transaction -h localhost -unextcloud -pnextcloud nextcloud > $BACKUP_STORE/ncdb_`date +"%w"`.sql
mysql -unextcloud -pnextcloud -e "SELECT table_schema 'DB',round(sum(data_length+index_length)/1024/1024,4) 'Size (MB)' from information_schema.tables group by table_schema;"
mkdir -p $(dirname $ARCHIVE_FILE)
tar -cpzf $ARCHIVE_FILE .
echo "Serverbackup size: $(stat --printf='%s' $ARCHIVE_FILE | numfmt --to=iec)"
rm -f $BACKUP_STORE/*.sql
rm -f $BACKUP_STORE/etc/fstab
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
echo "END: $(date)"
exit 0

Restore Nextcloud

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

We demonstrate a restore exemplarily from a Monday backup (_1) what have to be substituted properly. Switch into sudo mode:

sudo -s

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.3-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/ncserver_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 database:

– Delete the old database

mysql -h localhost -uroot -pnextcloud -e "DROP DATABASE nextcloud"

– Create an empty one

mysql -h localhost -uroot -pnextcloud -e "CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"

– Grant access

mysql -h localhost -uroot -pnextcloud -e "GRANT ALL PRIVILEGES on nextcloud.* to nextcloud@localhost"

– Restore the database

mysql -h localhost -unextcloud -pnextcloud nextcloud < /home/ubuntuusername/ncdb_1.sql

7. Restart services:

/usr/sbin/service php7.3-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


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 7 years.

19 Responses

  1. Eric says:

    Hi Carsten,

    I followed your tutorial about the install of Nextcloud and was now trying to understand the backup tutorial.
    First I am not a linux pro, but I’m trying to understand and use linux.

    I can follow and understand the manual backup steps and the manual restore steps of your tutorial.
    But when trying to read the steps in your proposed file, you did loose me.
    First, reading your backup script pointed me to re-read the text explaining the manual steps of taking a backup:
    “Ensure to have regulary backups from _0 (Sunday) to _6 (Saturday) of your Nextcloud, including at least:
    – the webfolder: /var/www/nextcloud
    – the datafolder: /var/nc_data/appdata_ !!!
    – and the database: nextcloud.”

    the datafolder: /var/nc_data/appdata_
    But in your manual step, Backup the datafolder, you are taking everything, not only a appdata_
    # tar -cpzf /home/ubuntuusername/ncdata_`date +”%w”`.tar.gz -C /var/nc_data .

    So when following the manual steps, I have finally
    – a tar file with the full content of /var/www/nextcloud folder (ncserver_…tar.gz)
    – a tar file with the full content of /var/nc_data (ncdata_….tar.gz)
    – a sql dumpfile with the full content of the database nextcloud (ncdb_….sql)
    The proposed restore steps will restore the whole dirtree.

    ==> so there is something wrong between the explaining text and the executed commands

    On the other side, what I understand, the backupscript will only take /var/nc_data/appdata_* ?
    After running the backupscript, I have:
    – a tar file ncserver_…tar.gz with several folders, not only the www nextcloud folder and the nextcloud sql dump file
    – a tar file ncdata_…tar.gz with only the content of the appdata_* folders?

    Can you explain more in detail what the script is exactly doing? Why you are only taken these appdata_* folders?
    I didn’t find in the Nextcloud docs more information about these folders.

    And maybe a suggestion for a following blog article ;-), the necessary restore steps when you did have a crash of the server and you want to recover the nextcloud instance with the backup files taken by the script.


  2. Stefan says:

    Hi Carsten,

    Ich trage mich mit dem Gedanken, eine NC Neuinstallation mit PostgreSQL zumachen. Kann man den msqldump in PostgreSQL einspielen und wenn ja, wie ist dann die Befehlszeille für ” Restore the database”?


  3. Jens says:

    Hi Carsten,
    thanks for your nice blog and tutorials. Regarding the backup script, to avoid having the username and password hard coded in the script, you can use a .my.cnf file in the home directory of the root user with the following content structure:



    After this is done, we should do a “chmod 600 .my.cnf” and the mysql commands need to be changed to:

    mysqldump –defaults-extra-file=/root/.my.cnf –single-transaction -h localhost nextcloud > $BACKUP_STORE/ncdb_`date +”%w”`.sql
    mysql -e “SELECT table_schema ‘DB’,round(sum(data_length+index_length)/1024/1024,4) ‘Size (MB)’ from information_schema.tables group by table_schema;”

    the mysql command automatically looks for a “.my.cnf”, so there is no extra parameter needed.
    Of course the username and password is still hard coded, but separated now to a file, which is accessible from the root user only. So the script can also be located outside (like /usr/local/bin) and is not leaking any secrets.

    If someone is using Mysql instead of MariaDB (doesn’t have this command / so incompatible) another possibility would be to create the file with “mysql-config-editor”, which also creates the .my.cnf file, but in an obfuscated way. See

  4. isaac says:

    hello Carsten,

    I seem to be getting this error… when I ran the script

    ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

    but the .gz files are in their respective directories

    thanks again

  5. Marcus says:

    Hi Carsten,
    first I want to thank you for all of your tutorials/blog posts. Those tutorials help me a lot.

    Regarding the backup-Script I have an “issue” with it. I have to backup a Nextcloud instance with about 70 GB and a lot of Image-Files.
    I Tried to do the backup via a similar solution like your solution, but it took hours creating a zip/tar.

    The plan is with rsync transfer the nextcloud-data and database to a NAS (reachable in the network). This might take too long too.

    Do you know a solution which might be faster? I thought of running the nextcloud server as a vm/docker image and “just” backup the vm image.

    Thanks a lot for your support and help.

  6. tito says:

    Hi, nice tutorial
    But when I tried to make a DATABASE backup.. shows this error:

    mysqldump: Got error: 1045: “Access denied for user ‘nextcloud’@’localhost’ (using password: YES)” when trying to connect

    Do you know what is the problem? Or how can I solve it?

    • How did you try to perform the db-backup? Please call mysql -unextcloud -p -hlocalhost. Are you able to connect to your databse server?

    • tito says:

      no matter, I resolved. Thank anyway.
      There some way the backup and crontab works together?. what would be the script? for example: every week. Nice tutorial and very profesional. thanks.

  7. Sebastien says:

    can you tell me what is the difference between backup_store and archive_store please?

    • Hi Sebastien,
      “BACKUP_STORE” is the working directory for rsync to sync all folders configured “FOLDERS_TO_BACKUP”
      “ARCHIVE_STORE” is the directory where the backups will be stored to: “/home/ubuntuusername/backup_dir”
      It is just an example, feel free to consolidate or simplify the backup script to your need. Cheers, Carsten

  8. Alfred says:

    Hi! Is it possible to use this manual to migrate an ARM based instance of Nextcloud /Odroid C2/ to a new x86 server? Thanks!

Leave a Reply

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