Limpeza de mensagens antigas do Zimbra via linha de comandos

De Wiki Hackstore

Remoção de mensagens por tempo

#!/bin/bash

CONTAS="contato@hackstore.com.br suporte@hackstore.com.br"

for CONTAMAIL in ${CONTAS}; do

        # Remove espaços vazios
        crunch_remove() {
                while read FOO ; do
                echo $FOO
                done
                         }
        # Gera lista de mensagens antigas (data em formato US)
        /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} s -l 1000 -t message "in:/Inbox (before:01/20/12)" > /tmp/lista-${CONTAMAIL}.tmp

        # Remove espaços vazios e gera lista de IDs das msgs
        cat /tmp/lista-${CONTAMAIL}.tmp|crunch_remove|cut -d" " -f2|sed s/'1000,'//g|sed s,Type,,g|sed s,'----',,g|sed '/^[\ ]\+\?$/d'|sed s/'\,'//g > /tmp/lista-${CONTAMAIL}.txt

        # Remove mensagens geradas na lista anterior
        while read ID_CLEAN;do
              /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} dm $ID_CLEAN
        done < /tmp/lista-${CONTAMAIL}.txt
done


Limpar a lixeira de todas as contas

#!/bin/bash

# /opt/zimbra/bin/zmprov -l gaa
CONTAS="$(/opt/zimbra/bin/zmprov -l gaa)"

for CONTAMAIL in ${CONTAS}; do

        # Remove espaços vazios
        crunch_remove() {
                while read FOO ; do
                echo $FOO
                done
                         }
        # Gera lista de mensagens antigas (data em formato US)
        /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} s -l 1000 -t message "in:/Trash" > /tmp/lista-${CONTAMAIL}.tmp

        # Remove espaços vazios e gera lista de IDs das msgs
        cat /tmp/lista-${CONTAMAIL}.tmp|crunch_remove|cut -d" " -f2|sed s/'1000,'//g|sed s,Type,,g|sed s,'----',,g|sed '/^[\ ]\+\?$/d'|sed s/'\,'//g > /tmp/lista-${CONTAMAIL}.txt

        # Remove mensagens geradas na lista anterior
        while read ID_CLEAN;do
              /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} dm $ID_CLEAN
        done < /tmp/lista-${CONTAMAIL}.txt
done


Limpar apenas algumas caixas especificas

#!/bin/bash

set -x

ZIMBRA_BIN=/opt/zimbra/bin

THEDATE=$(date -d "-10 day" +"%m/%d/%y")
OneYearAgo=THEDATE
echo "One year ago : $THEDATE"
TODAY=$( date +"%m/%d/%y")

############################
# Remove espaços vazios
crunch_remove() {
        while read FOO ; do
                echo $FOO
        done
}
############################

# gera lista de apenas usuários que comecem com r[numero]*
zmprov  -l gaa|egrep -v "$(zmprov -l gacf |egrep "zimbraAmavisQuarantineAccount|zimbraSpamIsSpamAccount|zimbraSpamIsNotSpamAccount|zimbraVersionCheckNotificationEmail"|awk '{print $2}')"|egrep ^r|egrep '^r1|^r2|^r3|^r4|^r5|^r6|^r7|^r8|^r9|^r0' > /tmp/lista-old-messages.txt

while read CONTAMAIL;do
        # Gera lista de mensagens antigas (data em formato US)
        /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} s -l 1000 -t message "(before:$THEDATE)" > /tmp/lista-${CONTAMAIL}.tmp

        # Remove espaços vazios e gera lista de IDs das msgs
        cat /tmp/lista-${CONTAMAIL}.tmp|crunch_remove|cut -d" " -f2|sed s/'1000,'//g|sed s,Type,,g|sed s,'----',,g|sed '/^[\ ]\+\?$/d'|sed s/'\,'//g > /tmp/lista-${CONTAMAIL}.txt

        # Remove mensagens geradas na lista anterior
        while read ID_CLEAN;do
              /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} dm $ID_CLEAN
        done < /tmp/lista-${CONTAMAIL}.txt

        rm /tmp/lista-${CONTAMAIL}.txt

        ###################################################################                                                                                                                          
                                                                                                                                                                                                     
        # Gera lista de mensagens da lixeira                                                                                                                                                         
        /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} s -l 1000 -t message "in:/Trash" > /tmp/lista-trash-${CONTAMAIL}.tmp                                                                            
                                                                                                                                                                                                     
        # Remove espaços vazios e gera lista de IDs das msgs                                                                                                                                         
        cat /tmp/lista-trash-${CONTAMAIL}.tmp|crunch_remove|cut -d" " -f2|sed s/'1000,'//g|sed s,Type,,g|sed s,'----',,g|sed '/^[\ ]\+\?$/d'|sed s/'\,'//g > /tmp/lista-trash-${CONTAMAIL}.txt       
                                                                                                                                                                                                     
        # Remove mensagens geradas na lista anterior                                                                                                                                                 
        while read ID_CLEAN;do                                                                                                                                                                       
              /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} dm $ID_CLEAN                                                                                                                              
        done < /tmp/lista-trash-${CONTAMAIL}.txt                                                                                                                                                     

        rm /tmp/lista-trash-${CONTAMAIL}.txt

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

        /opt/zimbra/bin/zmmailbox -z -m ${CONTAMAIL} emptyFolder /Junk

done < /tmp/lista-old-messages.txt