#!/bin/bash
# Created by Leon Trappett Sept. 2010 -
ltrappett@mtnmedical.com
#Make a backup before running.
#
#USE AT YOUR OWN RISK!
#
# This script was designed to move the contents of one folder to another folder without using the Web GUI and retaining recursion of the folder structures. This script is very helpful when
# an IMAP account is mounted at a particular mount point to move the contents of it to a folder on the actual Zimbra account.
# The below variables need to be modified for source and destination folder names
sourceFolder="/Groupwise"
destinationFolder="/FromGroupwise"
# The below variables need to be modified for the locations of where the temporary files are stored
# Verify the correct permissions are assigned below to these folders so the zimbra account can write to them
emailTemp="/tmp/zimbra-groupwise/emails"
scriptsTemp="/tmp/zimbra-groupwise/scripts"
who=`whoami`
if [ "$who" != "zimbra" ]
then
echo
echo "Please su to the zimbra user before running this script"
echo
exit
fi
if [ -z "$1" ]; then
echo
echo -e "\nUsage: name-of-script emailaddress"
echo
exit
fi
echo -e "\nUsage: Migrate Emails from a Source Folder to a Destination Folder"
# Gathers a list of email accounts in the mtnmedical.com domain
#zmprov gqu zimbra.mtnmedical.com | grep "@mtnmedical.com" | cut -f 1 -d' '> /tmp/zimbra-groupwise/emails/listaccounts
echo $1 > $emailTemp/listaccounts
# Read the email addresses one line at a time into the following loop
while read emailaddress
do
echo "Currently working on: $emailaddress"
echo "-----------------------------------"
echo -e "\n"
echo "Below is a list of all folders in the users email account"
zmmailbox -z -m $emailaddress gaf
echo -e "\n"
echo "The following folders have Messages that need to be Transfered"
echo -e "\n"
zmmailbox -z -m $emailaddress gaf | grep " $sourceFolder" | awk '{print $4 " " $5 " " $6 " " $7 " " $8 " " $9 " " $10 " " $11 " " $12 " " $13 " " $14 " " $15}' | grep -v "^0 " | awk '{print $2 " " $3 " " $4 " " $5 " " $6 " " $7 " " $8 " " $9 " " $10 " " $11 " " $12 " " $13}'
echo -e "\n"
zmmailbox -z -m $emailaddress gaf | grep " $sourceFolder" | awk '{print $4 " " $5 " " $6 " " $7 " " $8 " " $9 " " $10 " " $11 " " $12 " " $13 " " $14 " " $15}' | grep -v "^0 " | awk '{print $2 " " $3 " " $4 " " $5 " " $6 " " $7 " " $8 " " $9 " " $10 " " $11 " " $12 " " $13}' > $emailTemp/$emailaddress.folders
cat $emailTemp/$emailaddress.folders | sed -e "s@$sourceFolder@$destinationFolder@" > $emailTemp/$emailaddress.newfolders
zmmailbox -z -m $emailaddress cf $destinationFolder &> /dev/null
echo "Creating Folders to Transfer Emails Into"
rm -f $emailTemp/$emailaddress.error
while read foldername
do
errorCreateFolder=`zmmailbox -z -m $emailaddress cf "$foldername"`
if [ "$errorCreateFolder" -ge 0 ]
then
echo -e "\n"
else
echo $errorCreateFolder
echo "If the error is stating unknown folder create the folder by executing the following command with the name of the folder placed in the script."
echo "zmmailbox -z -m $emailaddress cf unknownfolder"
echo "After the folder has been created execute the script again."
exit
fi
echo "Created: $foldername"
done < "$emailTemp/$emailaddress.newfolders"
# Create the script to Move the messages from Source Folder to Destination Folder
rm -f $emailTemp/$emailaddress.zmmailbox.raw &> /dev/null
while read originalfoldername
do
echo -e "\n"
echo "Getting the messages from: $originalfoldername"
echo "The following messages are being added to a script to be moved:"
echo "---------------------------------------------------------------"
zmmailbox -z -m $emailaddress search -t message -l 999999 "(in:\"$originalfoldername\")"
for messageid in $(zmmailbox -z -m $emailaddress search -t message -l 999999 "(in:\"$originalfoldername\")"|awk {'print $2'}|grep [0-9]|grep -v , )
do
statusMessage="echo 'The next message is being moved.'"
echo $statusMessage >> $emailTemp/$emailaddress.zmmailbox.raw
echo 'zmmailbox -z -m ' $emailaddress 'mm ' $messageid ' "'$originalfoldername'"' >> $emailTemp/$emailaddress.zmmailbox.raw
done
done < "$emailTemp/$emailaddress.folders"
cat $emailTemp/$emailaddress.zmmailbox.raw | sed -e "s@$sourceFolder@$destinationFolder@" > $emailTemp/$emailaddress.zmmailbox
mv $emailTemp/$emailaddress.zmmailbox $scriptsTemp/$emailaddress-zmmailbox.script
chmod 700 $scriptsTemp/*
echo "The following script was created for you to run to move the folders for the following email address:"
echo "Script Name: " $scriptsTemp/$emailaddress-zmmailbox.script
echo "Email Address of User: " $emailaddress
done < "$emailTemp/listaccounts"