Saturday, March 5, 2011

Backing Up Gmail and Google Apps Mail using Ubuntu

I like using cloud-based services... especially Gmail since I can sit anywhere and access all my email. But what if Google has another failure like last week? I previously used Thunderbird and IMAP to download a copy of my emails until the "All Mail" folder got too large and I couldn't be sure I was getting a complete backup.

Lifehacker did an article on backing up your gmail. The command-line option they gave (fetchmail) is clunky and requires a local MTA. A few searches and a few clicks and I had the light-weight open-source "offlineimap" program working perfectly.

OfflineIMAP has a simple mode for Gmail. It's very easy to get it to copy your "All Mail" gmail folder to a local drive. It uses the convenient maildirs format where each email becomes a text file in a folder.

First enable IMAP access for your account.

Next install offlineimap using apt:
sudo apt-get install offlineimap
Create a configuration file for your user: `nano ~/.offlineimaprc`:
[general]
accounts = Gmail
ui = Noninteractive.Basic

[Account Gmail]
localrepository = LocalMaildirRepository
remoterepository = ServerRepository

[Repository LocalMaildirRepository]
type = Maildir
localfolders = /backup/Maildir_Gmail/

[Repository ServerRepository]
type = Gmail
remoteuser = user@googleappsdomain.com
remotepass = paaasswd
realdelete = no
folderfilter = lambda folder: folder in ['[Google Mail]/All Mail','[Gmail]/All Mail']

The configuration above restricts the download to the "All Mail" folder: I didn't want to download multiple copies of emails that have more than one label.

Test your configuration by running `offlineimap`:
$ offlineimap
OfflineIMAP 6.2.0
Copyright (C) 2002 - 2009 John Goerzen
This software comes with ABSOLUTELY NO WARRANTY; see the file
COPYING for details. This is free software, and you are welcome
to distribute it under the conditions laid out in COPYING.
***** Processing account Gmail
Copying folder structure from Gmail to Maildir
Establishing connection to imap.gmail.com:993.
Syncing [Google Mail]/All Mail: Gmail -> Maildir
Copy message 1 Gmail[[Google Mail]/All Mail] -> Maildir[[Google Mail].All Mail], LocalStatus[[Google Mail].All Mail]
Copy message 2 Gmail[[Google Mail]/All Mail] -> Maildir[[Google Mail].All Mail], LocalStatus[[Google Mail].All Mail]
Copy message 3 Gmail[[Google Mail]/All Mail] -> Maildir[[Google Mail].All Mail], LocalStatus[[Google Mail].All Mail]
...

You can start/stop and resume this download. It might pay to check the permissions on ~/.offlineimaprc. My version automatically locked the file so other users cant see the password.

To schedule a 2am nightly synchronisation using crontab: `sudo nano /etc/crontab`. Add this line at the bottom of the file:
0 2 * * * your_username offlineimap

No comments:

Post a Comment