Sistemas‎ > ‎Linux‎ > ‎Scripts Linux‎ > ‎Zimbra‎ > ‎

zimbraBackup.sh

#!/bin/bash
# Desenvolvido por Silvio Garbes em 06/04/2016
# Atualizado em 08/04/2016
# Crontab
# 30 19 * * * sh /empresa/zimbraBackup.sh

# ----------------- #
# Montando particao #
# ----------------- #
mount 192.168.2.25:/backup/zimbra /mnt/backup/

PATH=$PATH:$HOME/bin:/opt/zimbra/bin
export PATH

ZBACKUP=/mnt/backup
limiteProcessosParalelos=5

# ------------------------------------------- #
# Apagando arquivos antigos (mais de 1 dias). #
# ------------------------------------------- #
find $ZBACKUP -mtime +1 -exec rm -rf {} \;

# --------------------- #
# Backup Banco de dados #
# --------------------- #
SQLPASSWD=`zmlocalconfig -s mysql_root_password`
SQLPASSWD=${SQLPASSWD##*=}

# Backup base Zimbra
echo "Fazendo backup banco zimbra"
/opt/zimbra/mysql/bin/mysqldump zimbra -S /opt/zimbra/db/mysql.sock -u root --password="`echo $SQLPASSWD`" | bzip2 > $ZBACKUP/backupBanco_zimbra_`date "+%Y%m%d-%H%M"`.bz2

# Backup base Mailboxs
function fazBackupBanco {
   echo "Fazendo backup banco "$1
   /opt/zimbra/mysql/bin/mysqldump $1 -S /opt/zimbra/db/mysql.sock -u root --password="`echo $SQLPASSWD`" | bzip2 > $ZBACKUP/backupBanco_"$1"_`date "+%Y%m%d-%H%M"`.bz2 &
}

for db in `/opt/zimbra/bin/mysql -NB -e 'show databases' | grep mbox`
do
  while true
  do
    processosParalelos=`ps -ef | grep -c mysqldump`
    if [ "$processosParalelos" -le "$limiteProcessosParalelos" ]
    then
      break
    else
      sleep 10
    fi
  done
  fazBackupBanco $db
  sleep 2
done

# ---------------------- #
# Backup de configuracao #
# ---------------------- #
echo "Fazendo backup configuracoes"
tar cvfz $ZBACKUP/backupConfiguracao_`date "+%Y%m%d-%H%M"`.tar.gz /opt/zimbra/conf

# ---------------------------- #
# Backup lista de distribuicao #
# ---------------------------- #
function fazBackupLista {
   echo "Fazendo backup lista "$1
   /opt/zimbra/bin/zmprov getDistributionListMembership $1 | bzip2 > $ZBACKUP/backupLista_"$1"_`date "+%Y%m%d-%H%M"`.bz2 &
}

for i in `/opt/zimbra/bin/zmprov getAllDistributionLists`
do
  while true
  do
    processosParalelos=`ps -ef | grep -c getDistributionListMembership`
    if [ "$processosParalelos" -le "$limiteProcessosParalelos" ]
    then
      break
    else
      sleep 10
    fi
  done
  fazBackupLista $i
  sleep 2
done

# ----------------- #
# Backup de e-mails #
# ----------------- #
function fazBackupMail {
   echo "Fazendo backup mail "$1
   /opt/zimbra/bin/zmmailbox -z -m $1 getRestURL "//?fmt=tgz" > $ZBACKUP/backupMail_"$1"_`date "+%Y%m%d-%H%M"`.tgz &
}

for mbox in `/opt/zimbra/bin/zmprov -l gaa`
do
  while true
  do
    processosParalelos=`ps -ef | grep -c ZMailboxUtil`
    if [ "$processosParalelos" -le "$limiteProcessosParalelos" ]
    then
      break
    else
      sleep 10
    fi
  done
  fazBackupMail $mbox
  sleep 2
done

# -------------------- #
# Desmontando particao #
# -------------------- #
umount /mnt/backup

Comments