Categories
Unix

How to Backup your Gmail or Google Apps Account Daily

This howto provides a simple process for painlessly keeping a backup of all your Gmail or Google Apps emails for a single account.

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 = [email protected]
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 [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:

[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

By George Donnelly

I'm building a tribe of radical libertarians to voluntarize the world by 2064. Join me.

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

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!

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!

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.

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

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.

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

[email protected]:~> 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]@pop.gmail.com:995:
Configuration error: SSL not supported by this installation of Python

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.

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.

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

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!

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

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?

Thanks,

Leandro.

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?

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?

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

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

Well, in light of the above posts (and the possibility of of having to relabel 5000 messages), I ended up setting up Mail.app 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!

Leave a Reply

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