qmail: move Plesk mailboxes to a new server

You can use the following when you have a Plesk installation on two servers and need to move mailboxes from one server to another one.

Note that it assumes that this is all done at a time when there is no activity on the system. If there is no such timeframe in your case, you’ll probably need to do a little more work making sure you do not lose any incoming email in the process.

First, I assume that the domain has already been created on the new server but the DNS entries still point to the old server. Now you need to create a script to create the mailboxes. This can be done by a simple SQL script run against the old psa database:

use psa;
SELECT CONCAT("/opt/psa/bin/mail --create ",mail . mail_name,"@",domains . name ," -cp-access true -passwd ",accounts . password," -mbox_quota -1 -mailbox true")
FROM `mail` , accounts , domains
WHERE
mail . account_id =accounts . id AND
mail . dom_id =domains . id AND
domains . name = "my-domain-name.com"
ORDER BY `mail` . `postbox`  ASC;

If Plesk is not installed in /opt/psa, you’ll have to replace it in the SELECT statement by the path to your Plesk installation e.g. /usr/local/psa.

This has to be executed in a mysql client e.g.:

mysql -uadmin -p`cat /etc/psa/.psa.shadow`

It will return something like:

/opt/psa/bin/mail --create doc@my-domain-name.com -cp-access true -passwd mypassword1 -mbox_quota -1 -mailbox true
/opt/psa/bin/mail --create post@my-domain-name.com -cp-access true -passwd mypassword2 -mbox_quota -1 -mailbox true

Here, we have two user mailboxes for this domain: doc and post.

Here some information regarding the options used:

  • –create: it creates a Plesk e-mail account
  • doc@my-domain-name.com: this is the email address you’ll create
  • -cp-access true: enables the mail name user to Plesk control panel
  • -passwd mypassword1: sets the same password as on the old server
  • -mbox_quota -1: Sets the size of the mail box for the mail name to the default mail box size value set for the domain to which the mail name belongs
  • -mailbox true: creates mail box for the mail name

Now the mailboxes also exist on the new server and you just need to copy the files from one server to another. I copy them through my local machine using cp but you could also use rsync instead or directly cp from one server to the other one. I execute the following on my local computer:

mkdir /tmp/qmail
cd /tmp/qmail

rm -rf cur new
scp -r root@myoldserver:/var/qmail/mailnames/my-domain-name.com/doc/Maildir/cur .
scp -r cur root@mynewserver:/var/qmail/mailnames/my-domain-name.com/doc/Maildir/
scp -r root@myoldserver:/var/qmail/mailnames/my-domain-name.com/doc/Maildir/new .
scp -r new root@mynewserver:/var/qmail/mailnames/my-domain-name.com/doc/Maildir/

rm -rf cur new
scp -r root@myoldserver:/var/qmail/mailnames/my-domain-name.com/post/Maildir/cur .
scp -r cur root@mynewserver:/var/qmail/mailnames/my-domain-name.com/post/Maildir/
scp -r root@myoldserver:/var/qmail/mailnames/my-domain-name.com/post/Maildir/new .
scp -r new root@mynewserver:/var/qmail/mailnames/my-domain-name.com/post/Maildir/

Replace myoldserver by the name or IP address of the old server and mynewserver by the name or IP address of the new server. I have configure both servers to accept login with a public key. If it is not the case for you, you’ll have to enter a few passwords.

If you have more than two mailboxes, you will need to repeat the five lines more than twice.

The new directory contains new emails not yet read and the cur directory contains emails already read.

After that, all you need is to make that the writes are fine. To do this execute the following on the new server:

chown -R popuser:popuser /var/qmail/mailnames/my-domain-name.com/doc/Maildir/cur
chown -R popuser:popuser /var/qmail/mailnames/my-domain-name.com/doc/Maildir/new

chown -R popuser:popuser /var/qmail/mailnames/my-domain-name.com/post/Maildir/cur
chown -R popuser:popuser /var/qmail/mailnames/my-domain-name.com/post/Maildir/new

After that change the DNS setting for mail.my-domain-name.com to point to the new server. The propagation of the DNS server is usually fast but can sometimes take many hours. It’s all a matter of luck…

2 thoughts on “qmail: move Plesk mailboxes to a new server

  1. Hey man!

    You saved me! I could not find anywhere a post on migrating email accounts from one plesk to another.

    This did it!

    PS: Is there a reason why not to copy the entire /var/qmail/mailnames/my-domain-name.com/ folder? instead of the two you have copied.

    Thanks!
    Alex.

    1. Hi Alex, there is no real reason why I’ve only copied the two directories. Not too sure where it could cause problems to copy the whole directory. But since copying the two subdirectories was enough for me, I only copied them. Just wanted to limit the chances to have problems by copying too much.

Leave a Reply

Your email address will not be published.