We all know that GMail has offline mode. But in my case it wasn’t exactly what I wanted. What I was looking for was a way to backup my GMail as well make it searchable while offline.
To accomplish this I looked into what it would take to get a script running that I could schedule on my MBP to have it jump out and download my email every day.
This is going to require you to be comfortable using the command line so take that as the one of the first warnings of this post. What we will be doing in this tutorial is as follows.
1. Install Proper Certificates on our system so we can connect to GMail via the command line
2. Configure fetchmail to properly download email
3. Configure Thunderbird to read our newly downloaded email
The prep work will be to download and install Thunderbird. Feel free to use whatever standalone mail reader that you are familiar with but I’ll be showing what it takes to get Thunderbird to work.
Installing Certificates
First thing we need to do is to go download the root certificate and ensure its installed in our system for this tutorial. Go out to GeoTrust and download the certificate.
Open the commandline and we’ll create the directory that we need to save these certificates into.
mkdir ~/.ssl/certs/
Download the previous certificate into that folder but ensure that the extension is now ‘.pem’. Now we need to gather the appropriate GMail certificates.
openssl s_client -connect pop.gmail.com:995 -showcerts
You will find two sections like this.
-----BEGIN CERTIFICATE----- [large text block] -----END CERTIFICATE-----
Save the first one as googlepop.pem in the same folder as the last certificate.
Rehash this folder with c_rehash.
c_rehash ~/.ssl/certs/
This is where I encountered my first problem with this tutorial. When I ran that command I got a ‘bad command interpreter’ error. The scripting nerd that I am knew that it meant the first line in the c_rehash script wasnt pointing at the right thing. So I went to check on it and it had this.
#!/usr/bin/perl5
Well there is no file there, nor a link named perl5 to the current perl5 version installed on my system. So rather than just creating a link I decided to just edit the c_rehash script. But to do this you need root access at the command line in OSX. To enabled root on your OSX machine follow this KB article.
http://support.apple.com/kb/HT1528
After that is done you can switch to root like any normal Unix system and then update the file however you are comfortable with, vi/sed/awk/etc.
Configuring Fetchmail
Create the following file and set the permissions
~/.fetchmailrc chmod 710 ~/.fetchmailrc
Enter the following text in the fetchmailrc file that we just created.
poll pop.gmail.com with proto POP3 and options no dns user 'GMAIL_USERNAME@gmail.com' there with password 'GMAIL_PASSWORD' is 'LOCAL_USERNAME' here and wants mda "/usr/bin/procmail -d %T" options ssl keep sslcertck sslcertpath "/Users/LOCAL_USERNAME/.ssl/certs"
Update the following Variables with your appropriate information
GMAIL_USERNAME = Your GMail username
GMAIL_PASSWORD = Your GMail password
LOCAL_USERNAME = Your username on your system
Configure Thunderbird
In Thunderbird, go to Tools > Account Settings > Local Folders to get to the Local directory path. Go into this directory and create a symbolic link there that points to the mail spool file (/var/mail/LOCAL_USERNAME)
cd [Folder you just found above] ln -s /var/mail/LOCAL_USERNAME ./GMailBackups
Now wend you open Thunderbird, down in the Local Folders section you will see a new folder named ‘GMailBackups’ and it will contain all the email that you have downloaded. But we haven’t downloaded any yet! Here is how you will download your email.
Downloading Email
Downloading it is simple, just run the following command.
fetchmail -v
If you are like most OSX users you have Growl installed. You can write wrapper scripts in whatever language that they support to hook into Growl and show status updates as fetchmail pulls down your mail. I need to write a nice wrapper for this still.
Scheduling the Mail Backups
If you have a scheduler installed already on your system that you are comfortable with feel free to use that. You can also set it up in crontab. Rather than give a tutorial on crontab here, I’ll just send you guys here.
Thoughts
The first time you will run the script you will need to run it multiple times depending how much mail you have. It was only downloading 300-500 emails at a time for me. The script was also taking about 5 minutes to run each time as well. So after a few runs everything was done and when I fired up Thunderbird all the email was there in my nice little archive.