Zimbra: backup mailboxes script

Works on ZCS 7. Only active mailboxes from real active user accounts are backed up. Very simple, works great.

Create a cert file so scp can login to your backup server without asking the password. For instructions click here. Run the script as root, if you want to run it as zimbra remove the su - zimbra code including the " that go with that.

#!/bin/bash
#################################
# Zimbra Mailbox Backup         #
# By: Roderick Derks            #
# Date: 01-02-2012              #
#################################

export dir=/tmp/zimbra_mailbox_backup

if [ ! -d $dir ] ; then
   mkdir $dir
   chmod 777 $dir
fi

rm -rf $dir/*

su - zimbra -c "zmaccts | grep "@" | grep -v never|grep -i active | awk '{print $1}' > $dir/user.txt"
user=( `cat $dir/user.txt | awk '{print $1}' ` )
date=`date -I`

for i in "${user[@]}"
do
    echo mailbox currently being exported: $i
    eval "su - zimbra -c \"zmmailbox -z -m $i getRestURL \"//?fmt=tgz\" > $dir/$date.$i.tgz\""
    wait
done

#echo remove empty files
#find $dir/ -type f -size 0 | xargs rm

echo
echo scp files to freenas
scp $dir/* root@freenas:/mnt/DATA/Backup/Zimbra/

echo setting rights
ssh root@freenas 'chmod 777 /mnt/DATA/Backup/Zimbra/*'
ssh root@freenas 'chown root:wheel /mnt/DATA/Backup/Zimbra/*'

echo done