mbsync is a command line application which synchronizes mailboxes; currently Maildir and IMAP4 mailboxes are supported …
We can use it to make a copy/backup/mirror from own mail accounts following the new
- First, enable IMAP in your mail account.
- Install the dependencies for mbsync:
sudo apt-get install libc6 libdb4.2 libssl0.9.8 isync
(maybe, you must consider the chrisstreeter.com original post)
Now you should have an executable mbsync in your path. So it is time to start preparing to do the initial sync.
- Choose a place to store your backups. For example: /mnt/sdh1/backups
- To access securely, we’ll need the latest SSL certificates. To get those, we can
use the openssl client. For GMail case:
openssl s_client -connect imap.gmail.com:993 -showcerts
which should show two blocks of
in the output. You’ll want to take each block (including the BEGIN/END CERTIFICATE lines), and put each of them into their own file. I put the first one in a file gmail.crt and the second one in the file google.crt (since the first signs imap.gmail.com which is signed by Google Internet Authority, the second certificate).
- The second certificate, the one for the Google Internet Authority, is signed by Equifax. So we’ll need Equifax’s certificate also. An as it turns out, Ubuntu has a copy of Equifax’s certificate already sitting in the repositories. Just run
sudo apt-get install ca-certificates
to get the latest CA certificates. After installing the CAs, Equifax’s CA sits at /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt, which we’ll need in the configuration file in the next step.
- Now we can write the configuration file we are going to use. Here is a copy of mine:
# Exclude everything under the internal [Gmail] folder, except the interesting folders
# Patterns * ![Gmail]* “[Gmail]/Sent Mail” “[Gmail]/Starred” “[Gmail]/All Mail”
Check out the Patterns line. That is where you would include or exclude various labels. All lables are stored at the root of the hierarchy, with the special directory [Gmail] having things like ‘Sent Mail’, ‘Spam’, ‘Starred’, etc in it. I wanted to exclude all the items in the [Gmail] directory except for the ones listed. The ‘*’ at the beginning includes all other labels. You will also want to change the Path and Inbox lines to point to your mail location, as well as the first two CertificateFile lines. Also, be sure to enter your actual GMail login on the User line. Now save this file somewhere. Note: saving it as ~/.mbsyncrc will cause it to be automatically loaded when mbsync is run, meaning you don’t need to specify which config file with the -c option.
Now go ahead and test it out by listing the labels in your account with the command mbsync -l -c /path/to/the/configfile.rc gmail. Running it will look like this and ask you for your password:
[streeter@scout]:~$ mbsync -l -c ~/.mbsyncrc gmail
Reading configuration file /home/streeter/.mbsyncrc
Resolving imap.gmail.com… ok
Connecting to 22.214.171.124:993… ok
Connection is now encrypted
If you see something like this, then it worked! Now just go ahead and start your
- Finally, try to download all the scanned labels:
mbsync -c ~/.mbsyncrc gmail
That’s is all!.
2 thoughts on “Getting mails via IMAP with mbsync”
Nice to see that you enjoyed my article so much that you did a bit of editing instead of just copy and pasting the entire article. Maybe an attribute would be nice?
I have readed your original post about one month ago. I want to do a backup for my gmail account and your post appeared in the first positions in Google.
Your notes had been very useful for my purpouse.
I will be grateful to refer your weblog, … very much.
Thanks for your really great public job.