migrate_partition_mail_data   [plain text]


#!/bin/bash
#
# Copyright (c) 2010-2011 Apple Inc. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without  
# modification, are permitted provided that the following conditions  
# are met:
# 
# 1.  Redistributions of source code must retain the above copyright  
# notice, this list of conditions and the following disclaimer.
# 2.  Redistributions in binary form must reproduce the above  
# copyright notice, this list of conditions and the following  
# disclaimer in the documentation and/or other materials provided  
# with the distribution.
# 3.  Neither the name of Apple Inc. ("Apple") nor the names of its  
# contributors may be used to endorse or promote products derived  
# from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND  
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS  
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF  
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND  
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,  
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  
# SUCH DAMAGE.

#################################################################
# Path initialization
PATH="/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/bin/cyrus/bin"

#################################################################
# Must run as root
if [ `id -un` != "root" ]; then
  echo "Must run as root"
  exit 1
fi

#################################################################
# Check for source path argument
if [ -z $1 ]; then
  echo ""
  echo "Error: Missing required argument"
  echo ""
  echo "Usage: $0 <source path> <destination path>"
  echo ""
  exit 1
fi

#################################################################
# Set destination directory and make sure it exists

SRC_DIR="$1"
if [ ! -d ${SRC_DIR} ]; then
  echo "Error: Directory ${SRC_DIR} must exist."
  exit 1
fi

#################################################################
# Check for destination path argument
if [ -z $2 ]; then
  echo ""
  echo "Error: Missing required argument"
  echo ""
  echo "Usage: $0 <source path> <destination path>"
  echo ""
  exit 1
fi

#################################################################
# Set destination directory and make sure it exists

DEST_DIR="$2"
if [ ! -w ${DEST_DIR} ]; then
  echo "Error: Directory ${DEST_DIR} must exist and be writable."
  exit 1
fi

#################################################################
# Get database and default data store directories from imapd.conf
#  and verify that they exist
## MED: Need to handle alt data store locations
#
DATABASE_DIR=`/usr/bin/grep "configdirectory" /etc/imapd.conf | sed 's/^.*://' | sed -e 's/^ *//'`

# Verify that the source data directories exist
if [ ! -d ${DATABASE_DIR} ]; then
    echo "Error: Source mail database directory, ${DATABASE_DIR}, does not exist"
    exit 1
fi

if [ ! -d "${SRC_DIR}/user" ]; then
    echo "Error: Source mail database directory, ${SRC_DIR}/user, does not exist"
    exit 1
fi


#################################################################
# Change to spool directory

cd "${SRC_DIR}/user"

#################################################################
# Loop through user accounts and migrate mail

for USER in `ls`
do
  if [ ! -f "${SRC_DIR}/user/${USER}/.migration.lock" ]; then
    ####
    # Crete .lock file
    /usr/bin/touch "${SRC_DIR}/user/${USER}/.migration.lock"

    ####
    # Check for "<user>.seen" file in database directory
    echo "Checking for seen file: ${DATABASE_DIR}/user/${USER:0:1}/${USER}.seen"

    # If seen file exists, we need to convert it from skiplist to flat
    if [ -f ${DATABASE_DIR}/user/${USER:0:1}/${USER}.seen ]; then
      ####
      # Convert from skiplist to flat
      /usr/bin/cyrus/bin/cvt_cyrusdb ${DATABASE_DIR}/user/${USER:0:1}/${USER}.seen skiplist ${DATABASE_DIR}/user/${USER:0:1}/${USER}.seen.flat flat

      ####
      # Migrate mail data
      /usr/bin/cvt_mail_data -m -d ${DATABASE_DIR} -s ${SRC_DIR} -t ${DEST_DIR} -a ${USER}

      ####
      # clean up
      rm ${DATABASE_DIR}/user/${USER:0:1}/${USER}.seen.flat
    else
      ####
      # Do mail migration without setting any seen flags
      cvt_mail_data -m -d ${DATABASE_DIR} -g -s ${SRC_DIR} -t ${DEST_DIR} -a ${USER}
    fi

    ####
    # Set directory ownership
    chown -R _dovecot:mail ${DEST_DIR}/${USER}
  fi
done