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

# .mbsyncrc_pull
# global options
Create Slave
Expunge Slave
Sync Pull
# required when moving emails with email client
CopyArrivalDate yes

IMAPAccount email-me
Host mail.email.com
User [email protected]
PassCmd "gpg -q --for-your-eyes-only --no-tty -d email.pass"
SSLType IMAPS
CertificateFile /etc/ssl/certs/ca-certificates.crt

# mappings
IMAPStore remote-email-me
Account email-me

# local storage we'll be using. Note the trailing slash at the end of the Path
MaildirStore local-email-me
Path ~/imap-backup/mails/email/me/
Inbox ~/imap-backup/mails/email/me/Inbox
SubFolders Verbatim

# channels
Channel sync-email-me
Master :remote-email-me:
Slave :local-email-me:
Patterns * !Junk !Trash
SyncState *

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 :-

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

It will ask for a passphrase.

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

gpg -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 :-

mbsync -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 :-

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

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

rclone 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.




© 2016 - 2021