Skip to content

Instantly share code, notes, and snippets.

@iaean
Last active August 15, 2017 16:37
Show Gist options
  • Save iaean/31f7f4da5d9c303f0b62614e07b79bd1 to your computer and use it in GitHub Desktop.
Save iaean/31f7f4da5d9c303f0b62614e07b79bd1 to your computer and use it in GitHub Desktop.
Backup ownCloud calendars and addressbooks

A shell stub to backup ownCloud calendars and addressbooks as ICS or VCF. Maybe it works for Nextcloud as well.
In order to keep only one cleartext password, an owncloud backup user is used for authorization. All users wishing a backup of their data, have to share their calendars and contacts (read only) to this user and needs to be added to the config file...

oc.ics.vcf.backup

Config

  • Create an user backup within your ownCloud.
  • Share calendars and addressbooks read-only to the backup user.

Setup scripts

  • CONFFILE
  • ROOTDIR
  • OCSERVER
  • AUTHPASS

and CONFFILE content...

Some TODOs

  • better error handling
  • more things configurable
  • with appropriate API backup all users of an OC instance (e.g. via group membership, etc.)
  • ...
#!/bin/bash
# In order to keep only one cleartext password, an owncloud backup user
# is used for authorization.
# All users wishing a backup of their data, have to share their calendars
# and contacts (read only) to this user.
CONFFILE=/path/to/oc.ics.vcf.conf
ROOTDIR=/path/to/owncloud.backup
OCSERVER='https://dav.example.com'
AUTHUSER=backup
AUTHPASS=secret
getcal () { # .ics
local DIR=${ROOTDIR}/${1}/${DATE}
local FILE="${DIR}/[${TIME}] ${2}.ics"
mkdir -p $DIR 2>/dev/null
wget -q --no-check-certificate --auth-no-challenge --no-clobber \
-O "${FILE}" --http-user=${AUTHUSER} --http-password=${AUTHPASS} \
"${OCSERVER}/remote.php/dav/calendars/${1}/${2}?export"
if [ -e "${FILE}" ]; then
chmod 600 "${FILE}"
fi
}
getcard() { # .vcf
local DIR=${ROOTDIR}/${1}/${DATE}
local FILE="${DIR}/[${TIME}] ${2}.vcf"
mkdir -p $DIR 2>/dev/null
wget -q --no-check-certificate --auth-no-challenge --no-clobber \
-O "${FILE}" --http-user=${AUTHUSER} --http-password=${AUTHPASS} \
"${OCSERVER}/remote.php/dav/addressbooks/users/${1}/${2}?export"
if [ -e "${FILE}" ]; then
chmod 600 "${FILE}"
fi
}
OIFS=$IFS
IFS=\|
grep -v ^# ${CONFFILE} | while read USER type NAME
do
DATE=`date '+%F'`
TIME=`date '+%T'`
case ${type,,} in
addressbook) getcard $USER $NAME ;;
calendar) getcal $USER $NAME ;;
esac
done
IFS=$OIFS
# user|addressbook -or- calendar|name of addressbook -or- calendar
#
foo|addressbook|default
foo|calendar|default
bar|calendar|holidays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment