How to Backup your Gmail or Google Apps Account Daily

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:

type = SimplePOP3SSLRetriever
server =
username = [email protected]
password = my_gmail_password

type = Maildir
path = ~/GmailBackup/

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

Be sure to replace [email protected] 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:

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

13 March 2007

29 Responses to “How to Backup your Gmail or Google Apps Account Daily”

  1. […] The nerd method, using a UNIX/Linux box and Getmail + a cronjob: Click here 2. The mostly nerd method, using Cygwin (UNIX emulator) and fetchmail: Click here 3. The non-nerd […]

  2. skttbrbrain says:

    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!

  3. george says:

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

  4. skttbrbrain says:

    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?

  5. george says:

    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.

  6. […] props to Charles Cazabon for his getmail program. – This write-up from about a year ago covers how to back up Gmail as well. – The author of getmail seems to hang out quite a bit on this getmail mailing list. See […]

  7. Pablo says:

    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..

  8. george says:

    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.

  9. Rob Rutten says:

    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:[email protected]
    Configuration error: SSL not supported by this installation of Python

  10. mister jasonâ„¢ says:

    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:

    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.

  11. George says:

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

  12. mister jasonâ„¢ says:

    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.

  13. Mark Wilson says:

    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

  14. George says:

    Use the SimpleIMAPSSLRetriever instead of the SimplePOP3SSLRetriever.


  15. Mark Wilson says:

    Thanks for the advice George.

  16. […] I found this tutorial on how to setup a daily backup routine from the command […]

  17. Jay says:

    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!

  18. Jay says:

    (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!


  19. Good point Jay, I’ll work on that.

  20. Leandro says:

    Hi George! I’m Leandro and I’m following your staps, but I’ve one question!
    When I start my server to download the messages, the system shows just 390 messages, but I’ve more than 1000. Are ther any way to copy all messages including inbox, sent mails and contacts?



  21. Leandro, I think gmail limits how many messages you can grab at any one time. Run the getmail script a few times and see if that helps?

    Thanks for commenting. :)

  22. Ron says:

    Hello, I stumbled onto this site today, and implemented getmail on a home server. I have only found one issue that is giving me fits and I can’t find a way to fix it. After running getmail, it is only downloading what is in my ‘inbox’. It isn’t downloading any of the other folders. Am I missing something?

  23. Mark Wilson says:

    @Ron – try adding mailboxes = ("[Google Mail]/All Mail",) to the [retriever] section of the getmail configuration file.

    (I wrote a follow-up post which intends to pull together content on this subject from an number of sources – including George’s post here.)

  24. Ron says:

    I’ll try this today hopefully. One more thing.. are your labels in tact when backing up this way, or will you have to relabel the messages if you ever have to restore?

  25. Ron says:

    @Mark: by the logic of your hint above, could you add more than one these entries to the retriever, to selectively backup directories? That would be mice instead of having one huge dorectory to possibly have to relabel in the future…

  26. Mark Wilson says:

    @Ron, I’m not sure how it works with the labels as Google doesn’t implement folders as such and the labels are just tags really. I think you could be more selective with multiple retrievers but messages may be downloaded multiple times.

    Alternatively you could archive in mbox format. I think that will preserve the folder structure?

    All of this bears the caveat that I’m not expert and would never have got this working in the first place if it wasn’t for George’s original post!

  27. Ron says:

    Well, in light of the above posts (and the possibility of of having to relabel 5000 messages), I ended up setting up as an IMAP client and also back it up using Time Machine. At least this way, if I ever need to restore my mail to Gmail, it will be rather simple (as I would only need to link up the IMAP again). Thanks for the help though!