Below is my first attempt at a bash script that performs a hot-backup of the Open Source Edition. A few 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.
A few notes about its operation:
-The script backs up each mailbox one at a time. To do this, it sets the account to maintenance mode, copies the files under index/ and store/ and does a mysqldump of the user's MySQL database. When finished, it sets the account back to active mode.
-Once all mailboxes have been backed up, we need to dump the Zimbra MySQL database and LDAP. To ensure consistency, we set all accounts to maintenance mode, perform the two backups, and then set all accounts back to active mode. Setting all accounts to maintenance mode may be overkill, but I'd rather be safe than sorry.
-While a user account is in maintenance mode, the user will not be able to login. If the user was already logged in when their account was put into maintenance mode, then they can continue working in their mailbox, but they cannot receive new email until their account is set back to active mode.
-This script currently assumes that all Zimbra mailboxes are under:
/opt/zimbra/store/0/
and
/opt/zimbra/index/0/
I would assume that once you hit a certain number of mailboxes, then
/opt/zimbra/store/1/
and
/opt/zimbra/index/1/
are created and Zimbra stores new mailboxes under these new directories. Does anybody know what that mystery number of mailboxes is?
Here's the script, which I have creatively named "zimbra_hot_backup.sh":
Code:
#!/bin/bash
export time=`date +%Y-%m-%d_%H-%M-%S`
# Change this to the location where you want to store your backups
export backup_dir=/media/NO_NAME/zimbra/hot_backup/$time
mkdir -p $backup_dir
export zimbra_dir=/opt/zimbra
export index_dir=$zimbra_dir/index
export store_dir=$zimbra_dir/store
# 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 Backing up mailbox $i
echo --------------------------------------------------------
# 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
# Use email address to get zimbra_id
export zimbra_id=`$zimbra_dir/bin/mysql << EOF |grep -v index_volume_id |awk '{print $1}'
connect zimbra
select * from mailbox where comment = '$i';
EOF
`
# For debugging only
#echo $zimbra_id
# 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 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 -------------------------------------------------------- Here's some example output of the script in action:
Code:
--------------------------------------------------------
Backing up mailbox test1@example.com
--------------------------------------------------------
zimbraAccountStatus: maintenance
128 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test1@example.com/index/index/0
160 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test1@example.com/index/index
192 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test1@example.com/index/
64 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test1@example.com/store/msg/0
96 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test1@example.com/store/msg
128 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test1@example.com/store/
-rwxr-xr-x 1 root root 6084 Dec 15 09:44 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test1@example.com/mailbox72.sql
zimbraAccountStatus: active
--------------------------------------------------------
Backing up mailbox test2@example.com
--------------------------------------------------------
zimbraAccountStatus: maintenance
128 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test2@example.com/index/index/0
160 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test2@example.com/index/index
192 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test2@example.com/index/
64 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test2@example.com/store/msg/0
96 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test2@example.com/store/msg
128 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test2@example.com/store/
-rwxr-xr-x 1 root root 6084 Dec 15 09:44 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/test2@example.com/mailbox73.sql
zimbraAccountStatus: active
--------------------------------------------------------
Setting all accounts to maintenance mode to finish backup.
--------------------------------------------------------
--------------------------------------------------------
Backing up Zimbra master database
--------------------------------------------------------
-rwxr-xr-x 1 root root 16213 Dec 15 09:49 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/zimbra.sql
--------------------------------------------------------
Backing up LDAP accounts database
--------------------------------------------------------
-rwxr-xr-x 1 root root 175956 Dec 15 09:49 /media/NO_NAME/zimbra/hot_backup/2005-12-15_09-25-35/ldap.bak
--------------------------------------------------------
Setting all accounts to active mode.
--------------------------------------------------------
--------------------------------------------------------
Backup is complete!
--------------------------------------------------------