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/

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

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

type = SimpleIMAPSSLRetriever server = 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/

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

<pre class="src src-sh">

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

chmod u+rx $HOME/Backup/

crontab -e: 00 00 * * * $HOME/Backup/

About Vinh Nguyen



  1. If you get an error early on that isn’t the same as an invalid password error (try an invalid password to see what that looks like) it may be that you need to set your mailboxes list to say “Google Mail” rather than “GMail”:

    mailboxes = (“[Google Mail]/All Mail”,)

    • found that on another blog and it works for me, so just spreading the word.
  2. Hello, Being developer sharing my view about backup your IMAP enabled account is necessary to avoid the loss in situations like accidental loss of data from email servers, hacking of account, forget password, over size inbox or unpredictable scenarios. Using Beyond Inbox, you can backup, restore, archive, transfer or organize email from any IMAP enabled email account.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>