How to backup your email?

While I’ve always believed in my email provider’s data reliability (previously Google, now MXRoute), I feel it’s important to maintain a backup in the case of any unexpected catastrophes.

For my emails, it was just downloading all of them to my email client’s local folders. Pretty simple and straightforward and you’ll be able to quickly access and search through them whenever.

However, with the changing of my computing habits and using my laptop less frequently, I’ve been wanting to set up an alternate backup flow to my remote VPS instead of locally to my laptop.

After much procrastination and research, I’ve made the decision to go with offlineimap for the backup and notmuch to manipulate them on the server.

  1. Install offlineimap and notmuch

    sudo apt install offlineimap notmuch

  2. Create the ~/.offlineimaprc config file. Something like should get you started quickly if you’re using Gmail. You can check out some other examples here.

    ui = ttyui
    accounts = Gmail
    pythonfile = ~/.offlineimap/

    [Account Gmail]
    localrepository = Gmail-Local
    remoterepository = Gmail-Remote
    postsynchook = notmuch new

    [Repository Gmail-Local]
    type = Maildir
    localfolders = ~/mail/gmail

    [Repository Gmail-Remote]
    type = Gmail
    remoteuser = [email protected]
    remotepasseval = gmailpassword
    realdelete = no
    maxconnections = 3
    sslcacertfile = /etc/ssl/certs/ca-certificates.crt

    Note that I’ve stored my password in a separate file ~/.offlineimap/

    gmailpassword = "YOUR_ACTUAL_PASSWORD_HERE"

    Be sure to change the permission and the file ownership:

    chmod 600 ~/.offlineimap/
    chown user_name:group_name ~/.offlineimap/

  3. Run notmuch for the initial configuration


  4. Run offlineimap to start downloading all the emails. Your first run will probably take awhile.

    offlineimap -o

  5. Create a cronjob to schedule the download automatically. Since my primary purpose is to backup the emails, I’ve set mine to run once daily:

    0 0 * * * offlineimap -o -u quiet

  6. You can search through all the downloaded emails in ~/mail/gmail with notmuch

    notmuch search "email backup"
    notmuch search to:[email protected]
    notmuch search date:yesterday

  7. Then to view specific email thread:

    notmuch show thread:000000000000XXXX