Calcardbackup: bash script to backup Nextcloud calendars and addressbooks as *.ics/*.vcf files

It may be worth to take a look at vdirsyncer. It syncs calendar/card data from a server (e.g. nextcloud) to local files/folders.
And it can perform 2-way sync, so you can use other tools on your local files and the changes will be synched back.

vdirsyncer and calcardbackup have quite different use cases:

  • vdirsyncer syncronizes calendars/addressbooks of a single user. A single username/password combination must be given in a configuration file.

  • calcardbackup is meant as a backup solution for every single calendar and addressbook found in the database of Nextcloud/ownCloud without the need to give any usernames or passwords.

I just move servers and had to reinstall nextcloud and I wanted to say a big THANK YOU for saving me hours of trying to recover my user’s calendar!

1 Like

Thanks for this nice script! Excellent work!

Just the “–include-shares” option doesn’t work as expected, I still get this message:

Skipping calendar 'xxxxxxxxxxxxxx_shared_by_yyyyyy': already saved as 'yyyyy.ics"

Thank you :slight_smile:

That is actually intended behaviour as mentioned in the options-section of the README:

-i | --include-shares
       Backup shared addressbooks/calendars, too. Items will only be backed up once: e.g. a shared
       calendar won't be backed up if the same calendar was already backed up for another user.

I don‘t see a point of exporting exactly the same calendar twice.

But maybe you can convince me to overthink that. Is there a certain reason you need the file twice?

Well, the way that this option is named and described, suggest otherwise: without this option, each calendar will not be backed up more than once, and if you want shared calendars to be saved for each user, this option has to be used.

The option -i has been introduced for legacy versions of calcardbackup (when users’ credentials had to be passed to the script) to give a possibility to not expose users’ passwords. Starting with calcardbackup v0.8.0 the script reads all required data directly from the database without the need to pass any usernames/passwords to the script. In my opinion, that makes option -i obsolete (a while ago I actually thought about removing this option).

Changing the behaviour of a long time available option, is a step that requires a good reason - and I still can not see a reason to backup the same identical item more than once. Additionally, backing up shared calendars/addressbooks more than once can be very time consuming, which may be a problem for larger instances (e.g. several thousand users). So I’d rather leave it as it is - Sorry.

If you really need this though:
the script can easily be changed to backup shared calendars more then once. All you need to do is to comment out this line. Be aware that the change might get overwritten if you pull a new version. So you need to redo the change after every update.

I can not see a description that says so in the current documentation of calcardbackup. Could you tell exactly where you did get that impression so that I can correct the documentation?

Ok, I understand it now!

The misunderstanding was due to the combination of the option’s name and its explanation: the option is called “include-shares”, and the explanation says:

Backup shared addressbooks/calendars, too.

That can lead to the impression that you if you use it, shared calendars will be saved.

Once you are under that impression, the next sentence, saying that

Items will only be backed up once: e.g. a shared calendar won’t be backed up if the same calendar was already backed up for another user.

automatically sounds like “this will happen if you don’t use this option”.

I guess I would not have fallen for this misinterpretation, if those two sentences were linked by the word “but”:

Backup shared addressbooks/calendars, too, but every item will only be saved once!

Something like this, maybe. :slight_smile:

By the way…

In another thread I was asking about an add on that does what your script does, and that will be configurable via NextCloud’s GUI. The reason for this question is, that even an NC admin does not always have shell access to the server, so your script can not be used.

On the other hand I think it is very important to have a very fast and easy way of regaining the users calendars and contacts, if the server would crash or the data base would corrupt, while NC is still running. Letting the users access backups of their calendars as ical files, would be the fastest way to let them restore their calendars and contacts.

You script does exactly that: extracting the calendars and contacts, and copy the archives to the users file sharing area, where they can be downloaded. But if the admin can not ssh into the server, your script can not be used.

So, my question is: would it be very difficult to turn your script into an add on that the admin can configure in NC’s gui to provide this kind of function?

I see. Thanks for your input. I‘ll rephrase the explanation of the option.

I don’t think this is possible, because you would definitely run into webserver/PHP related timeout problems due to script execution time.

Oh man, this saved me so much headache.

I had an sql dump from an old version of nextcloud that I had to restore to an install of a recent version (usually, you have to create an instance of the same old version, import the database and then update version by version to get all the database migrations. But neither did I know the original nextcloud version, nor how to find out).
This shellscript from hell worked out of the box and let me export all of my data just with a mock config.php file.
Now importing it to the new instance is a breeze.

I can’t thank you enough for this <3


I remember reading that calcardbackup should backup Tasks as well. I added the Tasks app the other day, but looking at the backed-up files I don’t see anything relating to Tasks. Are Tasks saved within my personal.ics calendar file?

Calendar and task data are usually stored in the same file :wink:


Thank you so much @Bernie_O for writing this script!

This might be the final component I need for a self-hosted setup for notes and tasks using flat files for live editing or at least for backup. I’ve been reluctant to start using tasks in Nextcloud, until I set up backups that are readable by myself and other systems. Your script gives me peace of mind that if my Nextcloud instance crashes, I can quickly resurrect the tasks inside.

1 Like

calcardbackup v2.0.0 has been released. It comes with a new feature that might be interesting for some users who don’t have shell access to their Nextcloud server:

:tada: With the help of the included PHP script calcardbackup_wrapper.php there is now a good chance to be able to run calcardbackup even without shell access. As I can’t test all hosters out there I am very interested in feedback about this.
Check out the section Can I use this without shell access to the server? in the README of the repository.

:checkered_flag: Apart from that, calcardbackup has been adapted to use temporary files and kernel caching instead of Bashs’ file descriptors. Thus the memory consumption is only a fraction of what it used to be. These measures give a massive performance boost and make the script run two to three times faster! :zap:

Edit: please note that the version at github is heavily outdated and lacks a lot of bugfixes and other improvements compared to the stable version at codeberg. Find the link to the repo at codeberg in the first post of this thread.

1 Like

I’m intressting in your skript but is there a description for a use in Docker.

My nextcloud run on a synology inside 3 docker containers (db,app,reddis)

Unfortunately I don’t have any experience with docker. There is a docker version of calcardbackup, but I guess that is probably not what you are looking for.

calcardbackup needs to access the database - the credentials for MySQL/MariaDB/PostgreSQL (or the path to the SQLite3 datebase) are being read from Nextclouds config.php. If the config.php of the nextcloud container is not accessible from outside the container, you can easily set up a fake config.php with the according database details as described in the README under “Does this work with a broken instance”.

Let me know if that works.

Thanks for your replay, if I tried with the dummy there is this Error:


– calcardbackup: ERROR –
– ERROR: Nextcloud database is MySQL/MariaDB. Therefore this script requires package ‘mysql-client’ or ‘mariadb-client’.
– Install according package and run script again
– removing temporary files…
– calcardbackup: Exiting.

also with this command:
sudo -u http PATH="${PATH}:/usr/local/mariadb10/bin" ./calcardbackup “/volume1/homes/daniel/calcardbackup/”

Does it work, if you install the mysql Command-line Client as suggested in the error message?

Do you know how to connect to the dockerized database on the command line?

I can’t install it on the Synology.

There are users out there who managed to install it on Synology. My guess is, that the MySQL/MariaDB-Client comes with installing the MySQL/MariaDB-Server (not dockerized). But that is actually out of the scope of the Nextcloud-Forum and you might want to ask at Synology-Forum for how to install the mysql command-line client.