How to export e-mails from DBMail mailserver to Maildir format in a Linux VPS

Datetime:2016-08-22 23:52:37          Topic:          Share

In this article we are going through the steps on how to export e-mails from DBMail mailserver to Maildir format in aLinux VPS . More specifically, the tutorial explains how to export the e-mails from an SQL database to Mbox format and then convert the Mbox e-mails to Maildir.

Let’s say a word or two for each of the components being involved, shall we?

What is DBMail?

DBMailprovides fast and scalable SQL based mail services. It stores the mail messages in a relational database such as MySQL, PostgreSQL, Oracle, and SQLite. It has the following frontends IMAP4, POP3, ManageSieve and LMTP

What is Mbox?

Mboxfile is the most common format for storing email messages on a hard drive. All the messages for each mailbox are stored as a single, long, text file in a string of concatenated e-mail messages, starting with the From header of the message. MBOX files were used predominantly on Unix.

What is Maildir?

Maildire-mail format is a common way of storing e-mail messages, where each message is kept in a separate file with a unique name, and each folder is a directory. The local filesystem handles file locking as messages are added, moved and deleted.

0. SSH TO YOUR DBMAIL LINUX VPS

First thing to do is to login to your dbmail virtual server via SSH and optionally fire up a screen/tmux session. For example:

ssh YOUR_VPS_IP -p YOUR_VPS_SSH_PORT
screen -U -S dbmail-export-screen

1. EXPORT EMAILS FROM DBMAIL

To export emails from DBMAIL, we’re going to use the dbmail-export tool provided by DBMAIL itself. The tool supports multiple arguments and parameters (see dbmail-export --help ), but in this case we’re going to export all emails for a given account and period of time and also mark these emails as DELETED so dbmail-util can purge them from the database.

First off, let’s setup some shell variables such as EXPORT_USER , EXPORT_DIR , DATE_FROM and DATE_TO :

EXPORT_USER=foo.bar
EXPORT_DIR="/home/${EXPORT_USER}__dbmail_mail_export"
DATE_FROM='1-May-2016'
DATE_TO='1-Jul-2016'

once the variables are set, run the following commands:

mkdir -p "${EXPORT_DIR}"
cd "${EXPORT_DIR}"

and then execute the dbmail-export tool to export the emails from the database:

dbmail-export -v -D -u "${EXPORT_USER}" -b "${EXPORT_DIR}" -s "1:* SINCE ${DATE_FROM} BEFORE ${DATE_TO}"

keep in mind though, that you may want to adjust the parameters used according to your needs. for example, if you want to export all emails for a given user not just for some period of time, you can safely omit the -s argument and its parameter

if everything went fine, you should have the emails exported in MBOX format within ${EXPORT_DIR} . What you have to do now is to convert the exported emails from MBOX to Maildir format using a simple PERL script.

Navigate to your home user directory and download the mb2md script using the following commands:

cd -
wget https://vpsineu.com/wget/mb2md.pl
chmod +x mb2md.pl

Once the script is downloaded, convert the exported dbmail e-mails from MBOX to Maildir by executing:

./mb2md.pl -s "${EXPORT_DIR}/${EXPORT_USER}" -R -d "${EXPORT_DIR}/Maildir"

verify the Maildir tree is OK:

ls -laht "${EXPORT_DIR}/Maildir"

2. TRANSFER THE EXPORTED EMAILS

Next, setup some other shell variables such as ‘destination host’, ‘destination user’ and ‘destination path’:

ARCHIVE_HOST=1.2.3.4
ARCHIVE_USER=foo.bar
ARCHIVE_PATH="/home/${ARCHIVE_USER}/${EXPORT_USER}_Maildir"

and then transfer the Maildir emails to your another server using your preferred tool ( rsync , scp , ftp etc.). For example, we are using rsync over SSH as in:

rsync -Wav -e 'ssh -p22' "${EXPORT_DIR}/Maildir/" "${ARCHIVE_USER}"@"${ARCHIVE_HOST}":"${ARCHIVE_PATH}/"

3. FINIALIZE THE MIGRATION

Now, SSH to the archive/another mailserver and finalize the email migration by moving the Maildir directory to the configured emails location and setup proper permissions and ownership:

rsync -Wav /path/to/the/transfered/maildir/ /var/vmail/foo.bar/Maildir/
chown -R vmail:vmail /var/vmail/foo.bar/Maildir/

of course, you have to tune the above paths to match your setup

4. ACCESS THE ARCHIVED EMAILS

The final step is to try to access the migrated emails via your webmail system or by using an IMAP client like Thunderbird for example.

If you’re one of ourLinux VPS Hosting customers we can help you export your e-mails from DBMail mailserver to a Maildir format on yourvirtual server free of charge. Justcontact us and some of our experts will complete your request immediately.