Backing-up a Gmail IMAP account

by Martin Monperrus
To backup an IMAP account (e.g. Gmail on, mbsync (from the isync software package) is a very good solution. It backs-up all emails in a Maildir directory.


Install isync: $ aptitude install isync

Configuration file

Create a file .mbsyncrc in your home directory. This file contains something like:
IMAPAccount gmail
# you may add a Pass directive for running in crontab
# Pass foobar
UseIMAPS yes
CertificateFile /etc/ssl/certs/ca-certificates.crt

IMAPStore gmail-imap
Account gmail

MaildirStore gmail-local
# you have to manually create ~/.mbsync/gmail/ 
Path ~/.mbsync/gmail/
# Inbox required, even if one specifies  :gmail-local:INBOX
# otherwise it goes in ~/Maildir
Inbox ~/.mbsync/gmail/INBOX

Channel gmail-inbox
Master :gmail-imap:INBOX
Slave :gmail-local:INBOX
Create Slave
Expunge Slave
Sync Pull

Channel gmail-sent
# we need the double quotes
Master ":gmail-imap:[Gmail]/Sent Mail"
Slave :gmail-local:sent-mail
Create Slave
Expunge Slave
Sync Pull

Running mbsync

This is as simple as:
$ mbsync gmail-inbox
$ mbsync gmail-sent

You may then check that all messages are here: $ ls ~/.mbsync/gmail/INBOX/cur

See also

"Mirroring GMail locally with MBSync" and "GMail IMAP Backup With mbsync on Ubuntu"

Tagged as: