Hassing.org

Backing up Gmail with Fetchmail

Ever worry about the fact that if Google decide to close your Gmail account then you will loose a ton of information? Then here is an quick way to get a lokal copy of all your email.

This should work on any machine with fetchmail and procmail – BSD, Linux, OS X. Pretty much anything except Windows. For which I’m sure there’s some large, complicated package you could install for the same purpose.

First goto your account, click ‘Settings’ -> POP/IMAP and select ‘Enable POP for all mail’. Then click save.

Create a file called .fetchmailrc in your home folder. Add the following to it:

poll pop.gmail.com protocol pop3 user "username@gmail.com"
    password "password" mda "/usr/bin/procmail -d %T" ssl

If you have more than one account then just add one per line. This also works with Google Apps. Just replace @gmail.com with your domain. The pop3 server is still pop.gmail.com.

Now edit .procmailrc in your home folder and add:

PATH=/bin:/usr/bin:/usr/local/bin
SHELL=/bin/bash
VERBOSE=off
:0
$HOME/GmailBackup.mbox

Finally run this to download your mail:

fetchmail -aK

Google seem to only let you get around 300 mails at a time. You might need to run it many times if you have a lot of mail. Instead of running it manually you can just add it to your crontab. Check the bottom of Remote Backup Solution for info on how to use the crontab. This way you can also make daily backups of your email.

When all mail is downloaded all you need to do is backup the file GmailBackup.mbox in your home folder and you email is safe. The mbox format is just text and can be imported by most email software.