Backup my google calendars via shell and python scripts and scheduling them via cron


Vinh Nguyen


June 24, 2009

So recently my calendar got messed up a little (dupes) due my mobile phone (HTC Touch Pro running Windows Mobile 6.1) resetting [syncing again messed it up]. I realize (now???) I need to keep backups of my calendars and contacts.

I took the following script online (which requires python and the gdata module):

#! /usr/bin/python

## This script backs up my google contacts

import gdata.contacts.service
gd_client = gdata.contacts.service.ContactsService()
gd_client.ClientLogin('', 'password')
query = gdata.contacts.service.ContactsQuery()
query.max_results = 2000 # change for max contacts returned
feed = gd_client.GetContactsFeed(query.ToUri())
print feed

and adapted the following script to backup my calendars and contacts:

#! /bin/bash

## This script backs up my calendars -- will use with Cron to backup daily.
## following is adapted from
INCREMENT=`date +%Y%m%d%H%M`
## Vinh's calendar
curl -s url-to-private-ics -o "$DIR/Vinh_$INCREMENT.ics"
## TNTTSP's calendar'
curl -s url-to-private-ics -o "$DIR/TNTTSP_$INCREMENT.ics"
## Birthdays
curl -s url-to-private-ics -o "$DIR/Birthdays_$INCREMENT.ics"

## Now backup Google Contacts
$HOME/Documents/bin/ > ${DIR}/VinhContacts_$INCREMENT.xml

## Now backup Google Reader subscriptions
$HOME/Documents/bin/ $USERNAME $PASSWORD > Vinh_GoogleReaderSubs_$INCREMENT.opml

find $DIR -mtime +14 -exec rm -f {} \;

Every time the script runs, it backs up my 3 calendars and contacts. The very last line says delete if files are 14 days or older.

Now, to set up my crontab to run this everyday at 10pm, I type "crontab -e" in the shell. I put

00 22 * * * $HOME/Documents/bin/

when vi (or your default visual editor) opens up.

More info about cron on the Mac is here.

Sometimes, you might not want to schedule a recurring script. To run a script once, we can rely on the at ("at") command. More information here and here. However, the at command didn't work on my Macbook when I tried it a while back.

UPDATE: The script above ( is updated to include the google reader subscription, thanks to this site.