banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Backup Your Gmail Account With getmail

by agmg (Antonis Komis)

Backing up your data is always important. Even when it comes to web-based emails. If you are a Gmail user, you can use a simple command-line program called getmail to backup up all your emails to a single .mbox file. This file can then be imported into most email clients (i.e. Thunderbird) and let you have all your Gmail messages in a local folder.


getmail is not installed by default in PCLinuxOS, but you can get it very easily using Synaptic Package Manager. Just search for "getmail" and select it for installation.

Before using getmail, you need to create a configuration directory, a directory to store the .mbox file, and the .mbox file itself. Open your favorite terminal emulator (i.e. Konsole) and type the following command:

mkdir $HOME/.getmail

and then the following command to set permissions for the folder:

chmod -R 0700 $HOME/.getmail

(only owner can read, write and execute on that folder).

The next step is to create a folder for the downloaded messages to be stored in. I have named it "gmail-backup" but you can use any name you like:

mkdir $HOME/gmail-backup

Don't forget to set its permissions, like before:

chmod -R 0700 $HOME/gmail-backup

NOTE: Both "$HOME" and "~" refer to your home directory at /home/

Now, you must create the .mbox file to contain the downloaded messages because getmail does not do this automatically. Type the following command at the prompt to create the .mbox file inside the gmail-backup directory:

touch ~/gmail-backup/gmail-backup.mbox

Again, you can use any name you like for your .mbox file.

Finally, you must create a configuration file for getmail to use and read your Gmail account information. Additionally, this configuration file will tell getmail where to save your downloaded messages.

Open your favorite text editor (i.e. KWrite) and copy-paste the following text in a file:

[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = yourname@gmail.com
password = yourpassword
[destination]
type = Mboxrd
path = ~/gmail-backup/gmail-backup.mbox
[options]
verbose = 2
message_log = ~/.getmail/gmail.log

Change the username and password to the ones for your Gmail account. If you used a different directory and filename for the .mbox file, change the "path" in the "destination" section to reflect your actual path and filename.

NOTE 1: you have to enable POP3 support for your Gmail account in order for getmail to be able to successfully retrieve your messages.
More info: https://support.google.com/mail/troubleshooter/1668960?hl=en

NOTE 2: if you are using two-step verification for your Gmail account, you need to authorize getmail by generating an application-specific password and use that instead of your main account password.
More info: https://support.google.com/accounts/topic/28786?hl=en&ref_topic=2373945

Once you have filled all the required data, save your file as:

~/.getmail/getmailrc

Close your text editor and return to the terminal.

Type getmail

You'll see a long string of messages display in the terminal window as getmail starts downloading the contents of your Gmail account. You can stop it at any time by typing Ctrl+C inside the terminal window.

NOTE: If the script stops, don't panic. Google has some limits on how many messages can be downloaded from an account at one time. To continue downloading your messages, just run the getmail command again and getmail will pick up where it left off. See getmail's FAQs for more information about this issue.

Once getmail finishes downloading all your emails, you will see an informative message and you will be returned to the prompt. You can safely close the terminal window.

You can automate the process of backing up your Gmail account by creating a shell script and setting it to run on a schedule using a cron job that runs once a day, once a week, or however often you feel is necessary.

Official website: http://pyropus.ca/software/getmail/



Previous Page              Top              Next Page
Copyright (c) 2013, The PCLinuxOS Magazine. All Rights Reserved.