How to Backup your Gmail or Google Apps Account Daily

March 13, 2007

If you rely on Gmail or Google Apps for your mission-critical email then you know you need to take your own backups. Your Gmail email can get deleted or lost and once it’s gone, it’s gone. This howto provides a simple process for painlessly keeping a backup of all your Gmail or Google Apps emails for a single account.

You’ll Need a *nix Machine

This howto assumes that you have a shell account on a *nix (unix, linux, BSD, Mac OS X, etc.) machine and can install getmail, a flexible mail retriever. If you have root access on a FreeBSD machine you can install getmail like this:

cd /usr/ports/mail/getmail ; make install clean

If nothing else works, you can always download it from the getmail website and install it from source, since it’s written in python.

Run the Following Commands

Bring up a terminal or SSH session and run the following commands as the user that you will run getmail as. Any non-privileged user should do the job.

mkdir ~/.getmail
mkdir ~/GmailBackup/ ~/GmailBackup/new ~/GmailBackup/tmp ~/GmailBackup/cur
vi ~/.getmail/getmailrc.mygmailaccount

Getmail Configuration File

And put the following into ~/.getmail/getmailrc.mygmailaccount:

[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = me@gmail.com
password = my_gmail_password

[destination]
type = Maildir
path = ~/GmailBackup/

[options]
verbose = 2
received = false
delivered_to = false
message_log = ~/.getmail/gmail.log

Be sure to replace me@gmail.com with your Gmail or Google Apps email address and my_gmail_password with your account’s password.

Getmail will Copy your Gmail Emails, without Deleting them

This configuration file will cause getmail to copy the emails in your Gmail or Google Apps account, without deleting them, including emails in the Sent folder. Getmail will store your emails as individual files in the ~/GmailBackup/new directory. The ~/GmailBackup directory is a Maildir, so you can read those emails with your own POP or IMAP server or a text editor.

If you Prefer Mbox

If you prefer to store your emails in an mbox file, you can change the destination section to read:

[destination]
type = Mboxrd
path = ~/GmailBackup.mbox

You might want to also run:

touch ~/GmailBackup.mbox

The Mbox format can be less reliable than Maildir but is popular and can be easily imported into many popular email clients, such as Mail.app and Thunderbird.

The Daily Cronjob

Finally you’ll want to open your non-privileged user’s crontab:

crontab -e

And add this cronjob:

#backup gmail email
03 04 * * * /path/to/getmail -ln --rcfile getmailrc.mygmailaccount

You can get the path to getmail by typing the following in your terminal or SSH session:

which getmail

This will take a daily backup at 4:03AM in the time zone of the computer.

The First Backup

You can go ahead and run the first backup:

getmail -ln --rcfile getmailrc.mygmailaccount

If successful, this will output considerable detail, including information on each email message it downloads and where it saves it.

Check your Backups Anytime

You can always check the status of your recent backups by tailing the getmail log we configured in the getmailrc file:

tail -f ~/.getmail/gmail.log

Take Backups Today!

If your mission-critical email is hosted by Google, you know you need to take backups. Sleep easier and start taking backups today. Once you set them up, you won’t have to worry about them again.

Related Links

  • del.icio.us
  • Digg
  • Reddit
  • StumbleUpon
  • Mixx
  • Facebook
  • Live
  • Pownce
  • TwitThis
  • Technorati

{ 3 trackbacks }

Thoughts » Blog Archive » Never trust a 3rd party with your mail
07.27.07 at 8:32 am
How to back up your Gmail on Linux in four easy steps
03.11.08 at 2:31 am
Virtuous cycle » Backup your Gmail before it’s too late
09.21.08 at 3:28 pm

{ 17 comments… read them below or add one }

skttbrbrain 12.29.07 at 5:29 pm

hey there! stupid question:

where is the getmail configuration file? and how do I edit it with the details? please help!

nice tutorial btw. thanks!

george 12.29.07 at 8:19 pm

You create the config file, it’s not something you find but something you create (as shown in the article).

skttbrbrain 12.30.07 at 1:18 am

well I got till “Run the Following Commands” and got stuck at “Getmail Configuration File”.

when you say “And put the following into ~/.getmail/getmailrc.mygmailaccount”, how do you mean? I was expecting the file to open up in textedit or something and I put in those details and save, and watch it close automagically, so I get on to the next step. but obviously, I’m much too naive when it comes these things.

still, I’d really like to get this working. help?
thanks!

george 12.30.07 at 8:55 am

you should run those commands in Terminal.

Or you can try opening up a fresh textedit document, edit it as needed and then save as to the right location. If you’re on a Windows box of course my howto may not apply at all. My howto only applies to unix/linux, e.g. Ubuntu, Mac OS X, FreeBSD, etc.

Pablo 04.08.08 at 11:48 am

Can you be more specific about recovering? What if I want to migrate the backup to a new hosting?..Thanks a lot, the article is great..

george 04.08.08 at 11:59 am

Well you now have your email in either mbox or Maildir format. These are the 2 most popular formats so you shouldn’t have any trouble using them anywhere you can get email service and the access to upload your email.

Rob Rutten 07.08.08 at 2:58 am

I tried to follow the whole recipe on a MacBook Pro but get stuck at
gmail using SSL:

rr@mac:~> getmail -ln –rcfile getmailrc.mygmailaccount
getmail version 4.7.7
Copyright (C) 1998-2007 Charles Cazabon. Licensed under the GNU GPL version 2.
SimplePOP3SSLRetriever:xxxxxxx@gmail.com@pop.gmail.com:995:
Configuration error: SSL not supported by this installation of Python

George 07.08.08 at 7:54 am

You want to try installing this:

http://py25-socket-ssl.darwinports.com/

mister jason™ 08.14.08 at 8:16 pm

I have this working beautifully in cygwin. It just took installing getmaill and turning on the cron service…which I found how to do here: http://www.devdaily.com/blog/post/linux-unix/get-crontab-started-running-when-using-cygwin/

I’ve only run the script (manually) twice now to test functionality and I’m seeing a strange behavior. Thi first run only saw 282 messages and stopped when it had grabbed them all. The second run saw 832 messages and is still running. Right now there are 5157 messages in my gmail account.

Not sure what the deal is…I’m going to keep at it until I get a good result and post my findings back here.

George 08.14.08 at 8:44 pm

Jason, surprising, let us know how it turns out, thanks.

mister jason™ 08.15.08 at 3:26 am

Just as I was suspecting…it’s a gmail issue. For reasons that should be obvious, gmail only lets you grab messages a few hundred at a time.

I’m guessing it’s more about the amount of data rather than number of messages since the number I get each time varies so wildly.

Mark Wilson 09.14.08 at 6:49 pm

Thanks for publishing this - I have a question though - how would this approach be modified to use IMAP4 for fetching the mail rather than POP3? I use IMAP for my various GMail clients and I don’t want to have to keep switching back to POP for backups.

TIA, Mark

George 09.15.08 at 8:13 am

Use the SimpleIMAPSSLRetriever instead of the SimplePOP3SSLRetriever.

http://pyropus.ca/software/getmail/configuration.html#retriever-simpleimapssl

hth

Mark Wilson 09.19.08 at 7:24 am

Thanks for the advice George.

Jay 10.10.08 at 10:38 am

This article should really be called “How to Backup your Gmail or Google Apps Email Account Daily”. There is nothing in here about backing up my Google docs, calendar, etc!

Jay 10.10.08 at 10:39 am

(accidentally hit post)

I am reading a few other sites for info on this, but if anyone has suggestions that are automated, I would greatly appreciate that info!

Thanks!

George Donnelly 10.10.08 at 10:56 am

Good point Jay, I’ll work on that.

Leave a Comment

You can 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>

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.

Older post: Saddam Hussein: Killing a Killer Makes Two Wrongs and Zero Rights

Newer post: Three Reasons Why I’m Switching from Mac to Ubuntu