1. Home
  2. Linux
  3. Back up email linux imap grab

How To Back Up Email On Linux With IMAP Grab

Need to back up email? Consider skipping the complicated GUI backup tools, and going with IMAP Grab instead.

SPOILER ALERT: Scroll down and watch the video tutorial at the end of this article.

Check Python Version

IMAP Grab is a Python script available on Github that allows users to directly “grab” IMAP mail to archive it later. To use this script, you’ll need to install Python 2.3.3 or later. To see if you have the correct version of Python on your Linux PC, open up a terminal and run a version check.

python --version

As long as it returns Python 2.3.3 or higher, you’re good to go. If you don’t have the required version of Python, consider updating your Linux PC’s packages.

Download IMAP Grab

When Python is taken care of, use the Git tool to clone the latest version of the IMAP Grab tool.

Note: this part of the tutorial requires the Git package. To install it, search for “git” and install it.

git clone https://github.com/ralbear/IMAPbackup.git

Using the CD command, move the terminal into the new “IMAPbackup” directory.

cd IMAPbackup

The script is on your PC, but it’s not going to work without the “Getmail” dependency.

Ubuntu

sudo apt install getmail

Debian

sudo apt-get install getmail

Arch Linux

sudo pacman -S getmail

Fedora

sudo dnf install getmail

OpenSUSE

sudo zypper install getmail

Using IMAP Grab

Before we can back up email, we’ll need to use IMAP Grab to list all available mailboxes inside of the email account. To do this, fill out this command. There are several aspects to this command. In the example, we’ve written out “test” URLs, users and passwords. Change the command following these steps.

First, change the server after -s to the IMAP server your email account uses. Not sure what the server address is? Guess by taking the root domain, and add “imap” in front of it. For example, Mail.com has an IMAP address of imap.mail.com. If that doesn’t work, you can always Google this information.

Next, change the test user after -u to your email address, and add your email account’s password after -p.

python imapgrab.py imapgrab.py -l -s imap.test.com -u testuser@test.com -p emailpassword

Run this command in the terminal, and the script will print out all available mailboxes.

Back Up Email

IMAP Grab can download email from any Mailbox folder. To download, first, create a directory where your email will download.

mkdir ~/email-backups

Then, use IMAP Grab to download everything. Like before, be sure to change -s-u, and -p with the correct information.

python imapgrab.py -d -v -M -f ~/email-backups -s imap.test.com -u test@test.com -p emailpassword -m "_ALL_"

Keeping “_ALL_” after the -m option in the command will tell the script to download absolutely everything in your email account. Most users should use “_ALL_”, especially if you don’t understand the script’s syntax. Another good reason to stick with this option is if you’ve got a lot of folders, and you don’t want to spend hours specifying everything in the command.

Note: To download only a specific folder from an email account with IMAP Grab, change “_ALL_” with some of the inbox folder names that the script lists in the previous section.

Encrypt Email Backup

Downloading your email to back it up is a great idea, especially if you want to always have a record of it offline. Unfortunately, these downloads are not private, and anyone can easily read your personal messages if they get access to your PC. If you’d like to create an encrypted archive of your email backup, follow these steps. First, use tar to create an archive of the email backup folder.

tar -jcvf email-backup.tar.bz2 email-backups

Depending on the size of your ~/email-backups folder, compression may take a bit of time. Let the compression tool run, and soon after you’ll have a new archive with all your messages in it. From here, use the GnuPG tool to start the encryption.

gpg -c email-backup.tar.bz2

Running gpg -c will prompt the user to set a password for the new encryption archive. Use a secure password that is memorable.  Encryption, like creating the archive, takes time to complete and the larger your backup is, the longer it will take to encrypt it. When the GPG encryption process finishes, you’ll notice an email-backup.tar.bz2.gpg file. This is the locked archive. Nobody will be able to access this file without the passcode you entered. It’s completely secure, so feel free to take email-backup.tar.bz2.gpg and upload it to Dropbox, Google Drive, home servers and etc.

Now that the backup archive is locked with GPG, we have no need for the unencrypted archive. Using the rm tool, delete it.

rm email-backup.tar.bz2

Decrypt Email

So, you’ve used GPG to lock up and secure your email backup. How do you decrypt it? Simple! Open up a terminal, and use the GPG decrypt command to unlock the archive.

gpg email-backup.tar.bz2.gpg

When the decryption command runs, you’ll need to enter the password you set. Once you do, email-backup.tar.bz2 will appear. At this point, you’ll be able to use the tar command to extract the emails from the archive.

tar -xvf email-backup.tar.bz2

4 Comments

  1. Great ! I can backup my IMAP email. Now I have a simplistic question: What about restore ?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.