Moving a Postfix virtual user’s mail to a new address

A user was on too many spam lists and wanted a new mailbox.  He had removed all the spam, and wanted all the existing messages moved over.  The hosting server runs Postfix in a virtual setup.  The script below is what I used.  It makes it easy to move mailboxes in the future for my client.  The script takes two options, and expects the first to be the old email address, the second to be the new email address.  It could stand some error checking, etc., but since I expect it to be run by a clue-possessing administrator, I didn’t put any in here.  This script assumes that the destination mailbox has already been created (in our case, by using PostfixAdmin).

 

#!/bin/bash
mailroot="/var/spool/postfix/virtual"
usersource=$1
userdest=$2
sourcedir="${mailroot}/${usersource}"
destdir="${mailroot}/${userdest}"
echo "Source dir is ${sourcedir}"
echo "Dest dir is ${destdir}"
read -p "Press any key to begin" 
cd ${sourcedir}
for i in *; do
    mv -v $i ${destdir}/
done
cd -