Backing up cloud data

This year I replaced my Palm Treo with an iPod Touch. To make sure that I could access all of my data on my iPod as well as on my Linux desktop (and anywhere else for that matter) I moved all of my data to the cloud. This is great for accessibility, however I still want backups in case something happens to the cloud provider. Remember that all data needs to be backed up or you run the risk of losing it.

The 3 cloud providers that I am currently using and want backups for are Google Contacts, Google Calendar and Toodledo. My backups are done from my Linux desktop, so I’m using unix tools to do the work. I’m sure people can find replacements under Windows as well.

I wrote a script for each job (based upon various solutions that I found on the net). The data for each cloud service is backed up into a directory named by the date and I choose to keep 30 days worth of backups, so that I can go back and pull information that I may have accidentally changed or deleted.

First up is Google Calendar.

#!/bin/bash -e
root=${HOME}/backup/pim/calendar
date_dir=`date +%Y%m%d`
base=${root}/${date_dir}
mkdir -p ${base}
find ${root} -maxdepth 1 -type d -mtime +30 -exec rm -rf \{\} \;
CURL="curl -s -S"
${CURL} -o ${base}/cal.xml "PRIVATE_URL_FROM_GOOGLE"

Here I setup some variables to determine the name of the directory based upon date. I then delete anything older than 30 days. The real key here is the last line. This tells curl to download the calendar and save it locally as “cal.xml”. The PRIVATE_URL_FROM_GOOGLE is found by going to calendar settings for the calendar in Google. At the bottom is the private address with 3 links for XML, ICAL, and HTML. I choose to download the XML version, but you can pick any that you want to work with. Since this script has your private calendar URL, you should protect it from others as they can change your calendar with this URL.

Backing up Google Contacts is a little harder because you have to login to get the contacts. I found a script to do this searching Google. I found a script written in python that dumps the contacts in Google’s XML format. I don’t know the best way to load back from this format, but I’m sure I could figure it out if needed.

#!/usr/bin/env python

import gdata.contacts.service

gd_client = gdata.contacts.service.ContactsService()
gd_client.ClientLogin('user@gmail.com', 'password')

query = gdata.contacts.service.ContactsQuery()
query.max_results = 1000 # change for max contacts returned

feed = gd_client.GetContactsFeed(query.ToUri())
print feed

Then I wrote a wrapper script like the one for Google Calendar that puts the data where I want it.

#!/bin/bash -e
root=${HOME}/backup/pim/contacts
date_dir=`date +%Y%m%d`
base=${root}/${date_dir}
mkdir -p ${base}
find ${root} -maxdepth 1 -type d -mtime +30 -exec rm -rf \{\} \;
export base
${HOME}/bin/export-gcontact.py > ${base}/contacts.xml

I’m sure I could have done this in python too, but I wanted to keep all of my scripts as much the same as possible.

The last cloud service I needed a backup of was Toodledo. This one uses curl like the one for Google Calendar. Except that it does the login too. This script has curl post to the login page, save the cookies, then visit the XML export page. Since the cookie is saved, we don’t get prompted for a login again. I tried to do this trick on the Google Contacts page as well, but it doesn’t work because Google embeds a random hash in the login page as a hidden form field that needs to be passed on for the login to work. Anyway here’s my Toodledo backup script:

#!/bin/bash -e
root=${HOME}/backup/pim/tododate_dir=`date +%Y%m%d`base=${root}/${date_dir}mkdir -p ${base}
cd ${base}
curl \  
  --output /dev/null  \ 
  --form 'email=YOUR_EMAIL' \
  --form 'pass=YOUR_PASSWORD' \ 
  --form 'remember=1' \ 
  --cookie-jar ${root}/cookies.txt \ 
  http://www.toodledo.com/signin.php \ 
  --output toodledo.xml \ 
  http://www.toodledo.com/xml.php

Note that this script has your username and password in it, so you’ll want to set the permissions on the script to keep others out.

To backup my email from gmail, I’ve found offlineimap to be a great application and easy to setup.