In this tutorial I am going to discuss how to backup your Gmail emails to your own computer. Now personally I trust Google and keep allot of data on their services, but when it comes to emails I like to be doubly sure. It’s extremely rare but on occasion even Gmail has some problems, for example read this news article about some data loss at Gmail. When I read this I thought, “dam what would I do if I lost my emails“, well that scared the hell out of me, I’m always going back though old emails to find information (password, email addr’s, phone numbers, etc..) and this important information is only stored in my emails. So following the rule that “data only exists if its stored in three places” I thought I would add my own step to Google’s backup solution, just to be safe, and this is how I did it.
To backup your Gmail emails you’ll need to download your emails via POP3 onto your computer, these emails will then be contained in a file that you can backup onto your own backup media. First you’ll need to setup your Gmail to allow for POP3 download, go to your Gmail setting window and choose the “Forwarding and POP/IMAP” section. Now select the “Enable POP for all mail (even mail that’s already been downloaded)” option and the “When messages are accessed with POP” should be set to “Keep Gmail’s copy in the inbox”. Now click “Save Changed”
Now that Gmail is setup to send emails via POP3 you’ll need to setup a computer to receive and compile these emails. I started by launching an Amazon EC2 Instance, this way I had access to a computer with a high speed connection and high processing power, great for downloading all my emails and encrypting the backup file so no one else can read my precious emails. If you don’t already know how to start an EC2 Instance look here, or if you want to follow along at home this tutorial will also work on any computer running Ubuntu.
Once we have our computer or instance up and running we need to install postfix, this is the email server we will use to download the emails from Gmail. To install this go to your terminal and type:
sudo apt-get install postfix
After a few seconds you should see the following window outlining the types of server Postfix can run as:
Select ok by pressing the TAB key and then RETURN, you will now be asked for the type of server you want to use, select Internet Site:
Now you will see a window that askes for your System Mail Name, just leave this as it is, it will already be set the correct value.
That is you have installed a basic postfix server. Now it’s time to install fetchmail, this is the piece of software that will download mail from Gmail onto our own server:
sudo apt-get install fetchmail
Now we need to setup fetchmail, this works by creating a config file called .fetchmailrc to do this type:
cd ~/ touch .fetchmailrc chmod 600 .fetchmailrc
Now we add to the config file:
and add the folowing lines, subistuiting:
poll pop.gmail.com protocol pop3 user "[USERNAME]@gmail.com" there with password "[PASSWORD]" is "[LOCAL USER]" here keep ssl
Now all you need to do is run fetchmail, you can either do this normally or in daemon mode. Personally I test the settings with the normal mode then run in daemon mode so I can still use my terminal, here are the two methods:
NORMAL MODE: fetchmail -v
DAEMON MODE fetchmail -d 1
whitch ever way you choose fetchmail create a file in the directory /var/mail with the same name as your username, this will contained your email. All you need to do now is copy this file to your normal backup media.
If you want to look at your email within the backup file you can open the file with Thunderbird or Alpine (automatically opens /var/mail/[USER]). The other nice thing about this solution is you could use it to see your Gmail emails via an email client on your machine. If you just leave fetchmail running in daemon mode it will download any new emails to the backup file so you can see them with Thunderbird or Alpine.
If you haven’t used Alpine before it’s a console based email client, its automatic reads the mail file in /var/mail for current user, to install it type:
sudo apt-get install alpine
Please leave any comments or questions.