I have a set of scripts that can do migrate all the mailboxes from 1 IMAP-server to the localhost or vice versa with 1 command.
Code:
pleskmailmigrate <domain>
If you want it to work with only that 1 command, you need to have your mailboxes setup on your Plesk server exactly as they are on the remote server.
It also assumes that the current server is not yet doing the mail.
The real work is done by imapsync
I just wrote 2 wrappers (one is wrapped in the other) so that it is simplified and works with Plesk.
I could post them, but they are a bit quick-and-dirty written and when things become custom, you need to do things a bit different.
pleskmailmigrate is the outer wrapper.
It checks with Plesk for the domain and loops through all the mailboxes and writes configuration files to be used for the 2nd wrapper (syncimap).
With the option -o (old) you can keep it from overwriting the configuration files (in case you manually edited them)
The wrapper syncimap is a simple wrapper for imapsync
It is for syncing a mailbox with the use of a config file (same name as email-adress placed in your home folder).
You of course need "imapsync"
cat /usr/local/sbin/syncimap
Code:
#!/bin/bash
if [ -z "$1" ] ; then
echo "You need to give a config file"
exit 1
fi
if [ ! -e ~/$1 ] ; then
echo "~/$1 does not exist!"
exit 1
fi
. ~/$1
if [ -z "HOST1" ] ; then
echo "HOST1 does not exist in ~/$1"
exit 1
fi
echo "$PASS1" >~/$1.secret1
echo "$PASS2" >~/$1.secret2
echo "Migrating mail from \"$USER1\" on host \"$HOST1\" to \"$USER2\" on host \"$HOST2\""
imapsync --host1 $HOST1 \
--user1 $USER1 --passfile1 ~/$1.secret1 \
--host2 $HOST2 \
--user2 $USER2 --passfile2 ~/$1.secret2 \
--syncinternaldates \
--noauthmd5 \
--useheader 'From' \
--useheader 'Subject' \
--useheader 'Message-id' \
--skipsize \
--subscribe \
${OPTION}
# --exclude 'Deleted|Trash' \
EXITCODE=$?
if [ ${EXITCODE} -eq 0 ] ; then
rm ~/$1.secret1
rm ~/$1.secret2
fi
exit ${EXITCODE}
cat /usr/local/sbin/pleskmailmigrate
Code:
#!/bin/sh
while getopts osrnd: OPTION
do
case $OPTION in
o) OLDCONFIGS=1;;
s) SSLFOREIGN=1;;
r) REVERSE=1;;
n) NODOMAIN=1;;
d) DAYS="$OPTARG";;
?) printf "Usage: %s: [-r] [-s] [-o] [-n] [-d <days>] <domain> \n" $0
exit 2;;
esac
done
shift $(($OPTIND - 1))
if [ -z "${1}" ] ; then
echo "You have to at least supply a domain" >&2
exit 1
fi
TMP1=`mktemp`
TMP2=`mktemp`
TMP3=`mktemp`
TMP4=`mktemp`
[ ${DAYS} ] && DAYS=`echo "${DAYS}" | tr -cd '0-9'`
# mysql -uadmin -p`cat /etc/psa/.psa.shadow ` psa -e 'select CONCAT(mail_name,"@",name) as email_address, substring(accounts.password, '1') from mail left join domains on domains.id=mail.dom_id left join accounts on accounts.id=mail.account_id;' | egrep ".+@$1" >${TMP1}
mysql -uadmin -p`cat /etc/psa/.psa.shadow ` psa -e 'select CONCAT(mail_name,"@",name) as email_address from mail left join domains on domains.id=mail.dom_id left join accounts on accounts.id=mail.account_id;' | egrep ".+@$1" >${TMP2}
/usr/local/psa/admin/bin/mail_auth_view | grep "@${domein}" | grep -f ${TMP2} | awk -F\| '{print $2" "$4}' >${TMP1}
awk '{print $1}' ${TMP1} | awk -F@ '{print $2}' | sort | uniq >${TMP2}
DOMAINS=`cat ${TMP2} | wc -l`
if [ ${DOMAINS} -eq 0 ] ; then
echo "This server has no email-addresses for domain $1"
exit 1
elif [ ${DOMAINS} -gt 1 ] ; then
echo "Ambiguous! This server has more than 1 domain containing $1 (`cat ${TMP2}`)" >&2
echo "\"`cat ${DOMAINS}`\"" >&2
exit 1
fi
DOMAIN=`cat ${TMP2}`
FAILED=0
MIGRATED=0
if [ -z $2 ] ; then
MX=`host -t MX ${DOMAIN} | egrep -o 'handled by [0-9]+ .*' | head -n1 | awk '{print $4}' `
if [ ! -z "${MX}" ] ; then
MAILIP=`host -t A ${MX} | egrep -o 'has address .*' | head -n1 | awk '{print $3}'`
if [ ! -z "${MAILIP}" ] ; then
if ifconfig | grep 'inet' | grep -q " ${MAIL} " ; then
echo "If this server is already handling the mail, then you have to supply a domain to sync" >&2
exit 1
fi
fi
fi
else
HOSTRAW=`echo $2 | tr 'A-Z' 'a-z'`
HOST="`echo "${HOSTRAW}" | tr -cd 'a-z0-9.-' | egrep '.+\..+'`"
if [ "${HOST}" = "${HOSTRAW}" ] ; then
MAILIP=`echo $2 | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'`
[ -z "${MAILIP}" ] && MAILIP=`host -t A $2 2>/dev/null | egrep -o 'has address .*' | head -n1 | awk '{print $3}'`
else
echo "\"${HOSTRAW}\" is not a valid domain or IP-address (${HOST})"
fi
fi
# Make it easy for accounts at gmail
if echo "${MX}" | grep -q 'google' ; then
SSLFOREIGN=1
MAILIP=imap.gmail.com
fi
if [ -z "${MAILIP}" ] ; then
echo "Unable to resolve the foreign mailserver" >&2
else
echo ${DOMAIN}
while read CREDENTIAL ; do
LOGINHERE="`echo ${CREDENTIAL} | awk '{print $1}'`"
PASS="`echo ${CREDENTIAL} | awk '{print $2}'`"
if [ -z "${CREDENTIAL}" ] ; then
echo "Error, unable to fetch the credentials of \"${LOGINHERE}\""
let FAILED+=1
echo "${LOGINHERE}" >>$TMP4
else
LOGINTHERE="${LOGINHERE}"
[ ${NODOMAIN} ] && LOGINTHERE="`echo ${CREDENTIAL} | awk '{print $1}' | awk -F@ '{print $1}'`"
FILE=~/`echo "${LOGINHERE}" | sed 's/@/\\@/'`
if [ ! -f ${FILE} ] || [ ! ${OLDCONFIGS} ] ; then
echo -e "HOST1=${MAILIP}\nUSER1=${LOGINTHERE}\nPASS1=\"${PASS}\"\nHOST2=127.0.0.1\nUSER2=${LOGINHERE}\nPASS2=\"${PASS}\"" >${FILE}
OPTIONS='--automap'
OPTIONS=''
echo "${HOSTRAW}" | grep -q 'gmail' && OPTIONS='--exclude \"\\[Gmail\\]$\" ${OPTIONS}'
[ ${SSLFOREIGN} ] && OPTIONS="--ssl1 ${OPTIONS}"
[ ${DAYS} ] && OPTIONS="--maxage ${DAYS} ${OPTIONS}"
if [ -z "${OPTIONS}" ] ; then
echo -e "#OPTION=\"--maxage 1 \"" >>${FILE}
else
echo -e "OPTION=\"${OPTIONS}\"" >>${FILE}
fi
if [ ${REVERSE} ] ; then
sed -i 's/1=/0=/' ${FILE}
sed -i 's/2=/1=/' ${FILE}
sed -i 's/0=/2=/' ${FILE}
sed -i 's/ssl1/ssl2/' ${FILE}
fi
fi
syncimap "${LOGINHERE}" 2>>${TMP3}
if [ $? -ne 0 ] ; then
let FAILED+=1
echo "${LOGINHERE}" >>$TMP4
else
let MIGRATED+=1
fi
fi
done <${TMP1}
echo -e "\n\n\n\nMigrated ${MIGRATED} successfully for ${DOMAIN}"
if [ ${FAILED} -ne 0 ] ; then
echo -e "${FAILED} accounts failed:"
cat ${TMP3}
cat ${TMP4}
fi
fi
rm ${TMP1}
rm ${TMP2}
rm ${TMP3}
rm ${TMP4}