Scripts for backup/restore


#1

Besides the explanations about backup and restore in the Nextcloud documentation, I’ve created some bash scripts for easy backup/restore of your Nextcloud instance.

The Scripts can be found on GitHub:

Using the scripts

  • The backup script will backup the file directory (e.g. /var/www/nextcloud) and the data directory (e.g. /var/nextcloud_data) as tar.gz files. The database backup will be an SQL dump file (*.sql).
  • The backups will be stored in some mounted location (e.g. /mnt/Share/NextcloudBackups). This could be anything like a flash drive, external hard drive or a network share.
  • To backup your Nextcloud, simply call NextcloudBackup.sh. This will backup Nextcloud to a folder with time stamp (e.g. /mnt/Share/NextcloudBackups/20170910_132703).
  • Use the time stamp (20170910_132703) as parameter for the restore script, e.g. simply call NextcloudRestore.sh 20170910_132703.

Notes

  • Do not use the scripts out of the box: These need to be customized for your specific Nextcloud instance. Everything which needs to be customized is marked with TODO in the script’s comments.
  • The scripts assume that your data directory is located outside of your web root.
  • If your data directory is located inside the web root, the scripts need to be altered, otherwise the data directory will be copied twice.

Feel free to give it a try. Suggestions and/or contributions are welcome!


Creating Nextcloud backups
How-to/FAQ WIKI
#2

Thank you for putting this together :slight_smile:


#3

Nice :+1:

Could be easily extended to also support apache webserver (service apache2 stop/start), where some check could be implemented or the error messages about missing service could just be suppressed. Or you add some variable about the webserver service name to adjust at the top of the scripts.

Dependent on the size/usage of your data folder, keeping many backups of it, could fast fill your backup drive. I personally just keep one single backup of it. Internally trash and versions are a sort of additional backup for changing files (text/docs vs. images). So e.g. an additional variable for the amount if backups just for the data folder could be implemented.

But anyway thank you very much for sharing this :slight_smile:!


#4

Hey @MichaIng thanks for the hint. I totally forgot to mention that the scripts assume nginx as web server.
I’ve changed it the way that the service name of the web server can now be specified.

The only thing I’m uncertain about is keeping different amounts of file/data backups. Will older data directory backups still work when it doesn’t fit to the rest of the backup?


#5

In case you choose to keep e.g. just one data folder backup but e.g. 10 database and nextcloud folder backups, there would be in case the data folder backup same age or newer :wink: . Yeah, in case you choose an older database backup, you would loose e.g. new comments, tags, shares and such things related to the files, but at least the files itself would still be there. To bring the database to the newest stage of the files folder (and thus to make the newer files show up in files app), you simply would need to do some occ files:scan --all and occ files:cleanup. I didn’t see any situation, where an older database restore did not work with a newer (different) data folder.
The nextcloud folder again is actually totally irrelevant, as you can always just download your version again. Only the config.php and in case .htaccess/.user.ini would need to be restored/reconfigured. But the nextcloud folder also does not take any drive space, thus many backups do not hurt at all :laughing:.

I personally just keep 1 data folder backup against drive crashes/data loss and create/update it every night. I assume that I would recognize some drive crash during the day, which would lead to broken nextcloud access in most cases. Otherwise trash/versions are there to restore accidently changed/removed data.

Database corruption sometimes shows up days later, sometimes due to slowly breaking sd card on SBCs (was just the case for me some months ago). Also I had the case, that the database dump just stopped at a certain table and some days later I faced the last dumps just incomplete. So for database it is nice to keep some more backups here, also because they do not take much space.

Of course keeping backups of all 3 parts together several times is a great situation. I just guess that many people/organizations are not able (do not want to invest into this amount of drive space) to keep the size of e.g. 10 data folders on their drives. There it would be better for my point if one can just keep 1 or 2 data folder backups, to still keep 10 or 20 database backups beside. Again also 10 to 20 nextcloud folder backups do simply not hurt there.


#6

Hello,

The script works fine, but after it finishes with backup of the nextcloud data it freezes on the console, but the progress is still running meaning, it also finishes the backup of the sql and setting maintenance-mode off again.
Do you why that happens?


#7

Hi,

have you tried to run the commands of the script one by one on the console? Can you reproduce the problem here?


#8

So just ran the script again, it looks to be printing its progression on the console again. That is weird.


#9

The backup is needful thing for every system.There are many way to take backup but it is depend what type of data which you want to back. Your scrip for back is very useful which I use backup for my website https://applesupportnumber.net. If It work then I use for my other website.