Migração de estrutura

Migrando de um servidor para outro, mesmo se as versões forem diferentes.
Vamos criar o diretório /migration/zimbra para executar as exportações e importações.
O ideal é que seja um USB Drive ou montagem de um servidor com espaço suficiente:
chmod 777 /migration
chmod 777 /migration/zimbra
chown zimbra:zimbra /migration
chown zimbra:zimbra /migration/zimbra
Exportando todos os domínios:
su - zimbra
cd /migration/zimbra
mkdir domains
cd domains
zmprov gad | tee -a domains.txt
Exportando todas as contas:
Administradores:
cd /migration/zimbra
mkdir accounts
cd accounts
zmprov gaaa | tee -a admins.txt
Exportando os usuários:
zmprov -l gaa | tee -a users.txt
Exportando os detalhes das contas:
cd /migration/zimbra
mkdir account_details
cd account_details
for user in `cat ../accounts/users.txt`; do zmprov ga $user  | grep -i Name: | tee -a $user.txt ; done
Exportando todas as senhas:
cd /migration/zimbra
mkdir passwords
cd passwords
for user in `cat ../accounts/users.txt`; do zmprov -l ga $user userPassword | grep userPassword: | awk '{ print $2}' | tee -a $user.shadow; done
Exportando todas as listas de distribuição:
cd /migration/zimbra
mkdir distribution_lists
cd distribution_lists
zmprov gadl | tee -a distribution_lists.txt
for list in `cat distribution_lists.txt`; do zmprov gdlm $list > $list.txt ;echo "$list"; done
Exportando todos os aliases:
cd /migration/zimbra
mkdir aliases
cd aliases
for user in `cat ../accounts/users.txt`; do zmprov ga  $user | grep zimbraMailAlias | awk '{print $2}' | tee -a $user.txt ;echo $i ;done
Para remover as contas que não possuem aliases:
find . -type f -empty | xargs -n1 rm -v
Ao final teremos a seguinte estrutura:

Iniciando o processo de importação:
Importando os domínios:
cd /migration/zimbra/domains
for domain in `cat domains.txt `; do zmprov cd $domain zimbraAuthMech zimbra ;echo $domain ;done
Restaurando as contas e senhas:
cd /migration/zimbra
mkdir scripts
cd scripts
vim restore_accounts.sh
Conteúdo do script:
#!/bin/bash
PASSWDS="../passwords"
ACCOUNT_DETAILS="../account_details"
USERS="../accounts/users.txt"
for i in `cat $USERS`
 do
 givenName=$(grep givenName: $ACCOUNT_DETAILS/$i.txt | cut -d ":" -f2)
 displayName=$(grep displayName: $ACCOUNT_DETAILS/$i.txt | cut -d ":" -f2)
 shadowpass=$(cat $PASSWDS/$i.shadow)
 zmprov ca $i "TeMpPa55^()" cn "$givenName" displayName "$displayName" givenName "$givenName"
 zmprov ma $i userPassword "$shadowpass"
done
Torne o executável e prossiga:
chmod 777 restore_accounts.sh
./restore_accounts.sh
Restaurando as listas de distribuição:
cd /migration/zimbra/
for lists in `cat  distribution_lists/distribution_lists.txt`; do zmprov cdl distribution_lists/$lists ; echo "$lists -- done " ; done
cd /migration/zimbra/distribution_lists
vim restore_dist_lists.sh
Cole o script:
#!/bin/bash
for list in `cat distribution_lists.txt`
do
    for mbmr in `grep -v '#' ./$list.txt | grep '@'`
    do
        zmprov adlm $list $mbmr
        echo " $mbmr has been added to $list"
    done
done
Torne executável e prossiga:
chmod 777 restore_dist_lists.sh
./restore_dist_lists.sh
Restaurando as aliases:
cd /migration/zimbra/aliases
vim restore_aliases.sh
#!/bin/bash
echo "Processing User accounts"
for user in `cat ../accounts/users.txt`
do
    echo $user
    if [ -f "./$user.txt" ]; then
        for alias in `grep '@' ./$user.txt`
        do
            zmprov aaa $user $alias
            echo "$user ALIAS $alias - Restored"
        done
     fi
done

echo "Processing Admin accounts"
for user in `cat ../accounts/admins.txt`
do
    echo $user
    if [ -f "./$user.txt" ]; then
        for alias in `grep '@' ./$user.txt`
        do
            zmprov aaa $user $alias
            echo "$user ALIAS $alias - Restored"
        done
    fi
done
Torne o executável e prossiga:
chmod 777 restore_aliases.sh
./restore_aliases.sh
Importando os dados:
su - zimbra
cd /migration/zimbra 
mkdir mailbox_data
cd mailbox_data
for user in `cat ../accounts/users.txt`; do echo "Exporting mailbox $user" ; zmmailbox -z -m $user getRestURL '/?fmt=tgz' > ./$user.tgz ; done
Migrando Filtros:
mkdir /migration/zimbra/filters
cd /migration/zimbra/filters
vim export_filters.sh
chmod 777 export_filters.sh
#!/bin/bash
mkdir tmp
set -x
clear
for user in `cat ../accounts/users.txt`; 
do
    filter=`zmprov ga $user zimbraMailSieveScript > ./tmp/$user`
    sed -i -e "1d" ./tmp/$user
    sed 's/zimbraMailSieveScript: //g' ./tmp/$user > ./$user;
    rm ./tmp/$user
    echo "Export filter for $user"
done
\rm -rf tmp
Migrando o conteúdo:
cd /migration/zimbra/mailbox_data
for mailbox in `cat ../accounts/users.txt`; do zmmailbox -z -m $mailbox postRestURL "/?fmt=tgz&resolve=skip" ./$mailbox.tgz ; echo "$mailbox - done "; done
Migrando os filtros:
cd /migration/zimbra/filters
vim import_filters.sh
chmod 777 import_filters.sh
#!/bin/bash
for filter in ./*
do
    if [ "$filter" == "./import_filters.sh" ] ; then
        continue;
    fi


    if [ "$filter" == "./export_filters.sh" ] ; then
        continue;
    fi

    if [ "$filter" == "./tmp" ] ; then
        continue;
    fi

    Filter_String=`cat "$filter"`
    Account=$filter
    zmprov ma $(echo $filter | grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b") zimbraMailSieveScript '$Filter_String'
    echo "Process filter $Account"
done
echo "All filter has been import successfully"