Backup Gmail Using Getmail
Getmail is an open source Python program that can backup your Gmail account. Getmail will work on any *nix system, including OS X, and even Windows as long as you have Cygwin installed.
Ubuntu users have it easy, since Getmail is in the Ubuntu repositories.
For other operating systems, you can either add the repository to aptitude or you can compile from the source like so:
Download Getmail, then open a terminal windows and type:
tar xzvf getmail*.tar.gz
cd [the directory it was unpacked into]
sudo python setup.py install
For Cygwin, you will probably first have to “rebase” your Cygwin installation. Briefly, that means closing Cygwin, opening cmd.exe and using it to run “dash” from C:\cygwin\bin, then typing “/bin/rebaseall” at the prompt it gives you. More on that here.
Setting Up Getmail
Now you just need a configuration file to tell Getmail how to connect to Gmail. There are a ton of sample configuration files on the Getmail site, but here’s one that works well with Gmail. Create a file at ~/.getmail/getmail.gmail and put the following text in it:
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = firstname.lastname@example.org
password = yourpassword
type = Mboxrd
path = ~/gmail-archive/gmail-backup.mbox
verbose = 2
message_log = ~/.getmail/gmail.log
Edit the email@example.com and yourpassword in the file to your specific values, of course. Note: this doesn’t have to be an @gmail.com address. If you are using Google for mail on a custom domain, just fill it in here.
Getmail won’t create the mbox file, so before we get started, create a new empty file called gmail-archive.mbox in the gmail-archive folder of your home directory (or where ever you choose to store it). From the command line this will do the trick: touch ~/gmail-archive/gmail-backup.mbox
You have everything set up, so it’s time to actually make the backup. Fire up a terminal window and enter the following line:
getmail -r ~/.getmail/getmail.gmail
You should see a long string of messages begin printing out as Getmail starts grabbing the contents of your Gmail account. If the script stops, fear not — Google has some limits on how many messages can be retrieved at one time. Just run the above command again the Getmail will pick up where it left off. SeeGetmail’s documentation for more about this “cutoff” issue.
Now you have an mbox-formatted local backup of your Gmail account and you can access it with any e-mail client that understands mbox files, which is pretty much everything except Microsoft Outlook.
To automate this process, just create a shell script and attach it to a cron job that runs once a day or however often you feel is necessary.