Backing up your ownCloud contacts & calendars

I’ve had most of my stuff either backed up to Tarsnap or archived to BD-R for a while now, with two exceptions: the contacts and calendars I have stored in ownCloud.  It’s not much information sizewise, but losing everyone’s phone numbers would be a royal pain in the ass.

Backing up contacts is relatively simple; ownCloud provides a URL that grabs them in one shot. Calendars are a bit more problematic, as you probably have more than one. HTTrack is used to grab all of the calendars, which are then concatenated and compressed (except for the contact birthdays calendar, which is auto-generated from your contacts). In my case, the backup is stored in a directory that gets sent to Tarsnap by another script; you could do whatever you want with your backup files.

Set this up as a cronjob; set it to run maybe a half-hour before your backup job. (12345 isn’t really my ownCloud password; I only use that on my luggage. :-) )

#!/bin/bash

source /etc/profile
cd $HOME

# script settings: ownCloud server address, username, password, 
# backup destination

OWNCLOUD=https://home.alfter.us/owncloud
USERNAME=salfter
PASSWORD=12345
DEST=/mnt/files/documents

PREFIX=`echo $OWNCLOUD | sed "s/\/\//\/\/$USERNAME:$PASSWORD@/"`

# retrieve all contacts from ownCloud and concatenate them into one
# compressed file, which then gets sent to Tarsnap with the rest of
# our documents

rm /mnt/files/documents/contacts.vcf* 
wget $PREFIX/remote.php/carddav/addressbooks/$USERNAME/contacts\?export -O "$DEST"/contacts.vcf && \
xz -z9 "$DEST"/contacts.vcf

# do the same with calendars...use httrack instead of wget as there's no
# way AFAICT to enumerate calendars so we can export them

httrack $PREFIX/remote.php/caldav/calendars/$USERNAME -O calendars && \
for i in `find calendars -mindepth 7 -type d | grep -v contact_birthdays`
do
  cat `find $i -name \*.ics` | xz -9 >"$DEST"/calendar-`basename $i`.ics.xz
done
rm -r calendars