The first question you will ask is why? Well there are few users who lost all the their mails because of some bug. You can find the details about that here. And the second reason is I am too paranoid. :P
Backing up is very easy as gmail offers you a pop access to it’s service. So you can use any pop client, I chose getmail (getmail is written in python, you can even run it on windows with Cygwin). Reason being it gives you a lot more flexibility than Fetchmail. Now you just have to enable pop access for your gmail account for all mail (IMP: Let it keep the mail in your gmail account after you access it from pop, to be on the safer side.) and let getmail do it’s magic. Configuring getmail is quite easy. All you have to do is create a file called getmailrc in ~/.getmail/. Put the following contents in that.
[options]
verbose = 0
delete = false
message_log = ~/.getmail/log
[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = gmailusername@gmail.com
[destination]
type = Mboxrd
path = ~localusername/backup/gmailMbox
user = localusername
Note :- You can also keep your password in this file so you can just a run a cron job. But like I said I am too paranoid to do that. :)
Also /home/localusername/backup/gmailMbox file should created before running get mail, as getmail doesn’t create it for you.
Now you can run getmail via command line Or put it in a user cron.
$getmail -l
It will ask you for a password, If you have not entered your password in the getmailrc. (If you have large no. of mails in your account you may not able be able to get all of it in one go. More information here.)