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