Super Nerdy Cool

My (technical) adventures through the professional world…
April 16, 2010

Backup your gmail account in Linux/Unix or Mac OS X using getmail

Even though I have utmost trust in Google’s data centers (>= 99.9% uptime), I finally decided to backup my gmail account. I am pretty OCD when it comes to backing up my data (bi-weekly backups of my home directory into 3 external hard drives, one of which is in a water/fire-proof safe box; MOST important research-related files in the cloud via dropbox), but I’ve never backed up my mail archive which holds a lot of valuable information.

I googled it before but never got around to doing it. Finally, I’ve implemented my backup scheme, thanks to this, this, and this.

The main program that does the work is getmail. To install, issue the following in Debian (apt-get) or Mac OS X (Macports):

 <pre class="src src-sh"><span style="color: #ff4500;">## </span><span style="color: #ff4500;">Debian</span>

sudo apt-get install getmail4 ## Mac OS X via macports sudo port install getmail

mkdir ~/.getmail mkdir ~/Backup/gmail-archive emacs -q -nw ~/.getmail/getmail.gmail

Put the following in “~/.getmail/getmail.gmail” (use IMAP and save both an mbox and a maildir version of my gmail account):

 <pre class="src src-sh">[retriever]

type = SimpleIMAPSSLRetriever server = imap.gmail.com username = email.address password = my.password mailboxes = (“[Gmail]/All Mail”,) [destination] type = MultiDestination destinations = (‘[mboxrd-destination]’, ‘[maildir-destination]’)

[mboxrd-destination] type = Mboxrd path = ~/Backup/gmail-archive/gmail-backup.mbox

[maildir-destination] type = Maildir path = ~/Backup/gmail-archive/

[options] # print messages about each action (verbose = 2) # Other options: # 0 prints only warnings and errors # 1 prints messages about retrieving and deleting messages only verbose = 2 message_log = ~/.getmail/gmail.log # preserves your mail after backup delete = false # just get new mails read_all = false

Note the “read_all” option – without this, getmail downloads multiple versions of the mail and the hard drive will fill. This happened to me!

In the shell, type

<pre class="src src-sh">touch ~/Backup/gmail-archive/gmail-backup.mbox

mkdir ~/Backup/gmail-archive/tmp ~/Backup/gmail-archive/new ~/Backup/gmail-archive/cur getmail -r $HOME/.getmail/getmail.gmail

To set a cron job for midnight, create the following Backup_gmail_emails.sh and place it in cron via “cron -e”:

<pre class="src src-sh">Backup_gmail_emails.sh

#!/usr/bin/env bash # Note: -q means fetch quietly so that this program is silent /usr/bin/getmail -q -r $HOME/.getmail/getmail.gmail

chmod u+rx $HOME/Backup/Backup_gmail_emails.sh

crontab -e: 00 00 * * * $HOME/Backup/Backup_gmail_emails.sh

4 Comments to “Backup your gmail account in Linux/Unix or Mac OS X using getmail”

Leave a Comment