I've heard good things about
imapcopy, which is a
IMAP backup/copy/migration tool. However, because it lacks SSL support, it may not work with Gmail.
For my purposes, I wrote a tool called
imap2maildir, which is optimized for periodically backing up an IMAP mailbox to a local maildir folder. Assuming you're using maildir for your mailboxes (if you have folders 'cur' and 'new', and each message occupies one file, you do), this will probably do the trick.
_________________
Code:
/* TODO: need to add signature to posts */