All pastes #958242 Raw Edit

Miscellany

public text v1 · immutable
#958242 ·published 2008-03-26 16:25 UTC
rendered paste body
#/bin/bash
## mkperm.sh
##
## Kon
## 3/25/2008
##
## This script is intended to create a backup of Exchange tree when mounted in EXCHANGE_MIRROR.
##
## Configuration:

##
## This should contain the path to MBX folder.
## Make sure that $EXCHANGE_MIRROR is mounted with utf-8 option, or many files may not copy.
DOMAIN=MYDOMAIN
EXCHANGE_MIRROR=/mnt/mailsync/$DOMAIN.COM/MBX

## And the user homes to be copied to
DOMAIN_HOMES=/home/$DOMAIN

## Set Permissions.
MAILDIR_PERMISSIONS=700
MAILFILE_PERMISSIONS=600

######################################################################################################
# Since we're messing with Maildirs, let's stop services
/etc/init.d/courier-imap stop
qmailctl stop

# use mkdomainskel script to make sure we have maildirs and user folders created for existing users.
MAILROOT=/home/$DOMAIN
SYNCROOT=/home/exchange_backup
getent passwd | grep $DOMAIN | sed -e 's/^/su /' -e 's/\([^:]*\).*$/\1/g' -e 'e'

cd $MAILROOT

for i in $( ls )
do
cd $i
chown -R $i:"Domain Users" *
cd ..
done;

cd $SYNCROOT

for i in $( ls )
do
cd $i
chown -R $i:"Domain Users" *
cd ..
done;

for mailuser in `ls $EXCHANGE_MIRROR`					# Do this for alll users in MBX
do
	SOURCE=$EXCHANGE_MIRROR/$mailuser
	MAILUSER=`echo $mailuser | tr '[A-Z]' '[a-z]'` 			# make mailuser lowercase for target
	DEST=$DOMAIN_HOMES/$MAILUSER/Maildir
	cd $SOURCE
	OIFS="$IFS"; IFS="" 						# Some voodoo to deal with insane trailing spaces in folders.
	find . -mindepth 2 -type d | while read subdir			# traverse folders 2 deep (deal with the subfolders first)
	do
		subdir=`echo "$subdir" | sed 's/^\.\///'`		# remove leading ./
		destsub=`echo ".$subdir" | sed 's/Inbox\///;s/\//\./'`	# Since all folders are subfolders of Inbox, remove Inbox
		destsub="$DEST/$destsub"				# and add dots for each subfolder

		mkdir "$destsub/"{new,tmp,cur}				# Create Maildir structure
									# cp cant handle large dirs, so do this:
		for i in "$SOURCE/$subdir/"*; do cp -u "$i" "$destsub/cur/"; done
	done
	mkdir "$DEST/"{cur,tmp,new}

	for i in "$SOURCE/Inbox/"*; do cp -u "$i" "$DEST/cur/"; done

	find . -maxdepth 1 -type d | while read subdir
	do
		subdir=`echo $subdir | sed "s/+/ /;s/.\///"`
		if [ "$subdir" != ".." -a "$subdir" != "." -a "$subdir" != "Inbox" -a "$subdir" != "Contacts" -a "$subdir" != "Journal" ]
		then
			subdir=`echo $subdir | sed 's/\ /\\ /g'`
			mkdir "$DEST/.$subdir"
			mkdir "$DEST/.$subdir"/{cur,tmp,new}
			for i in "$SOURCE/$subdir/"*; do  cp -u "$i" "$DEST/.$subdir/cur/"; done
		fi
	done
	IFS="$OIFS"
	chown -R -L $MAILUSER:domain\ users $DEST
	find . -type d -exec chmod $MAILDIR_PERMISSIONS  {} \;
	find . -type f -exec chmod $MAILFILE_PERMISSIONS {} \;
done

# Restart services
/etc/init.d/courier-imap restart
qmailctl restart