Backup Imap Server Emails

These are notes for backing up email from an Imap server using the raspberry pi. These are notes, and you should make sure to read this post over in its entirety, so other searches on the internet before making any attempts to replicate the content of this post. As these are again notes.

$ sudo apt-get install git python getmail4
$ cd ~
$ git clone https://github.com/ralbear/IMAPbackup.git
$ cd IMAPbackup

This will install the required packages to run the script imap backup. Next we will test the connection to see if we can connect.

$ python imapgrab.py -l -s MAIL.SERVER.COM -S -p 993  -u USER@DOMAIN.COM -p 'EMAIL_PASSHERE'

If the command worked, you should see a list of folders.

IMAP Grab 0.1.4
---
List of mailboxes available:
confirmed-ham
Archive
INBOX/Spam
INBOX/Sent
INBOX/Drafts
INBOX/Trash
Spam
Sent
Trash
Drafts
confirmed-spam
INBOX

Next we can move on to batch download all of our emails. The syntax for this is:

$ mkdir ~/email-backups
$ python imapgrab.py -d -v -M -f ~/email-backups -s MAIL.SERVER.COM -S -p 993  -u USER@DOMAIN.COM -p 'EMAIL_PASSHERE' -m "_ALL_"

We create a folder ’email-backups’ in the home directory, and then repeat a similar command from before. But this time we add the options to download all of the folders in the mailbox. After this happens imapgrab will create a list of folders for the emails to be downloaded to.

IMAP Grab 0.1.4
---
Download option selected
Getting list of mailboxes
List of mailboxes selected to download:
['Archive', 'Drafts', 'INBOX', 'INBOX/Drafts', 'INBOX/Sent', 'INBOX/Spam', 'INBOX/Trash', 'Sent', 'Spam', 'Trash', 'confirmed-ham', 'confirmed-spam']
Checking available mailboxes on server
Creating download config files in /home/kion/email-backups
Created "/home/kion/email-backups/Archive.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Archive/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Archive/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Archive/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Archive/tmp/"
Created "/home/kion/email-backups/Drafts.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Drafts/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Drafts/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Drafts/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Drafts/tmp/"
Created "/home/kion/email-backups/INBOX.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/tmp/"
Created "/home/kion/email-backups/INBOX/Drafts.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Drafts/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Drafts/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Drafts/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Drafts/tmp/"
Created "/home/kion/email-backups/INBOX/Sent.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Sent/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Sent/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Sent/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Sent/tmp/"
Created "/home/kion/email-backups/INBOX/Spam.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Spam/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Spam/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Spam/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Spam/tmp/"
Created "/home/kion/email-backups/INBOX/Trash.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Trash/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Trash/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Trash/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/INBOX/Trash/tmp/"
Created "/home/kion/email-backups/Sent.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Sent/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Sent/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Sent/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Sent/tmp/"
Created "/home/kion/email-backups/Spam.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Spam/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Spam/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Spam/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Spam/tmp/"
Created "/home/kion/email-backups/Trash.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Trash/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Trash/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Trash/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/Trash/tmp/"
Created "/home/kion/email-backups/confirmed-ham.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-ham/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-ham/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-ham/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-ham/tmp/"
Created "/home/kion/email-backups/confirmed-spam.rc"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-spam/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-spam/new/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-spam/cur/"
Maildir directory doesn't exist, creating "/home/kion/email-backups/confirmed-spam/tmp/"

From there the script will proceed to download all of the messages from all of the folders in the imap server.

Calling getmail to retrieve mail for mailbox "/home/kion/email-backups//Trash"
Downloading mailbox "Trash"
getmail version 5.13
Copyright (C) 1998-2019 Charles Cazabon.  Licensed under the GNU GPL version 2.
SimpleIMAPSSLRetriever:kion@dashgl.com@mail.privateemail.com:993:
  msg  1/73 (11976 bytes) delivered
  msg  2/73 (11997 bytes) delivered
  msg  3/73 (11982 bytes) delivered
...

And we can poke through the folders and view the list of emails.

(Not Pictured) It looks like the content of all of the emails is the raw form of what’s on the server. Which isn’t a clean text copy of all of the emails, but a pretty ugly text file that contains the header, a text copy of the email an html copy of the email along with boundaries and a number of protocol things that are normally hidden when you open an email.

But in general the good news is that you have a raw copy of everything. And logging into my net-based mail client, it looks like all of my emails are still there. Which means that I was able to make a copy without deleting anything (yay). My experience aside, make sure you read everything and make sure to double check as I take no responsibility for any mistakes you might make while doing this.


Leave a Reply

Your email address will not be published. Required fields are marked *