I've updated the script and added a check to ensure that the mailbox id is valid.
DISCLAIMERS:
-I give no guarantees that this script will work for you and cannot be held liable for any damages that may occur.
-Use at your own risk!
-If your Zimbra server breaks, you get to keep both pieces.
-This script has NOT been tested in a production environment.
-This script has not had much testing at all.
-I have not yet verified that I can perform a successful restore from the backups that are produced by this script.
-The restore script is left as an exercise to the reader.
Code:
#!/bin/bash
export time=`date +%Y-%m-%d_%H-%M-%S`
########################################
# Modify the following variables to suit your installation
export backup_dir=/media/usbdisk/zimbra/hot_backup/$time
export zimbra_dir=/opt/zimbra
export index_dir=$zimbra_dir/index
export store_dir=$zimbra_dir/store
# Do not modify anything below this point
########################################
# Create the backup directory
mkdir -p $backup_dir
# We need mysqldump. If it doesn't exist,
# copy mysql and modify it to call mysqldump.
if [ ! -e $zimbra_dir/bin/mysqldump ]
then
cp $zimbra_dir/bin/mysql $zimbra_dir/bin/mysqldump
sed -i 's|/bin/mysql |/bin/mysqldump |g' $zimbra_dir/bin/mysqldump
fi
# Get a list of all accounts and execute
# the contents of the for-loop for each account.
for i in `$zimbra_dir/bin/zmprov gaa`
do
# Beginning of for-loop
echo
echo --------------------------------------------------------
echo $i
echo --------------------------------------------------------
# Use email address to get zimbra_id
zimbraId=`$zimbra_dir/bin/zmprov ga $i |grep zimbraId |sed 's|zimbraId: ||g'`
#echo zimbraId is $zimbraId
zimbra_id=`$zimbra_dir/bin/mysql -e "use zimbra; select id from mailbox where account_id = '$zimbraId'"`
#echo zimbra_id is $zimbra_id
zimbra_id=`echo $zimbra_id | sed 's|id ||g'`
#echo zimbra_id is $zimbra_id
# Check to make sure we got a valid zimbraId
if [ "$zimbra_id" = "" ]; then
echo $i has no mailbox.
else
# Set zimbraAccountStatus to "maintenance"
$zimbra_dir/bin/zmprov ma $i zimbraAccountStatus maintenance
# Verify that zimbraAccountStatus got set to "maintenance"
$zimbra_dir/bin/zmprov ga $i |grep zimbraAccountStatus
# Copy all files in $index_dir/0/$zimbra_id
mkdir -p $backup_dir/$i/index/
cp -r $index_dir/0/$zimbra_id/* $backup_dir/$i/index/
du $backup_dir/$i/index/
# Copy all files in $store_dir/0/$zimbra_id
mkdir -p $backup_dir/$i/store/
cp -r $store_dir/0/$zimbra_id/* $backup_dir/$i/store/
du $backup_dir/$i/store/
# Copy the user's MySQL database
$zimbra_dir/bin/mysqldump mailbox$zimbra_id > $backup_dir/$i/mailbox$zimbra_id.sql
ls -al $backup_dir/$i/mailbox$zimbra_id.sql
# Set zimbraAccountStatus to "active"
$zimbra_dir/bin/zmprov ma $i zimbraAccountStatus active
# Verify that zimbraAccountStatus got set to "active"
$zimbra_dir/bin/zmprov ga $i |grep zimbraAccountStatus
# End of if statement
fi
# End of for-loop
done
# All mailboxes have been backed up.
# Backup Zimbra master database and LDAP accounts database.
# First, set all accounts to maintenance.
echo
echo --------------------------------------------------------
echo Setting all accounts to maintenance mode to finish backup.
echo --------------------------------------------------------
for i in `$zimbra_dir/bin/zmprov gaa`
do
$zimbra_dir/bin/zmprov ma $i zimbraAccountStatus maintenance
done
echo
echo --------------------------------------------------------
echo Backing up Zimbra master database
echo --------------------------------------------------------
$zimbra_dir/bin/mysqldump zimbra > $backup_dir/zimbra.sql
ls -al $backup_dir/zimbra.sql
echo
echo --------------------------------------------------------
echo Backing up LDAP accounts database
echo --------------------------------------------------------
su - zimbra -c "$zimbra_dir/bin/zmslapcat /tmp/"
cp /tmp/ldap.bak $backup_dir/
rm /tmp/ldap.bak
ls -al $backup_dir/ldap.bak
echo
echo --------------------------------------------------------
echo Setting all accounts to active mode.
echo --------------------------------------------------------
for i in `$zimbra_dir/bin/zmprov gaa`
do
$zimbra_dir/bin/zmprov ma $i zimbraAccountStatus active
done
echo
echo --------------------------------------------------------
echo Backup is complete!
echo --------------------------------------------------------