Charlton's Blog

NextCloud CardDAV/CalDAV Backer-Upper

Back up your calendars, contacts, and to-dos to git.

Published: Dec 31, 2021
Category: Programming, Projects
Tags: ,

I’m a prolific user of CardDAV/CalDAV sync for my contacts, calendars, and to-do lists. While imperfect, these protocols represent the most mature, interoperable, and widely-supported standards for this purpose I’ve found to date. The “clouds” hosting my contacts & calendars have come and gone (the clients have too), but the dynamic duo of ‘DAVs have always guaranteed me the most freedom and portability.

These days, I use Nextcloud to consolidate things into a single managed service. It’s heavier-weight than alternatives such as Radicale or Apple’s Calendar and Contact Server, but I’m pleased with its robustness. Plus, certain built-in features of Nextcloud (native clients, web UI, other applications, …) are nice to have.

Anyways, when I was initially assessing DAV server implementations Radicale’s inbuilt support for Git-based version control caught my eye. Since then, I’ve been wanting to accomplish something similar to maintain versioned backups of my own address book and calendars on Nextcloud.

To achieve this, I wrote the following Ruby script:

In principle, the way the script works is simple: using the same URLs that a native client would to sync, it downloads your address book/iCal feeds to local files.

To configure this script, simply update the backups array with the names and URLs of all the contacts/calendars/to-do lists you’d like to back up from Nextcloud. Following that, run the script with NC_USER/NC_PASS environment variables set with your credentials (ideally app passwords). You can set up continuous backup using a cron job.

If you run the script inside of a Git repository, then the sync results will be checked into version control. If the current Git repository has at least one remote configured, the script will push the local changes upstream.

Hopefully it’ll help somebody. Enjoy!