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.














6 Comments
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!
You create the config file, it’s not something you find but something you create (as shown in the article).
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.
2 Trackbacks
[...] 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 [...]
[...] 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 [...]