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`:
accounts = Gmail
ui = Noninteractive.Basic
localrepository = LocalMaildirRepository
remoterepository = ServerRepository
type = Maildir
localfolders = /backup/Maildir_Gmail/
type = Gmail
remoteuser = email@example.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`:
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