Archives par mot-clé : Zimbra

Script Backup Zimbra

#!/bin/bash

## Autor : infragate
## version 2.0
## tested with zimbra version : 8.7.11.GA.1854.UBUNTU16.64_amd64.deb
## RESTORE mbox
## $ZMBOX -z -m $mbox postRestURL "//?fmt=zip&resolve=reset" $mbox.zip
## mailutils package

## colors

red='\033[0;31m'
RED='\033[1;31m'
green='\033[0;32m'
GREEN='\033[1;32m'
blue='\033[0;34m'
BLUE='\033[1;34m'
purple='\033[0;35m'
PURPLE='\033[1;35m'
cyan='\033[1;36m'
def='\033[0m'

#############
# VARIABLES #
#############
DATE=`date +%Y%m%d`
FQDN=`hostname -f`
ZHOME=/opt/zimbra

ZBACKUP=/backup/$FQDN/$DATE
ZMBOXBACKUP=$ZBACKUP/mailbox

ZCONFD=$ZHOME/conf
ZDUMPDIR=/opt/zimbra/backup
ZMBOX=/opt/zimbra/bin/zmmailbox

### Log & Mail
ZLOG=~/log_backup.txt
TEMP=~/temp.txt
RECEP=yourmail@domain.fr
ATT=~/mail.txt

### OPTS
MAIL_ALERT=1
CIFS_BACKUP=1

#### Exec
SLAPCAT=/opt/zimbra/common/sbin/slapcat
MYSQLDUMP=/opt/zimbra/common/bin/mysqldump

### LDAP
LDAPCONFDIR="/opt/zimbra/data/ldap/config"
LDAPDUMPNAME="zimbra_ldap"

### Mysql
MYSQL_USER="root"
MYSQL_PASSWORD="mJClQWgfgfgfNTAg1YQo3a"
MYSQL_SOCKET="/opt/zimbra/data/tmp/mysql/mysql.sock"
MYSQLDUMPNAME="zimbra_mysql"

### CIFS Backup
USER_SHARE=username
PASS_SHARE="E9????????2"
SHARE_SERVER=0.0.0.0
SHARE_FOLDER=folder_name
MOUNT_POINT=/backup

##############
#SCRIPT USAGE#
##############

ARGS=$(getopt -o a:h -l "action:help" -n "$(basename $0)" -- "$@")

function usage()
{
        echo -e "${GREEN}Usage:\t$(basename $0) [-a mysql | ldap | mbox | all]${def}" >&2
}

#Bad arguments
if [[ $? -ne 0 ]];
then
        echo -e "${RED}Error: unparsable arguments${def}" >&2
        exit 255
fi

eval set -- "${ARGS}"

while true
 do
        case "$1" in
                -a|--action)
                        shift
                        if [ -z "${action}" ]
                        then
                                action="$1"
                        else
                                echo -e "${RED}Error: -a option cannot be used several times.${def}" >&2
                                exit 255
                        fi
                        shift
                        ;;
                -h|--help)
                        shift
                        usage
                        exit
                        ;;
                --)
                        shift
                        break
                        ;;
        esac
done

#####################
##### FUNCTIONS     #
#####################

function log_date()
{
 echo $(date +%d/%m/%Y-%H:%M:%S)
}

####################

function test_dir()
{
 if [ ! -d $1 ]; then mkdir -p $1; fi
}

###################

function lblank()
{
 echo ""  >> $TEMP ;
}

####################

function send_mail()
{
 if [ $MAIL_ALERT = 1 ]
  then
    sed -r "s/\x1B\[([0-9];)?([0-9]{1,2}(;[0-9]{1,2})?)?[m]//g" $TEMP > $ATT ;
    mail -s "Zimbra Backup Report for `hostname` `date +%d/%m/%Y-%H:%M:%S`" $RECEP < $ATT ;
    [ "$?" != 0 ] && exit ;
    rm -rf $ATT ;
 fi
}

####################

function time_computing()
{
 elapsed="$(expr $time_end - $time_start)"
 hours=$(($elapsed / 3600))
 elapsed=$(($elapsed - $hours * 3600))
 minutes=$(($elapsed / 60))
 seconds=$(($elapsed - $minutes * 60))

 echo -e "${cyan}The $1 backup lasted : $hours hours $minutes minutes $seconds seconds ${def}" >> $TEMP ;
 lblank
}
##########################

