Today I was playing with CalDavSynchronizer. I tested different configuration options for synchronization and ended up with some of my calendar entries deleted. I knew there were a couple of other Nextcloud users on my server so I checked whether it is suitable to restore the calendar from last night’s backup.
This is written based on my own experience so YMMV. This has been tested on Nextcloud 16.0.3 and MariaDB 10.4.6.
First I thought that I could use Nextcloud’s recycle bin. Unfortunately, this helps with files only. The recycle bin for calendar events is under construction: https://github.com/nextcloud/server/issues/1662
As the recycle bin couldn’t be used, my next option was to restore from database backup. The dumps are done using –all-databases option, so first I had to separate the needed tables (oc_calendarobjects, oc_calendars and oc_calendarsubscriptions) to restore. I found the idea here: https://stackoverflow.com/questions/1013852/can-i-restore-a-single-table-from-a-full-mysql-mysqldump-file
So to extract the tables to different files, I ran:
sed -n -e '/DROP TABLE.*`oc_calendarobjects`/,/UNLOCK TABLES/p' all_databases_dump.sql > oc_calendarobjects_tabledump.sql
This was repeated for all the three tables.
Next before making any changes to database, I took another dump from the database so that I have something to return to in case of a failure.
As a last step just restore the dump as usual:
mysql --user databaseuser --password --database=nextcloud_database < oc_calendarobjects_tabledump.sql
Note that you have to use database-parameter as the extracted dumps don’t contain the “use database …”. Other option is to add it manually in the beginning of each dump.