Backup/Restore Scalix mailboxes

Jul 10th, 2008 | By Roma | Category: Scalix

This script backups all Scalix mailboxes at once, and keeps the last three backups in a single compressed file. It’s inspired by an script found on the Scalix Wiki.

Modifications from the original script include:

  • compressing the output files
  • keeping last three backups
#! /bin/bash
 
# Edit the following two values if necessary
MBOXDIR="/home/myunixuser/mboxBkp"
SXDIR="/opt/scalix/bin"
# No more editing after this line
 
echo "Mailbox Backup Starting [`date`]"
if [ -d $MBOXDIR ]; then
     echo "Found Backup Directory ${MBOXDIR}. Using It."
else
    echo "Creating Backup Directory ${MBOXDIR} to backup mailboxes."
    mkdir -p ${MBOXDIR}
fi
 
if [ -d $MBOXDIR/d2 ]; then
     echo "Deleting previous backup nr2"
     rm -f  ${MBOXDIR}/d2/*.tar.gz
else
    echo "Creating d2 Directory"
    mkdir -p ${MBOXDIR}/d2
fi
 
if [ -d $MBOXDIR/d1 ]; then
     echo "Keeping previous backup nr1"
     mv  ${MBOXDIR}/d1/*.tar.gz ${MBOXDIR}/d2/.
else
    echo "Creating d1 Directory"
    mkdir -p ${MBOXDIR}/d1
fi
 
echo "Keeping previous backup nr0"
mv  ${MBOXDIR}/*.tar.gz ${MBOXDIR}/d1/.
 
for i in $(${SXDIR}/omshowu -m all | cut -d "/" -f 1 | sed -e 's: $::g' -e 's/ /\//g'); do
    user=`printf "$i" | sed -e 's:/: :g'`
#
#  $user = Username With Space
#  $i = Username With \ Before Space
#  ex:  $user="Firstname Lastname"
#  ex:  $i="Firstname\ Lastname"
#
    echo
    echo +++++++++++++++++++++++++++++++++++++++++++++
    echo ${user}
    if [ -a $MBOXDIR/$i.mbox ]; then
        echo "Existing backup found.  Deleting before creating new backup."
        rm -f ${MBOXDIR}/${user}.mbox
    fi
    echo "Backing up user ${user}"
    nice -n 19 ${SXDIR}/sxmboxexp -u "${user}" -a ${MBOXDIR}/"${user}".mbox --listlevel folder -F
    echo +++++++++++++++++++++++++++++++++++++++++++++
done
 
echo
echo "Finished backing up user data."
echo
echo
echo "Backing up public folders..."
if [ -a $MBOXDIR/public_folders.mbox ]; then
    echo "Existing backup found.  Deleting before creating new backup."
    rm -f ${MBOXDIR}/public_folders.mbox
fi
nice -n 19 ${SXDIR}/sxmboxexp -p -a ${MBOXDIR}/public_folders.mbox --listlevel folder -F
echo "Done with mailbox backup! [`date`]"
echo
 
echo "Compressing mbox files."
tar zcvf  ${MBOXDIR}/mboxBkp.tar.gz ${MBOXDIR}/*.mbox
chown myunixuser:myunixuser ${MBOXDIR}/mboxBkp.tar.gz
rm -f  ${MBOXDIR}/*.mbox
echo

And this is the crontab entry for running the script every day at 4 AM.

/home/myunixuser/bin/mailboxBackup > /home/myunixuser/log/mailbox_backup.log 2 >& 1

To restore a mailbox, simply untar the backup file, create a new Scalix user an import the backup using

sxmboximp -u "New_User" -a "/home/myunixuser/mboxBkp/Old_User.mbox" -s --listlevel folder
Tags: , , ,

4 comments
Leave a comment »

  1. Thanks, this works great, just tweaking it to use z7ip so we can have password protection.

    This is a much better solution then what I had seen digging around the scalix forums, many of them suggested taking the email server offline then just copying the /opt/ dir which was really silly imho.

  2. Howard,

    What you’ve seen on the forums is for backing up the whole Scalix installation, while this is only for user’s mailboxes/calendar/etc…

    I think it’s still a good idea to schedule regular backups of your entire file system (or the Scalix installation as you’ve seen in the forums) which will certainly come in handy in case of a disk crash.

    On the other hand, mailbox backups are a lot easier to handle when restoring a couple of emails deleted by mistake a week ago…

    Roma.

  3. True enough Roma, I plan to instead do a base install of scalix and then back the vmware image and use that for any recovery.

    I just think mailboxes are crazy important and configuration settings are also important but they are hopefully not changing nearly as often.

    Thanks again!

  4. I couldn’t agree more with you: This is exactly how I do it most of the time!

Leave Comment