function mount_cifs()
{
        if [ $CIFS_BACKUP = 1 ]
                then
         echo -e "${BLUE}$(log_date) CIFS BACKUP Method Selected${def}" >> $TEMP ;
         test_dir $MOUNT_POINT
         mount -t cifs \\\\$SHARE_SERVER\\$SHARE_FOLDER $MOUNT_POINT  -o username=$USER_SHARE,password=$PASS_SHARE
         [ "$?" != 0 ] &&  echo -e "${red}CIFS MOUNT : FAILED : BACKUP ABORTED${def}" >> $TEMP && exit ;
    fi
}
#########################

function umount_cifs()
{
        if [ $CIFS_BACKUP = 1 ]
                then
                 cd
                 umount $MOUNT_POINT
                 [ "$?" = 0 ] &&  echo -e "$(log_date) ${green}CIFS Share sucessfully unmounted${def}" >> $TEMP || echo -e "$(log_date) ${red}CIFS Share unmounting : FAILED${def}" >> $TEMP ;
    fi
}
############################

function mailbox_1by1()
{
 lblank
 echo -e "$(log_date)${BLUE} Mbox backup activated${def}" >> $TEMP;

 test_dir $ZBACKUP
 test_dir $ZMBOXBACKUP
 test_dir $ZDUMPDIR

 for mbox in `su - zimbra -c "zmprov -l gaa"`
  do
           echo -ne "$(log_date)${blue} Generating files from backup $mbox ... ${def}" >> $TEMP ;
           su - zimbra -c "$ZMBOX -z -m $mbox getRestURL "//?fmt=zip" > $ZDUMPDIR/$mbox.zip" ;
           ([ "$?" = 0 ] &&  echo -e "${green}DONE${def}" || echo -e "${red}FAILED${def}") >> $TEMP ;

 done
 mv $ZDUMPDIR/*.zip $ZMBOXBACKUP
}
#########################

function backup_mysql()
{
 lblank
 test_dir $ZBACKUP
        echo -ne "$(log_date)${BLUE} Mysql Backup activated ... ${def}" >> $TEMP ;

        if [[ -z "${MYSQL_USER}" || -z "${MYSQL_PASSWORD}" ]]
        then
                echo -e "${red}Error: mysql credentials have to be specified${def}" >> $TEMP ;
                exit 255
        fi

        dump_options="--user=${MYSQL_USER} --password=${MYSQL_PASSWORD} --socket="${MYSQL_SOCKET}" --all-databases \
                                  --single-transaction --flush-logs --events --ignore-table=mysql.event"

        $MYSQLDUMP ${dump_options} | gzip > "${ZBACKUP}"/"${MYSQLDUMPNAME}_${FQDN}.sql.gz"
        ([ "$?" = 0 ] &&  echo -e "${green}DONE${def}" || echo -e "${red}FAILED${def}") >> $TEMP ;
}

############################

function backup_ldap()
{
lblank
test_dir $ZBACKUP
        echo -ne "$(log_date)${BLUE} LDAP Backup activated ... ${def}" >> $TEMP ;

        $SLAPCAT -F "${LDAPCONFDIR}" -b "" | gzip > "${ZBACKUP}"/"${LDAPDUMPNAME}_${FQDN}.ldif.gz"
        ([ "$?" = 0 ] &&  echo -e "${green}DONE${def}" || echo -e "${red}FAILED${def}") >> $TEMP ;
}

################
### MAIN  ######
################


case ${action} in
        mbox)
                time_start=`date +%s`
                mount_cifs
                mailbox_1by1
                umount_cifs
                time_end=`date +%s`
                time_computing mbox
                cat $TEMP >> $ZLOG
                send_mail
                rm -rf $TEMP
                ;;

        mysql)
                time_start=`date +%s`
                mount_cifs
                backup_mysql
                umount_cifs
                time_end=`date +%s`
                time_computing mysql
                cat $TEMP >> $ZLOG
                send_mail
                rm -rf $TEMP
                ;;

        ldap)
                time_start=`date +%s`
                mount_cifs
                backup_ldap
                umount_cifs
                time_end=`date +%s`
                time_computing ldap
                cat $TEMP >> $ZLOG
                send_mail
                rm -rf $TEMP
                ;;

        all)
                time_start=`date +%s`
                mount_cifs
                backup_mysql
                ret_mysql=$?

                backup_ldap
                ret_ldap=$?

                mailbox_1by1
                ret_mbox=$?
                umount_cifs
                time_end=`date +%s`
                time_computing full
                cat $TEMP >> $ZLOG
                send_mail
                rm -rf $TEMP
                ;;

        "")
                usage
                ;;

        *)
                echo -e "${red} Error: unknown ${action} action. ${def}" >&2
                exit 255
                ;;
esac