253c0b7
#!/bin/bash
253c0b7
253c0b7
# author: G.R.Keech <rkeech@redhat.com>
253c0b7
# name:   migrate-folders
253c0b7
# date:   2004-10-20
253c0b7
253c0b7
# This script assists in the conversion of mail boxes
253c0b7
# in mbox format to maildir format.
253c0b7
# See also migrate-users.
253c0b7
253c0b7
# Applicability.
253c0b7
#
253c0b7
# This script is intended for the common case on Red Hat systems
253c0b7
# where mail users have mail folders in their home directories
253c0b7
# under /home, and have inboxes in /var/spool/mail/
253c0b7
#==================================================================
253c0b7
# Change the value of the elements in this section as required.
253c0b7
253c0b7
#This is a list of folders, one per line.  This does not
253c0b7
#include the inboxes.  This might be prepared starting with
253c0b7
#the output of "find /home -type d".
253c0b7
FOLDERLIST=/root/migrate/folderlist
253c0b7
# folder is the existing mbox folder being migrated.
253c0b7
# It is a path under /home.
253c0b7
# eg if oldfolder is  fred/personal, then folder is personal,
253c0b7
# user is fred.
253c0b7
253c0b7
#Specify the location of the new location for mail folders.
253c0b7
#This cannot be the same as the old location because it will
253c0b7
#create directory names that contend with existing file names.
253c0b7
NEWBASE=/var/spool/mail2
253c0b7
253c0b7
#The script to convert invidual mail folders to maildir format.
253c0b7
#http://perfectmaildir.home-dn.net/
253c0b7
FOLDERCONVERT=/usr/local/bin/perfect_maildir.pl 
253c0b7
253c0b7
#This is a list of users to have their mail folders created.
253c0b7
#One user per line.
253c0b7
#Suggest create with cut -d: -f1 /etc/passwd > ~/migrate/u1
253c0b7
#then remove inappropriate entries by hand.
253c0b7
USERLIST=/root/migrate/userlist
253c0b7
253c0b7
# Detailed migration information is sent to this file
253c0b7
MIGRATELOG=/tmp/foldermigrationlog-$(date -I) 
253c0b7
#=================================================================
253c0b7
echo 
253c0b7
echo "Have you created the users' mail directories yet? (y/n)"
253c0b7
echo
253c0b7
read ans
253c0b7
if [ "$ans" != "y" ]
253c0b7
then
253c0b7
  echo Good Bye.
253c0b7
  echo use the migrate-users script first.
253c0b7
  exit 0
253c0b7
fi
253c0b7
echo
253c0b7
echo This will copy existing mbox-style mail folders listed
253c0b7
echo in the file $FOLDERLIST.  Maildir-style folders will
253c0b7
echo be created under $NEWBASE
253c0b7
echo
253c0b7
echo "Do you want to continue? (y/n)"
253c0b7
read ans
253c0b7
if [ "$ans" != "y" ]
253c0b7
then
253c0b7
  echo Good Bye.
253c0b7
  exit 0
253c0b7
fi
253c0b7
echo
253c0b7
echo Note: Detailed folder migration information will be sent to $MIGRATELOG
253c0b7
echo
253c0b7
echo Press enter to start
253c0b7
read ans
253c0b7
253c0b7
if [ ! -x ${FOLDERCONVERT} ]
253c0b7
then
253c0b7
  echo Error: file ${FOLDERCONVERT} is not available to execute.
253c0b7
  exit 1
253c0b7
fi
253c0b7
253c0b7
if [ ! -d ${NEWBASE} ]
253c0b7
then
253c0b7
  echo Error: directory $NEWBASE does not exist
253c0b7
  exit 1
253c0b7
fi
253c0b7
253c0b7
if [ ! -f "${USERLIST}" ]
253c0b7
then
253c0b7
  echo Error: user list file \"$USERLIST\" does not exist.
253c0b7
  exit 1
253c0b7
fi
253c0b7
253c0b7
253c0b7
#-----------------------------------------------------------------
253c0b7
echo
253c0b7
echo Testing that the base of the folderlist entries corresponds to usernames
253c0b7
while read oldfolder
253c0b7
do
253c0b7
  user="$(dirname "$oldfolder")"
253c0b7
  if grep ^${user}: /etc/passwd &> /dev/null
253c0b7
  then
253c0b7
    echo -n .
253c0b7
  else
253c0b7
    echo User \"$user\": is bogus.
253c0b7
    echo The string \"$user\" from the file \"$FOLDERLIST\" needs to
