The following script will export all accounts to an import script.
It may be useful for quickly re-create all accounts on another server.
#!/bin/bash
# this script should be run as root
# target for ZCS 7, single server deployment
# last update 2011-12-07
# customize these to your needs
# work folder must be writable by zimbra
work_folder=/tmp
account_dump=${work_folder}/account.dump
exclude_accounts="admin wiki galsync system-ham system-spam system-virus-quarantine"
# get cos_id by looking at the web UI
cos_id="e00428a1-0c00-11d9-836a-000d93afea2a"
zimbra_ldap_password=`/opt/zimbra/bin/zmlocalconfig -s | grep -e zimbra_ldap_password | cut -d '=' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
# ip of the zimbra server, must not be 127.0.0.1
hostip=192.168.0.62
import_script=${work_folder}/accounts-import.sh
# reset files:
echo '' > ${import_script}
# get all account to $accounts
accounts=`su - zimbra -c 'zmprov -l gaa'`;
# loop for each account
for account in ${accounts}; do
account_uid=`echo ${account} | cut -d '@' -f1`
echo ${exclude_accounts} | grep -q -e ${account_uid}
if [ $? -eq 0 ] ; then
echo "Skip ${account}"
continue
fi
echo "Processing ${account} ..."
su - zimbra -c "zmprov -l ga ${account} uid givenName sn cn displayName userPassword mail > ${account_dump}"
# get all basic attributes
uid=`grep -e uid ${account_dump} | cut -d ':' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
givenName=`grep -e givenName ${account_dump} | cut -d ':' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
sn=`grep -e sn ${account_dump} | cut -d ':' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
cn=`grep -e cn ${account_dump} | cut -d ':' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
displayName=`grep -e displayName ${account_dump} | cut -d ':' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
#userPassword=`grep -e userPassword ${account_dump} | cut -d ':' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
userPassword_base64=`/opt/zimbra/bin/ldapsearch -H ldap://${hostip}:389 -x -w ${zimbra_ldap_password} -D "uid=zimbra,cn=admins,cn=zimbra" uid=${uid} userPassword | grep userPassword | cut -d ' ' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`
userPassword=`echo ${userPassword_base64} | base64 -d`
# write csv file: echo ${uid} $givenName $sn $cn $displayName $userPassword
# generate import script
echo "# ${uid}" >> ${import_script}
echo "zmprov ca ${account} ${userPassword} givenName '${givenName}' sn '${sn}' cn '${cn}' displayName '${displayName}' zimbraCOSid $cos_id" >> ${import_script}
# get all aliases
for newalias in `grep -e ^mail: ${account_dump} | cut -d ':' -f2 | sed 's/^ *//g' | sed 's/ *$//g'`; do
if [ "x${newalias}" != "x${account}" ]; then
echo "Extracting alias ${newalias} for ${account} ..."
echo "zmprov aaa ${account} ${newalias}" >> ${import_script}
fi
done
# add blank line separator
echo '' >> ${import_script}
done