Automatic Radicale backups with Git

Since version 0.9, Radicale has been able to keep track of content changes using Git. As well as providing version history, this feature can also be used to set up an automated backup.

Setting up Git

Git support requires dulwich, on Raspbian this, and Git, can be installed with apt-get:

sudo apt-get install python-dulwich git

Once the required packages are installed, check which folder Radicale is using to store data. This is set with the filesystem_folder option and defaults to /var/lib/radicale/collections:

$ grep filesystem_folder /etc/radicale/config
filesystem_folder = /var/lib/radicale/collections

Run git init as the radicale user to setup the git repository:

sudo -u radicale bash -c 'cd /var/lib/radicale/collections/ && git init'

Finally restart Radicale:

systemctl restart Radicale

All changes made to the files managed by Radicale should now be automatically tracked with Git. It's worth noting that commits will be made using Radicale <radicale@example.com> by default, however this can be configured in the [git] section of the config with the committer option.

Backup hook

Git hooks are programs or scripts run at certain points in git's execution. For this post we are going to create a very simple post-commit hook that will push any new commits made by Radicale to a remote server.

The first step is to setup SSH keys for the Radicale user:

sudo -u radicale ssh-keygen

Once SSH keys have been setup, copy them to the remote host. This can be done using ssh-copy-id:

sudo -u radicale ssh-copy-id user@backup-host

Assuming SSH authentication is now setup, create a bare repository on the remote server. This repo will be used to mirror the repo Radicale commits to:

sudo -u radicale ssh user@backup-host 'git init --bare ~/radicale-backup.git'

As the radicale user, add the new remote to the local Radicale repository:

sudo -u radicale bash -c 'cd /var/lib/radicale/collections/ && git remote add backup ssh://user@backup-host/home/user/radicale-backup.git'

Finally create a simple hook to push changes made to the backup repository:

cat > /var/lib/radicale/collections/.git/hooks/post-commit <<EOF
#!/bin/sh
cd /var/lib/radicale/collections/
git push backup --all
EOF

chown radicale: /var/lib/radicale/collections/.git/hooks/post-commit
chmod 755 /var/lib/radicale/collections/.git/hooks/post-commit

Now every time changes are made, Radicale should commit the changes and replicate them to the remote repository.

Future versions of Radicale

Unfortunately Git support is being dropped in Radicale 2.0. There is however a hook feature than can be used to run arbitrary scripts. This should make it fairly simple to replicate Git support by calling git from a script.