How to Backup Imap Emails

Posted on Fri Feb 26 2021

Preface

Recently I bought some space for email hosting. For personal emails I use Gmail & Outlook, so I never thought about keeping backup of my emails. But with this provider I wanted to keep off-site backup of my new email accounts.

I researched many CLI based tools for this. I think mbsync works best for this. It supports bidirectional and one-way sync, which is required for backup and restore.

Our goal here is to download emails in Maildir format and then backup them using rclone.

Configuring mbsync

By default mbsync uses ~/.mbsyncrc file as default configuration, but it supports custom config with -c argument. We are going to use this option, so we can switch between different configuration files.

Here is a sample configuration file for pulling emails to a local directory

1# .mbsyncrc_pull
2# global options
3Create Slave
4Expunge Slave
5Sync Pull
6# required when moving emails with email client
7CopyArrivalDate yes
8
9IMAPAccount email-me
10Host mail.email.com
11User [email protected]
12PassCmd "gpg -q --for-your-eyes-only --no-tty -d email.pass"
13SSLType IMAPS
14CertificateFile /etc/ssl/certs/ca-certificates.crt
15
16# mappings
17IMAPStore remote-email-me
18Account email-me
19
20# local storage we'll be using. Note the trailing slash at the end of the Path
21MaildirStore local-email-me
22Path ~/imap-backup/mails/email/me/
23Inbox ~/imap-backup/mails/email/me/Inbox
24SubFolders Verbatim
25
26# channels
27Channel sync-email-me
28Master :remote-email-me:
29Slave :local-email-me:
30Patterns * !Junk !Trash
31SyncState *

GPG for encrypting/decrypting passwords

We don't want to keep plaintext passwords in the configuration file. We can use GPG to encrypt/decrypt passwords in our local system.

You can use the following command to encrypt password :-

1echo "password" | gpg --symmetric --armor > secret.pass

It will ask for a passphrase.

Then you can use the following command to decrypt this file :-

1gpg -q --for-your-eyes-only --no-tty -d secret.pass

You will be prompted for passphrase which was entered when encrypting this file.

Configuration details

This configuration is specifically for pulling emails to local disk. Sync Pull indicates we want to pull emails from our IMAPStore to MaildirStore.

CopyArrivalDate is here to ensure correct dates are used when moving emails with email clients.

IMAPAccount defines remote IMAP account. Notice PassCmd is using GPG decryption that was explained in last section.

IMAPStore/Account mapping links an email account with IMAP store. MaildirStore is used for defining local disk path where Maildir will be stored.

Lastly we have channels, Channel is used to define which direction emails will flow. Because of Sync Pull mails will go from Master to Slave.

Once you have configured your email account, emails can be downloaded using the following command :-

1mbsync -aq -c .mbsyncrc_pull

Backup using rclone

mbsync is a good option for CLI based backups. We have downloaded emails in a directory, we just need to upload it as a zip file.

Configuring rclone to use your choice of backend is explained in details on their website.

In Maildir there are some additional files which we don't need in zip archive. Use the following command for preparing zip :-

1zip -q -r emails.zip mails -x '*/*.mbsyncstate' '*/*.uidvalidity'

Finally move zip file to remote location with the following command :-

1rclone move emails.zip remote:<remote_backup_path>

Conclusion

Here we have discussed how to take IMAP backup using mbsync and rclone. Similarly mbsync can be used for restoring emails back to IMAP server. Readers should be able to figure that out. If I get some time in future I will try to write a post about restoring emails.