offlineimap
is a popular solution for this.
Enable IMAP in GMail
(Google's instructions)
- Sign in to Gmail.
- Click Settings at the top of any Gmail page.
- Click Forwarding and POP/IMAP.
- Select Enable IMAP.
- Configure your IMAP client and click Save Changes.
Setup offlineimap
After you install it create a .offlineimaprc
[general]
accounts = GMail
maxsyncaccounts = 3
[Account GMail]
localrepository = Local
remoterepository = Remote
autorefresh = 10 #refresh every 10 minutes
quick = 5 #Quick-syncs do not update if the only changes were to IMAP flags
[Repository Local]
type = Maildir
localfolders = /whatever/directory
[Repository Remote]
type = IMAP
remotehost = imap.gmail.com
remoteuser = your_username@gmail.com
remotepass = your_password
ssl = yes
maxconnections = 1
#Setting realdelete = yes will Really Delete email from the server.
#Otherwise "deleting" a message will just remove any labels and
#retain the message in the All Mail folder.
realdelete = no
Then you need to run offlineimap.
Automating offlineimap with cron
In some cases having offlineimap running all the time can consume large amounts of memory, especially if you have a large mailbox, so you can add an offlineimap check into cron to just check it and then exit, here's a daily backup:
@daily offlineimap -u Noninteractive.Basic
Backing up offlineimap
The files will be in whatever you define in the localfolders line as a Maildir. You can back this up using whatever backup method you use:
References