Skip to content

Instantly share code, notes, and snippets.

@quetzyg
Created May 10, 2015 10:20
Show Gist options
  • Save quetzyg/7d33a9da24120b0c6a36 to your computer and use it in GitHub Desktop.
Save quetzyg/7d33a9da24120b0c6a36 to your computer and use it in GitHub Desktop.
A shellscript to migrate maildir data (Courier/Dovecot) between different web panels (H-Sphere, ISP Manager and cPanel).
#!/bin/sh
#
# Maildir Migrator v0.5.1
# Last updated: 25/10/2011
#
# Copyright (C) 2011 Quetzy Garcia <quetzyg@altek.org>
#
##################################
# GLOBAL VARIABLES BEGIN #
##################################
SCRIPT_NAME="Maildir Migrator"
SCRIPT_VER="0.5.1"
account=""
mdir_src_dir_chk=0
mdir_src=""
mdir_dst=""
mdir_src_dir=""
mdir_dst_dir=""
domain=""
xdir=""
dry_run=0
# whitespace filler
wsf="{!}"
##################################
# GLOBAL VARIABLES END #
##################################
#
# Just die
#
die()
{
echo "$@"
exit
}
#
# Check for privileges
#
check_root()
{
if [ $(whoami) != "root" ]; then
die "You must be root to run this script!"
fi
}
#
# Script usage
#
usage()
{
echo "Usage: $0 -s <source> [-a <account>] [-d <domain>] [-x <H-Sphere extra domain directory>] [-t]"
echo ""
echo "Options:"
echo "-a set panel account (username)."
echo "-s set the maildir source."
echo "-d set a different domain to migrate to."
echo "-x set the H-Sphere extra domain directory."
echo "-t set test mode (no changes are made)."
die "-h print this help."
}
#
# Configure paths and other stuff
#
configure()
{
if [ -z $mdir_src ]; then
usage
fi
if [ ! -d $mdir_src ]; then
die "[ FAIL ]: '$mdir_src' is not a directory!"
fi
if [ $dry_run -eq 1 ]; then
echo "[ INFO ]: Script running in test mode"
fi
# detect maildir source
for mbox in $(ls $mdir_src); do
if [ -d $mdir_src$mbox/Maildir/cur/ ]; then
echo "[ INFO ]: Maildir source: H-Sphere"
mdir_src_dir="/Maildir/"
mdir_src_dir_chk=1
break
fi
if [ -d $mdir_src$mbox/.maildir/cur/ ]; then
echo "[ INFO ]: Maildir source: ISP Manager"
mdir_src_dir="/.maildir/"
mdir_src_dir_chk=1
break
fi
if [ -d $mdir_src$mbox/cur/ ]; then
echo "[ INFO ]: Maildir source: CPanel"
mdir_src_dir="/"
mdir_src_dir_chk=1
break
fi
done
if [ $mdir_src_dir_chk -eq 0 ]; then
die "[ FAIL ]: Sorry, could not detect the maildir source!"
fi
# if not specified, get domain from path
[ -z $domain ] && domain=$(basename $mdir_src)
# H-Sphere
if [ -z $account ]; then
echo "[ WARN ]: No account has been set, assuming we're on H-Sphere..."
if [ -z $xdir ]; then
mdir_dst="/hsphere/local/var/vpopmail/domains/$domain/"
else
mdir_dst="/hsphere/local/var/vpopmail/domains/$xdir/$domain/"
fi
mdir_dst_dir="/Maildir/"
if [ -d $mdir_dst ]; then
echo "[ INFO ]: Maildir destination: H-Sphere"
else
die "[ FAIL ]: Maildir destination '$mdir_dst' not found! Try the -x flag to set the H-Sphere extra domain directory"
fi
# ISP Manager
elif [ -d "/var/www/$account/data/email/$domain/" ]; then
echo "[ INFO ]: Maildir destination: ISP Manager"
mdir_dst="/var/www/$account/data/email/$domain/"
mdir_dst_dir="/.maildir/"
# CPanel
elif [ -d "/home/$account/mail/$domain/" ]; then
echo "[ INFO ]: Maildir destination: CPanel"
mdir_dst="/home/$account/mail/$domain/"
mdir_dst_dir="/"
else
die "[ FAIL ]: Could not detect panel, invalid account/domain?"
fi
}
#
# Get hidden directories
#
get_hidden_dirs()
{
hd=""
if [ -d $1 ]; then
for dir in $1.*; do
# remove whitespace to prevent usage as a field separator
suffix="$(basename "$dir" | sed 's/ /$wsf/g')"
# get every hidden directory except '.' and '..'
if [ -d "$dir" ] && [ "$suffix" != "." ] && [ "$suffix" != ".." ]; then
hd=$hd" $suffix/"
fi
done
fi
echo "$hd"
}
#
# Migrate mailbox content
#
migrate()
{
for mbox in $(ls $mdir_src); do
if [ -d $mdir_dst$mbox ]; then
usr=$(ls -ld $mdir_dst$mbox | awk -F' ' '{print $3}')
grp=$(ls -ld $mdir_dst$mbox | awk -F' ' '{print $4}')
run_chown=0
for mid in "" $(get_hidden_dirs $mdir_src$mbox$mdir_src_dir); do
for top in "cur/" "new/" "tmp/"; do
# put the whitespace back as it was
mid=$(echo $mid | sed 's/$wsf/ /g')
# make paths
src="$mdir_src$mbox$mdir_src_dir$mid$top"
dst="$mdir_dst$mbox$mdir_dst_dir$mid$top"
if [ -d "$src" ]; then
if [ -d "$dst" ] || [ ! -z "$mid" ]; then
echo "[ INFO ]: Moving: '$src' -> '$dst'"
if [ $dry_run -eq 0 ]; then
mkdir -p "$dst"
mv -f "$src"* "$dst" > /dev/null 2>&1
fi
run_chown=1
else
echo "[ WARN ]: Skipping '$src'..."
fi
fi
done
done
if [ $run_chown -eq 1 ]; then
echo "[ INFO ]: Setting file ownership to '$usr:$grp' on '$mdir_dst$mbox'"
if [ $dry_run -eq 0 ]; then
chown -R $usr:$grp $mdir_dst$mbox
fi
fi
fi
done
die "[ INFO ]: Done!"
}
#
# Parse script parameters
#
while getopts "a:s:d:x:th" flag; do
case $flag in
"a")
account=$OPTARG
;;
"s")
mdir_src=$OPTARG
;;
"d")
domain=$OPTARG
;;
"x")
xdir=$OPTARG
;;
"t")
dry_run=1
;;
"h")
usage
;;
*)
usage
;;
esac
done
#
# Show script info
#
echo "$SCRIPT_NAME - v$SCRIPT_VER"
echo ""
check_root
configure
migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment