Script de backup Zimbra
Autor: Marcio José Atanásio | Publicado: 2012-10-10T14:00:00-03:00

As seguintes ações são executadas:
- Backup local ou remoto por compartilhamento cifs (Windows);
- Backup de aliases;
- Backup de listas;
- Backup dos emails, contatos e agenda no formato tgz;
- Backup de todos os domínios ou alguns;
- Geração de arquivo de log. 

Script: 

#!/bin/bash# Marcio Jose Atanasio # marcio.jose@atanasio.com.br# Compuserv Informatica # Versao 20121008# LOCALBCKDIR="/opt/backup"LOGFILE="/var/log/zmbackup.log"# User "all" to backup all domains accounts or domains that want backup with space between.DOMAINS="all"# REMOTEMNTREMOTE="//192.168.1.10/backup"USER="cobaia"PASSWD="cobaia"REMOTE_BACKUP=0 # 0 - Backup will be saved in BCKDIR | 1 or others values - Backup will be saved in MNTLOCAL mounted in BCKDIR.# ENVIROMENTDT=`date +%Y"-"%m"-"%d`function put_logger {&nbsp;&nbsp;&nbsp; echo `date +%Y"-"%m"-"%d" - "%H":"%M`" - $1" &gt;&gt; $LOGFILE}function backup_dir {&nbsp;&nbsp;&nbsp; if [ "$1" = "mount" ]; then&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$REMOTE_BACKUP" = "1" ]; then&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /bin/mount $MNTREMOTE $BCKDIR -o user=$USER,password=$PASSWD&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$?" = "0" ]; then&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "Montagem de $MNTREMOTE realizada com sucesso"&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "Montagem de $MNTREMOTE falhou. O backup não será efetuado."&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ -d $BCKDIR/$DT ]; then&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rm -rf $BCKDIR/$DT&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /bin/mkdir $BCKDIR/$DT &gt; /dev/null&nbsp; 2&gt;&amp;1&nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; if [ "$1" = "umount" ]; then&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$REMOTE_BACKUP" = "1" ]; then&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /bin/umount $BCKDIR&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "Desmontagem de $MNTREMOTE realizada com sucesso"&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; fi}&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; function get_domains {&nbsp;&nbsp;&nbsp; if [ "$DOMAINS" = "all" ]; then&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DOMAINS=`/opt/zimbra/bin/zmprov gad | sort`&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; echo $DOMAINS | sed s/" "/"\n"/g &gt;&gt; $BCKDIR/$DT/domains.txt&nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; put_logger "Lista dos domínios $DOMAINS salva."}&nbsp;&nbsp;&nbsp; function get_lists {&nbsp;&nbsp;&nbsp; for DOM in `echo $DOMAINS`; do&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for LST in `/opt/zimbra/bin/zmprov gadl $DOM` ; do &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
echo "Lista "$LST &gt;&gt; $BCKDIR/$DT/$DOM.lists.txt&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /opt/zimbra/bin/zmprov gdl $LST | grep zimbraMailForwardingAddress | cut -d" " -f2 &gt;&gt; $BCKDIR/$DT/$DOM.lists.txt&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
echo "" &gt;&gt; $BCKDIR/$DT/$DOM.lists.txt&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; done&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "Salvo lista de distribuição de $DOM."&nbsp;&nbsp;&nbsp; done}function get_aliases {&nbsp;&nbsp;&nbsp; for DOM in `echo $DOMAINS`; do&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for ACC in `/opt/zimbra/bin/zmprov -l gaa $DOM | sort`; do&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ALIASES=`/opt/zimbra/bin/zmprov ga $ACC | grep zimbraMailAlias | cut -d: -f2`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ ! -z "$ALIASES" ]; then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; echo $ACC" - &gt;"$ALIASES &gt;&gt; $BCKDIR/$DT/aliases.txt&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; done&nbsp;&nbsp;&nbsp; done}function backup_accounts_aliases { &nbsp;&nbsp;&nbsp; for DOM in `echo $DOMAINS`; do&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "$DOM iniciando backup das contas e aliases."&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for ACC in `/opt/zimbra/bin/zmprov -l gaa $DOM | sort`; do&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /opt/zimbra/bin/zmmailbox -z -m $ACC getRestURL '//?fmt=tgz' &gt; $BCKDIR/$DT/$ACC.tgz&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "$ACC foi efetuado backup."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ALIASES=`/opt/zimbra/bin/zmprov ga $ACC | grep zimbraMailAlias | cut -d: -f2`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ ! -z "$ALIASES" ]; then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; echo $ACC" - &gt;"$ALIASES&nbsp; &gt;&gt; $BCKDIR/$DT/$ACC.alias.txt&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "$ACC tem aliases salvos."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; done&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put_logger "$DOM finalizado backup das contas e aliases."&nbsp;&nbsp;&nbsp; done}# STARTput_logger "Início do backup"put_logger "Efetuar montagem"backup_dir mountput_logger "Verificar domínios para efetuar backup"get_domainsput_logger "Backup listas de distribuição dos domínios"get_listsput_logger "Backup das contas e aliases"backup_accounts_aliasesput_logger "Efetuar desmontagem"backup_dir umountput_logger "Fim do backup"