Backup your ownCloud calendar & contacts

The title describes the task: I want to backup my calender and contacts storing in an ownCloud instance. After doing some google search I found this blog post, which almost did it. I decided to change the script a little bit for my needs, since I wanted to have two more features:

  • Storing multiple calenders/contacts using their names: wget provides the parameter –content-disposition, which does the trick. Since wget does not return the file name for further processing and I didn’t like grep-sed-awk-cut the output of wget, I put all files in a folder and compress them.
  • A quiet script which reports errors: I’d like to use a cronjob, which does the backup for me. On the one hand I don’t want to receive a daily/weekly mail, that the backup was a success, on the other hand I want to have some information, if something failed, so i count the downloads and the resulting files and compare the numbers

Before I show you the script, I have to mention, that there are some security issues with this way of doing the backup:

  • You have to store your password plaintext in the backup script, so change the modes!
  • As mentioned here the script shows username and password in the process list. You can use .netrc to hide them.
#!/bin/bash
URLS="https://<CLOUDURL1>/?app=calendar&getfile=export.php?calid=<ID> https://<CLOUDURL2>/?app=calendar&getfile=export.php?calid=<ID2> https://<CLOUDURL3>/?app=contacts&getfile=export.php?bookid=<ID3>";
USER="name"
PASSWORD="password"
DIR="backup-`date +%Y%m%d`" # everything in this directory will be deleted after this run
COUNTER=0;
declare -i COUNTER;
pushd `pwd` > /dev/null
mkdir $DIR
chmod 700 $DIR
cd $DIR
for URL in $URLS
do
COUNTER=${COUNTER}+1;
wget --quiet --content-disposition --auth-no-challenge --http-user=${USER} --http-password=${PASSWORD} ${URL};
done;
popd > /dev/null
FILES=`ls -1 ${DIR} | wc -l`
[ ${FILES} -eq ${COUNTER} ] || echo "Failed."
tar cfz ${DIR}.tar.gz ${DIR}
rm -rf ${DIR}/

Leave a Comment

Your email address will not be published. Required fields are marked *