If you need to move all your email from one server to another, an easy and quick way is by using imapsync. Below is a simple script which will ask for server names, credentials and run imapsync. This might not work for you, so please check the imapsync manual page for different options, and remember to test!
#!/bin/sh
# Need imapsync command (eg. apt-get install imapsync)
# Server to sync FROM
echo -n "Enter SOURCE server hostname: "
read SRC_SERVER
echo -n "Enter SOURCE username: "
read SRC_USER
echo -n "Enter SOURCE password: "
read SRC_PASS
# Server to sync TO
echo -n "Enter DESTINATION server hostname: "
read DST_SERVER
echo -n "Enter DESTINATION username: "
read DST_USER
echo -n "Enter DESTINATION password: "
read DST_PASS
# Echo values
echo "Will sync $SRC_USER @ $SRC_SERVER to $DST_USER @ $DST_SERVER"
echo "Press CTRL-C to abort or wait 5 seconds to continue ... "
sleep 5
imapsync --syncinternaldates --skipsize --useheader subject --useheader data \
--host1 $SRC_SERVER --user1 "$SRC_USER" --password1 "$SRC_PASS" --authmech1 'LOGIN' \
--host2 $DST_SERVER --user2 "$DST_USER" --password2 "$DST_PASS" --authmech2 'LOGIN'