Migrando e-mails entre provedores com ImapSync (Script)

porDiego Elcain

Migrando e-mails entre provedores com ImapSync (Script)

Muitas vezes aparecem a árdua tarefa de ter de migrar várias caixas postais de um mesmo domínio entre provedores diferentes, para essa tarefa usamos o Imap Sync que pode tanto ser usado um à um ou via script otimizando a tarefa.

Via Linha de Comando

imapsync --host1 srv.origem.com  --user1 user01@exemple.com --password1 pwd_exemple \
 --host2 srv.dest.com --user2 user1@exemple.com --password2 pwd_exemple

Basicamente com essa linha ja coneseguimos conectar aos dois provedores inciar a transição de e-mails, mas coso use o Gmail é necessário uma conexão SSL para isso insira o parametro --ssl1 e --ssl2

imapsync --host1 srv.origem.com  --user1 user01@exemple.com ssl1  --password1 pwd_exemple \
 --host2 srv.dest.com --user2 user1@exemple.com --ssl2  --password2 pwd_exemple

 

Via Script

Crie um arquivo chamado imapsync-accounts.txt e insira o username sem o @ um ponto e virgula e a senha

user1;password1
user2;password2
#...
user9;password9
user10;password10

No mesmo diretório crie o script imapsync-batch.sh com permissão de execução com o seguinte conteúdo .

imapsync-batch.sh

#!/bin/bash

####################################################
# File name - imapsync-batch.sh
# Description - Batch IMAP to IMAP sync
# Creation - DEC 06 2010 - BN
# Modified - DEC 13 2010 - BN
# Requires - imapsync-accounts.txt
# Requires - Username/password to be the same on both sides
#          - may change this if I ever need it to backup to different account.
####################################################


SERVERNAME=$HOSTNAME
SCRIPT_NAME="$SERVERNAME - Batch IMAP TO IMAP"
MAIL=/bin/mail;
MAIL_RECIPIENT="deadmail AT mrbuckykat.com"
LOCK_FILE="/tmp/$SERVERNAME.imapsync.lockfile"
LOGFILE="imapsync_log.txt"


#host1 is Source
HOST1=mail.somedomain.com


#host2 is Dest
HOST2=mail2.somedomain.com


#domain is where email account is "every thing after the @"
DOMAIN=somedomain.com

####################################################
###### Do not modify past here
####################################################

if [ ! -e $LOCK_FILE ]; then
touch $LOCK_FILE
#Run core script

TIME_NOW=$(date +"%Y-%m-%d %T")
echo "" >> $LOGFILE
echo "------------------------------------" >> $LOGFILE
echo "IMAPSync started - $TIME_NOW" >> $LOGFILE
echo "" >> $logfile

{ while IFS=';' read u1 p1; do
USER_NAME=$u1"@"$DOMAIN
echo "Syncing User $USER_NAME"
TIME_NOW=$(date +"%Y-%m-%d %T")
echo "Start Syncing User $u1"
echo "Starting $u1 $TIME_NOW" >> $LOGFILE
imapsync --nosyncacls --syncinternaldates --host1 $HOST1 --user1 "$USER_NAME" --password1 "$p1" --host2 $HOST2 --user2 "$USER_NAME" --password2 "$p1" --noauthmd5
TIME_NOW=$(date +"%Y-%m-%d %T")
echo "User $USER_NAME done"
echo "Finished $USER_NAME $TIME_NOW" >> $LOGFILE
echo "" >> $LOGFILE
done ; } < imapsync-accounts.txt
TIME_NOW=$(date +"%Y-%m-%d %T")
echo "" >> $LOGFILE
echo "IMAPSync Finished - $TIME_NOW" >> $LOGFILE
echo "------------------------------------" >> $LOGFILE

#End core script
#uncomment if you want a email once script is finished - useful for big syncs
#echo " IMAPSync Finished" | $MAIL -s "[$SCRIPT_NAME] Finshed" $MAIL_RECIPIENT
rm -f $LOCK_FILE


else
TIME_NOW=$(date +"%Y-%m-%d %T")
echo "$SCRIPT_NAME at $TIME_NOW is still running" | $MAIL -s "[$SCRIPT_NAME] !!WARNING!! still running" $MAIL_RECIPIENT
echo "$SCRIPT_NAME at $TIME_NOW is still running"
fi
Aqui Script já automatizado para contas do Google Mails 
#!/bin/bash
SERVERNAME=$HOSTNAME
SCRIPT_NAME="$SERVERNAME - Batch IMAP TO IMAP"
MAIL=/bin/mail;
MAIL_RECIPIENT="deadmail AT mrbuckykat.com"
LOCK_FILE="/tmp/$SERVERNAME.imapsync.lockfile"
LOGFILE="imapsync_log.txt"

HOST1=imap.gmail.com
HOST2=191.252.134.207
DOMAIN=minhaempresa.com.br

####################################################
###### Do not modify past here
####################################################

if [ ! -e $LOCK_FILE ]; then
 touch $LOCK_FILE
 #Run core script

TIME_NOW=$(date +"%Y-%m-%d %T")
 echo "" >> $LOGFILE
 echo "------------------------------------" >> $LOGFILE
 echo "IMAPSync started - $TIME_NOW" >> $LOGFILE
 echo "" >> $logfile

{ while IFS=';' read u1 p1; do
 USER_NAME=$u1"@"$DOMAIN
 echo "Syncing User $USER_NAME"
 TIME_NOW=$(date +"%Y-%m-%d %T")
 echo "Start Syncing User $u1"
 echo "Starting $u1 $TIME_NOW" >> $LOGFILE

imapsync --host1 $HOST1 --ssl1 --user1 "$USER_NAME" --password1 "$p1" --host2 $HOST2 --ssl2 --user2 "$USER_NAME" --password2 "$p1" \
 --noauthmd5 --maxbytespersecond 10000 --automap --exclude "\[Gmail\]$"

TIME_NOW=$(date +"%Y-%m-%d %T")
 echo "User $USER_NAME done"
 echo "Finished $USER_NAME $TIME_NOW" >> $LOGFILE
 echo "" >> $LOGFILE
 done ; } < imapsync-accounts.txt
 TIME_NOW=$(date +"%Y-%m-%d %T")
 echo "" >> $LOGFILE
 echo "IMAPSync Finished - $TIME_NOW" >> $LOGFILE
 echo "------------------------------------" >> $LOGFILE

#End core script
 #uncomment if you want a email once script is finished - useful for big syncs
 #echo " IMAPSync Finished" | $MAIL -s "[$SCRIPT_NAME] Finshed" $MAIL_RECIPIENT
 rm -f $LOCK_FILE

else
 TIME_NOW=$(date +"%Y-%m-%d %T")
 echo "$SCRIPT_NAME at $TIME_NOW is still running" | $MAIL -s "[$SCRIPT_NAME] !!WARNING!! still running" $MAIL_RECIPIENT
 echo "$SCRIPT_NAME at $TIME_NOW is still running"
 fi

Sobre o Autor

Diego Elcain administrator