HowTos/BackupScript Mbox Style

From Scalix Wiki
Revision as of 20:41, 5 June 2007 by Kilrathi (Talk | contribs)

Jump to: navigation, search

MBOX Style Backup Script for Scalix v11

This is a shell script I use to backup all my users to a single flat file (mbox style). I then backup the directory to an offisite location for disaster recovery purposes.

This script backs up all user mailboxes into a single flat file (mbox style) in the directory specified (MBOXDIR). I backup those .mbox files to an offsite archive for disaster recovery purposes. Every time you run this script it will look in the directory specified for an existing .mbox file. If it finds one it will delete it first, then create a new .mbox file from the Scalix Server. This was written for Scalix Server v11. I do know know how well it will work with earlier versions.

#! /bin/bash
echo "[`date`]"
echo "Mailbox Backup Starting."
MBOXDIR="/backup/mailboxes"
if [ -d $MBOXDIR ]; then
    echo "Found Backup Directory. Using It."
else
    echo "Creating Backup Directory ${MBOXDIR} to backup mailboxes."
    mkdir ${MBOXDIR}
fi
for i in $(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 ${user}
    if [ -a $MBOXDIR/$i.mbox ]; then
        echo "Existing backup found.  Removing before creating new backup."
        rm ${MBOXDIR}/${user}.mbox
    fi
    echo "Backing up user [${user}]"
    sxmboxexp -u "${user}" -a ${MBOXDIR}/"${user}".mbox --listlevel folder -F
done
echo "Mailbox Backup Complete."
echo "[`date`]"

Cron Job

I run this script every week on my mail server. I decided to run it every saturday at 1am since that is when the least amount of users are logged into my system. I also decided to send the output to a new log file each week. I named my backup script mailbox_backup.sh Here is what my cron job looks like.

0 1 * * 6 /backup/scripts/mailbox_backup.sh > /var/log/mailbox_backup[`date '+%m.%d.%Y'`].log

Now I can look in /var/log at the backup log for each week and verify that each individual users mailbox was backed up completely. Each log file will also have a date stamp in the filename for quick reference.