Migrating from Dovecot passwd with bash

From Zimbra :: Wiki

Jump to: navigation, search

This script allows to migrate the virtual users from a Dovecot-style passwd file to Zimbra. It also parses an Exim-style aliases file with virtual domains.

The format of the passwd file used by Dovecot is described at http://wiki.dovecot.org/AuthDatabase/PasswdFile

The script makes the following assumptions:

  * your passwd file is /etc/dovecot.passwd
  * the aliases file is /etc/exim/aliases 
  * you are storing messages in mbox format (not Maildir!) under /home/mail/domainname/username (e.g. messages for info@foo.com are stored under /home/mail/foo.com/info/mbox)
  * you have installed mb2md under /usr/local/bin/mb2md . mb2md is a script to convert from mbox  to Maildir. It can be found at http://batleth.sapienti-sat.org/projects/mb2md/
  * you are using crypt() to encrypt the user passwords on the dovecot server

How to use:

The scripts generates a directory which must be copied to the server running Zimbra and a series of commands that must be run on Zimbra.

On the old server run:

./dovecot2zimbra.sh >/tmp/import-msgs.sh
scp -r /tmp/temp-copy user@zimbra-server:/tmp/temp-copy
scp /tmp/import-msgs.sh user@zimbra-server:/tmp

On the new Zimbra server run:

su - root
chown -R zimbra /tmp/import-msgs.sh /tmp/temp-copy
su - zimbra
bash /tmp/import-msgs.sh

Here is the code for dovecot2zimbra.sh:

#!/bin/bash

ACCTS="/etc/dovecot.passwd"
ALIASES="/etc/exim/aliases"
BASEDIR="/home/mail"
TEMPSTORE="/tmp/temp-copy"
ZMMBOX="/opt/zimbra/bin/zmmailbox"
ZMPROV="/opt/zimbra/bin/zmprov"
MB2MD="/usr/local/bin/mb2md"

# first create the domains
for i in $(cat $ACCTS|cut -d ':' -f 1|cut -d '@' -f 2|sort -u);do
        echo $ZMPROV createDomain $i
done

# parse pop3/imap accounts
for i in $(cat $ACCTS |cut -d ':' -f 1,2);do
        EMAIL=$(echo $i|cut -d ':' -f 1)
        NAME=$(echo $EMAIL|cut -d '@' -f 1)
        DOM=$(echo $EMAIL|cut -d '@' -f 2)
        PASS=$(echo $i|cut -d ':' -f 2)
        # create the account 
        echo $ZMPROV ca \'$EMAIL\' dssssfs3hsdyfgbsdgfbsd displayName \'$NAME\'
        # set the password
        echo $ZMPROV ma \'$EMAIL\' userPassword \'{crypt}$PASS\'|grep -v CRAM-MD5
        # convert from mbox to maildir
        mkdir -p $TEMPSTORE/$DOM/$NAME/Maildir/
        $MB2MD -s $BASEDIR/$DOM/$NAME/mbox -d $TEMPSTORE/$DOM/$NAME/Maildir/ 1>&2
        # import Maildir into Zimbra
        if [ -d $TEMPSTORE/$DOM/$NAME/Maildir/cur ]; then
		echo echo addMessage /INBOX $TEMPSTORE/$DOM/$NAME/Maildir/cur \| $ZMMBOX -z -m $NAME@$DOM
	fi
done

# parse aliases
for i in $(cat $ALIASES);do
        ORIG=$(echo $i|cut -d ':' -f 1)
        ORIGDOM=$(echo $ORIG|cut -d '@' -f 2)
        for j in $(echo $i|cut -d ':' -f 2|sed -e 's/,/ /g');do
                ALIASDOM=$(echo $j|cut -d '@' -f 2)
                if [ "$ORIGDOM" == "$ALIASDOM" ]; then
                        # alias
                        echo $ZMPROV aaa $j $ORIG
                else
                        # forward
                        echo $ZMPROV ca $ORIG dsfs123hsdyfgbsdgfbs$RANDOM
                        echo $ZMPROV ma $ORIG zimbraprefmailforwardingaddress $j
                fi
        done
done
Personal tools