How To Backup Nextcloud

https://docs.nextcloud.com/server/stable/admin_manual/maintenance/backup.html

https://help.nextcloud.com/t/backup-nextcloud-with-seperate-data-folder-and-the-database/5841/2

good info on rsync:
http://webgnuru.com/linux/rsync_incremental.php

more info on mysql backups:
https://www.liquidweb.com/kb/how-to-back-up-mysql-databases-from-the-command-line/

Restoring Nextcloud from backup:
https://kenfavors.com/code/how-to-restore-a-nextcloud-backup/

To backup an Nextcloud installation there are four main things you need to retain:

  1. The config folder
  2. The data folder
  3. The database
  4. The theme folder

Backup Folders

Simply copy your config, data and theme folders (or even your whole Nextcloud install and data folder) to a place outside of your Nextcloud environment.

1. Copy the data directory (if outside of the nextcloud directory):

$ sudo rsync -Aax /var/www/data/ /media/ncbackup/nextcloud-data_`date +"%Y%m%d"`/

2. Copy the entire Nexcloud install, which includes the required config and theme folders:

$ sudo rsync -Aax /var/www/html/nextcloud/ /media/ncbackup/nextcloud-dirbkp_`date +"%Y%m%d"`/

3. Backup MySQL/MariaDB:

$ mysqldump --single-transaction -h [server] -u [username] -p[password] [db_name] > nextcloud-sqlbkp_`date +"%Y%m%d"`.bak

an example below (the ‘-h' flag is unncessary if the database is on the same server). Also, ‘–lock-tables' is included per the link below:

$ mysqldump --lock-tables -u root -p nextcloud > /media/ncbackup/database/nextcloud-sqlbkp_`date +"%Y%m%d"`.bak

There are some answers to common issues in this link:
https://help.nextcloud.com/t/backup-nextcloud-with-seperate-data-folder-and-the-database/5841/7

If you get a ‘Permission denied' error when trying to backup the database, it is because the current user account you are using does not have the right permission to write to the destination path. Change permissions like this (substituting ‘username'):

$ sudo setfacl -R -m "u:username:rwx" /media/ncbackup

also found at How To Add User Permissions To A Folder in Ubuntu

Related

How to manually upgrade Nextcloud

https://docs.nextcloud.com/server/stable/admin_manual/maintenance/manual_upgrade.html

and

How To Restore A Nextcloud Backup


Hosting

Develop and scale your apps globally with DigitalOcean and/or Vultr – or use shared hosting with no server maintenance required at iMarketHost.com.

Installation & Maintenance

If you would like to get this app installed, maintained or need training, Contact Me to get current rates.

2 thoughts on “How To Backup Nextcloud

  1. Hey Ken:
    I looking at your backup solution for Nextcloud. Looks pretty straightforward. I’ve never dumped a db before but we’ll see how it goes. I’m not expecting too many issues. (that btw are, at least for me, famous last words. Ha) Additionally, I have a few questions.
    Before doing any of this should Nextcloud be put in maintenance mode?
    For the rsync section, I read your sidebar url reference on the db dump. The sidebar refers to to command having the delete option of rsync active but I’m not seeing that in the code above. Am I missing something? I’m figuring I should add the –update–delete flags but I don’t want to duplicate options.
    Lastly, since it fairly straightforward, have you written a script for this or are you working strictly from cron? The cron command seems like it could get fairly lenghy.

    1. Hello Kevin, the db dump is pretty simple after you do it for the first time and it’s a good way to dump other databases when you don’t have access to a GUI to do it (e.g. phpmyadmin or adminer). (1) It is not necessary to put Nextcloud in maintenance mode before doing a backup although you can on an active/busy install by setting the maintenance parameter to `true` in the config file. (2) Sorry, but I did not see a reference to using an `-update-delete` flag and can verify that it is not needed anyway. (3) For my particular use case, I only do db dumps now and since I sync Nextcloud using the desktop client, I don’t feel the need to rsync my data files anywhere else – for now. I haven’t written a script but setting up a cron for a db dump would simply be something like:

      $ crontab -e

      and adding something like this, which would run everyday at 2am:

      * 2 * * * mysqldump --lock-tables -u root -p nextcloud > /media/ncbackup/database/nextcloud-sqlbkp_`date +"%Y%m%d"`.bak
      

      Good luck!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.