#!/bin/bash
###############################################################
#                                                            
#  Thanks to hhtp://www.birdbrained.org/dbmail.php for the    
#  info about importing messages from mbox to DBMAIL          
#                                                             
###############################################################
#
#  uw2dbmail.sh  (Version: 2002/11/28)                                
#
#  Somebody ask me on this messages list if i had a script
#  to convert mbox to DBMAIL. I did not have any at the time
#  and told him i was going to try to find or write one.  
#
#  This is a script to start migrating a UW-IMAP mail server
#  to DBMAIL im posting this script for everyone to try
#
#  This script has succesfully migrated 3229 users
#  and 13240 email messages in 70 minutes on a dual 400 mhz
#  pentium II server using redhat 7.3.           
#
#  Please try this script on a test server
#
#  Jacques-Beaudoin@cspi.qc.ca                                
#
############################################################### 
#
#  This script as 2 sections 
#  Auther sections will be added if i nead them
#
#  Section-1: Migrating users and users-mail-inbox to DBMAIL
#  Section-2: Migrating postfix aliases to DBMAIL 
#
###############################################################
#
#  To do before executing this script                          
#                                                             
#  1: Make shure postfix and mysql are running                           
#  2: Make shure you have the procmail "formail" utility      
#     in directory /usr/bin                                   
#  3: Create a $WORK directory (I use /work). You will put all
#     the data files in subdirectories.
#  4: Create a $WORK/mail subdirectory  
#  5: Create a $WORK/alisases subdirectory
#  6: Copy this script in the /root directory                  
#  7: Copy /etc/passwd and /etc/shadow file from your existing
#     UW-IMAP mail server to the $WORK directory. 
#  8: Edit the $WORK/passwd file and delete users that are    
#     not mail users (root,bin,...etc)                        
#  9: Make shure this script has the execute permissions 
# 10: Copy all /var/spool/mail files from your existing UW-IMAP
#     server to the $WORK/mail directory 
# 11: Copy all postfix aliases files in the $WORK/aliases 
#     directory and delete any blank lines at the end of each 
#     aliases files because they will create bogus aliases.
#     NB: If you dont want to create aliases in DBMAIL 
#         dont put any files in the $WORK/alisases directory
#         and section-2 will do nothing                                
# 12: Set the SERVER_NAME and the WORK variable that fallows                           
# 13: To execute change to the /root directory
#     and type uw2dbmail.sh
#
###############################################################
#
SERVER_NAME="mail.yourname.ca"
WORK="/work"
#
###############################################################
#
#  Section-1: Migrating users and users-mail-inbox to DBMAIL
#
###############################################################
#
#  To change to the $WORK directory
#
cd $WORK
#
#  A loop to process all the USER in the $WORK/passwd file
#
while read RECORD 
do
USER=`echo $RECORD|cut -d':' -f1`   # To get $USER from $WORK/passwd
NAME=`echo $RECORD|cut -d':' -f5`   # To get $NAME from $WORK/passwd 
echo "=============================================================="
echo "Processing user [$USER / $NAME]"
echo "=============================================================="
#
#  To create the user in DBMAIL with a temporary password "temp" 
#  and 10 megabytes quota
#
dbmail-adduser a $USER temp 0 10M $USER@$SERVER_NAME
#
#  To change the user password to is crypted password 
#  from the $WORK/shadow file   
#
dbmail-adduser c $USER -P:$WORK/shadow
#
#  I keep a clear text password of my users in the name field of
#  "passwd" file if i want to store this password uncrypted in dbmail 
#  I uncomment the next 3 lines. I do this only if the name field is not
#  equal to "secret". You probebly dont have to do this so keep 
#  the next 3 lines commented
#
#if [ ! $NAME = "secret" ];
#then dbmail-adduser c $USER -p $NAME
#fi
#
#  To transfert all INBOX mail from directory $WORK/mail
#  to dbmail for this $USER
#
echo "Adding mail  files for user [$USER]"
cat $WORK/mail/$USER | /usr/bin/formail -s /usr/local/sbin/dbmail-smtp -m "INBOX" -u $USER
#
#  Continue with next user 
#
done<passwd
#
###############################################################################
#
#  Section-2: Migrating postfix aliases to DBMAIL  
#
###############################################################################
#
#  To create a file named $WORK/files containing all  
#  the files names in the $WORK/aliases directory
#
dir -1 $WORK/aliases > $WORK/files
#
#  2 loops to process all users in all aliases-files 
#
while read ALIAS 
do
     while read USER
     do
     dbmail-adduser f $ALIAS@$SERVER_NAME $USER@$SERVER_NAME
     done<$WORK/aliases/$ALIAS  # Continue with next user in this alias file  
done<$WORK/files                # Continue with next file-name  
#
#  Pause 60 secondes before terminating
#
sleep 60
exit 0
###################
#  End of script  #
###################