253c0b7
    echo correspond exactly to a username.  Edit the file accordingly.
253c0b7
    exit 1
253c0b7
  fi
253c0b7
done < $FOLDERLIST
253c0b7
echo
253c0b7
echo PASS
253c0b7
echo
253c0b7
nusers=$(wc -l $USERLIST | awk '{ print $1 }' )
253c0b7
n=1
253c0b7
#-----------------------------------------------------------------
253c0b7
# Iterate through user list and migrate folders.
253c0b7
while read user
253c0b7
do
253c0b7
  #-----------------------------------------------------------------
253c0b7
  # Step 1: Check stuff
253c0b7
  if grep ^${user}: /etc/passwd &> /dev/null
253c0b7
  then
253c0b7
    echo -n "$n / $nusers : User \"$user\" is OK: "
253c0b7
    n=$(( $n + 1 ))
253c0b7
    echo "User \"$user\"" >> $MIGRATELOG
253c0b7
253c0b7
    inbox=/var/spool/mail/${user}
253c0b7
253c0b7
    if [ \( ! -f "${inbox}" \) -o \( ! -s "${inbox}" \) ]
253c0b7
    then
253c0b7
	echo User \"${user}\" has no inbox to convert.
253c0b7
    else
253c0b7
      #-----------------------------------------------------------------
253c0b7
      # Step 2: Migrate user inboxes from /var/spool/mail/.
253c0b7
      newdir="${NEWBASE}/${user}/"
253c0b7
      $FOLDERCONVERT "$newdir" < "${inbox}" >> $MIGRATELOG 2>&1
253c0b7
      chown -R ${user}:mail "${newdir}"
253c0b7
      find "$newdir" -type f  -exec chmod 600 {} \;
253c0b7
      echo -n  " inbox "
253c0b7
    fi
253c0b7
    #-----------------------------------------------------------------
253c0b7
    # Step 3: Migrate other mail folders from user home directories.
253c0b7
    while read oldfolder
253c0b7
    do
253c0b7
      folder=$(basename "${oldfolder}")
253c0b7
      fuser="$(dirname "$oldfolder")"
253c0b7
253c0b7
      if [ "$user" = "$fuser" ]
253c0b7
      then
253c0b7
	if [ ! -f "/home/${oldfolder}" ]
253c0b7
	then
253c0b7
	  echo Error folder \"${folder}\" does not exist.
253c0b7
	  break
253c0b7
	fi
253c0b7
253c0b7
	if [ ! -d ${NEWBASE}/${fuser} ]
253c0b7
	then
253c0b7
	    echo Error ${NEWBASE}/${fuser} does not exist.
253c0b7
	    break
253c0b7
	fi
253c0b7
253c0b7
	newdir="${NEWBASE}/${fuser}/.$folder"
253c0b7
	mkdir -p "$newdir"/cur
253c0b7
	mkdir -p "$newdir"/new
253c0b7
	mkdir -p "$newdir"/tmp
253c0b7
	chmod -R 770  "${newdir}"
253c0b7
	$FOLDERCONVERT "$newdir" < "/home/$oldfolder" >> $MIGRATELOG 2>&1
253c0b7
	chown -R ${user}:mail "${newdir}"
253c0b7
        #chmod 600 "$newdir/cur/*"
253c0b7
	find "$newdir" -type f  -exec chmod 600 {} \;
253c0b7
  
253c0b7
	echo "$folder" >> ${NEWBASE}/${fuser}/.subscriptions
253c0b7
	chmod 600 ${NEWBASE}/${fuser}/.subscriptions
253c0b7
	chown ${fuser}:mail ${NEWBASE}/${fuser}/.subscriptions
253c0b7
	
253c0b7
	echo -n .
253c0b7
      fi
253c0b7
    done < $FOLDERLIST
253c0b7
    echo
253c0b7
253c0b7
  else
253c0b7
    echo User "$user: is bogus."
253c0b7
  fi
253c0b7
253c0b7
done < $USERLIST
253c0b7
253c0b7
echo
253c0b7
echo
253c0b7
echo To make the new base mail directory active, change the
253c0b7
echo mail_spool_directory setting for postfix using
253c0b7
echo postconf -e \"mail_spool_directory = ${NEWBASE}/\"
253c0b7
echo and change Dovecots default_mail_env setting in
253c0b7
echo /etc/dovecot.conf to
253c0b7
echo default_mail_env = maildir:${NEWBASE}/%u
253c0b7
echo
253c0b7