nextcloud calender contact backup script

Hello Joulinar,

i have a suggestion about a backup script that i found and i am very happy with.
i use calcardbackup (Bash script to backup calendars and addressbooks from a local ownCloud/Nextcloud installation).
So i have an “always working” backup of my calenders and contacts on all devices.
Maybe it is useful for others as well,

https://codeberg.org/BernieO/calcardbackup

Regards, droogi

My goodness, this is a huge script. It seems to have a large feature set, also SQLite and PostgreSQL support (if Nextcloud was manually installed/configured to use it instead of MariaDB), support for encryption, compression and remote transfer of the backup etc etc.

For those who think it is an overkill:

cat << '_EOF_' > /etc/cron.daily/nextcloud_db_backup
#!/bin/dash

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

# Backup Nextcloud database with numerically sortable timestamp
mysqldump nextcloud > /path/to/backup/$(date '+%Y-%m-%d_%T').sql

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

# Remove old backups as long as more than 10 are present
while [ $(find /path/to/backup -name '*.sql' | wc -l) -gt 10 ]
do
rm -v "$(find /path/to/backup -name '*.sql' | sort -n | head -1)"
done

exit 0
_EOF_
chmod +x /etc/cron.daily/nextcloud_db_backup

So you have a daily running script which creates a whole Nextcloud database backup (instead of calendar/contacts only) and keeps 10 iterations of it.

MichaIng
Should we add this to our NextCloud docs?

what i like in the calcardbackup script:

  1. my backupfiles are .ics calenders and .vcf contacts. I am able to handle these with “any” program without a nextcloud installation.
  2. i was able to restore these data out of my broken nextcloud installation.

i will try Michalngs short backup suggestion, but i dont know yet how to handle the .sql file.

.sql is a full database dump and can be used to restore/recreate your entire NextCloud database on a re-install if needed.

Btw I did a little but important syntax fix in the script above.

Since .sql files are not binary but plain text, it can be also used to restore single tables, individual rows or data. I agree that .ics and .vcf files are pretty handy. So basically the question is what you need. I do the database backups to be able to restore the full or parts of the database, when corrupted, on the server itself, to repair a broken Nextcloud server. Calendar/contacts exports can be done at the clients instead, also when the server is dead. But indeed depending on the client removed/broken data could be synced so that a central export/backup to quickly access/restore those on the clients is nice as well.

coming back to your backup script:
I may do a fresh install of dietpi and would install nextcloud.
How do i restore the sql-dump?
what would happen, if external mounted drives were changed? when i mount them to same /mnt/directory is it ok?

How do i restore the sql-dump?

Theoretically if you create the NextCloud data directory /mnt/dietpi_userdata/nextcloud_data manually and store the SQL dump named as dietpi-nextcloud-database-backup.sql inside, it should be restored automatically on new installation.

what would happen, if external mounted drives were changed? when i mount them to same /mnt/directory is it ok?

Nothing should happen if the new drive contains the very same data and is mounted to very same mount point.