Restore a single calendar from a Nextcloud backup
March 29, 2017
I expirienced an issue with one of my CalDAV clients, which deleted some events from my calendar. So I restored the data of that one calendar from the latest backup before the malicious client got connected.
To achieve this only seven little steps are necessary:
-
Remove unrelated calendars from the backup
DELETE FROM oc_calendars WHERE id NOT IN (1, 5, 3, 12, 22, 33, 34); DELETE FROM oc_dav_shares WHERE type <> 'calendar' OR resourceid NOT IN (1, 3, 5, 12, 22, 33, 34); DELETE FROM oc_calendarobjects WHERE calendarid NOT IN (1, 3, 5, 12, 22, 33, 34); DELETE FROM oc_calendarchanges WHERE calendarid NOT IN (1, 3, 5, 12, 22, 33, 34);
-
Save the rescued calendar alone
Now backup (data only) the content of the 4 tables as
rescue.sql
. -
Backup the calendars live data
CREATE TABLE backup_calendars AS SELECT * FROM oc_calendars;
-
Delete the affected calendars on the live database
DELETE FROM oc_calendars WHERE id IN (1, 3, 5, 12, 22, 33, 34); DELETE FROM oc_dav_shares WHERE type = 'calendar' AND resourceid IN (1, 3, 5, 12, 22, 33, 34); DELETE FROM oc_calendarobjects WHERE calendarid IN (1, 3, 5, 12, 22, 33, 34); DELETE FROM oc_calendarchanges WHERE calendarid IN (1, 3, 5, 12, 22, 33, 34);
-
Restore the rescue on the live database
mysql -u root -p nextcloud < rescue.sql
-
Reset the sync token to a higher value than before
UPDATE oc_calendars l JOIN backup_calendars b ON (l.id = b.id) SET l.synctoken = b.synctoken + 1 WHERE l.id IN (1, 3, 5, 12, 22, 33, 34);
-
Delete the backup table
DROP TABLE backup_calendars;