常用 script

最後更新: 2019-09-18

目錄

  • Remove a user 's  mail from queue
  • Cleanup old mail (over 90 days)
  • telnet smtp test script
  • mailreport
  • Cleanup dummy mail data
  • Check in mailbox
  • Check User Quota on DB
  • pop3 uidl 保留失敗時

 


Remove a user 's  mail from queue

 

remove_mail_in_queue.sh

#!/bin/bash

_USER="USER@YouWantDelete"

for i in `postqueue -p | grep $_USER | awk {'print $1'} | grep -v 'MAILER-DAEMON'`
do
        # remove "*"
        mymail=`echo $i | tr -d '*' `
        if [[ "$mymail" != ""  ]]
        then
                postsuper -d $mymail
        fi
done

 


Cleanup old mail (over 90 days)

#!/bin/bash
# V0.6
# clean FOLDER on $1
# build a list.sh to run

echo "# clear 90 day before file"
MYDIR=`pwd`

function cleardir {
        FOLDER="$1"

        if [ -d "$FOLDER" ]; then
                echo "#######################################"
                echo "# pwd: $MYDIR"
                echo "# FOLDER: $FOLDER"

                if [ -e "${FOLDER}/cur" ]; then
                        bakdir="${MYDIR}/backup/${FOLDER}.cur.old"
                        src="${FOLDER}/cur"
                else
                        bakdir="${MYDIR}/backup/${FOLDER}.old"
                        src="${FOLDER}"
                fi

                echo "# src: $src"
                echo "# bakdir: $bakdir"
                echo "mkdir -p \"$bakdir\""
                find "$src" -mtime +90 -type f -exec echo 'mv -b ' \''{}'\' \'"$bakdir"\' \;
        fi
}

# folder list
ls -1 -A > myfilelist.txt

while read line
do
        if [ "$line" != "courierimaphieracl" ] && [ "$line" != "courierimapkeywords" ]; then
                echo "$line"
                cleardir "$line" >> list.sh
        fi
done < "myfilelist.txt"

rm -f myfilelist.txt

chmod 700 list.sh

echo "Done"

 


telnet smtp test script

 

#!/bin/sh

_SERVER="xxx.xxx.xxx.xxx"

( echo "HELO testing.com"
sleep 2
echo "MAIL FROM:<tim@MyReturnDomain>"
sleep 2
echo "RCPT TO:<tester@TestDomain>"
sleep 2
echo DATA
sleep 2
echo "Subject:Test-Mail!"
sleep 2
echo "Test-Mail!"
sleep 2
echo .
sleep 2
echo QUIT
) | telnet  $_SERVER 25

P.S.

telnet 後沒有待對方 reply 說 helo 及每句只等 2 秒好易出事, 所以這段 script 唔多 work ...

 


mailreport.sh

 

cron jobs

30 23 * * *     /root/script/mailreport.sh &> /dev/null

官網 package

pflogsumm-1.1.3.tar.gz

mailreport.sh

#!/bin/bash

pflogsumm="/usr/sbin/pflogsumm.pl"
mail="/usr/bin/mail"

maillog="/var/log/mail.log"
report="/root/mail_report.txt"

recipient="x@y"
content="/root/script/mail_report_content.txt"

echo "gen report"
$pflogsumm -d today -u 10 -h 10 \
           --no_bounce_detail --no_deferral_detail \
           --no_reject_detail --no_smtpd_warnings \
           $maillog > $report 2>/dev/null

echo "send report"
echo "" | mail -s"Daily e-mail report" -a"$report" -q "$content" -r "$recipient" $recipient

 


Cleanup orphan mail data

 

cleanup_orphan_mail_data.sh

#!/bin/bash
# filename: cleanup_orphan_mail_data.sh

###################################### Setting
_DOMAIN="datahunter.org"
_SQLPW="???????????"
_MailData="/var/vmail/vmail"


###################################### Code
_MailBoxPath=$_MailData/$_DOMAIN
_USERLIST=/tmp/userlist.$$.txt
_MailBoxList=/tmp/mailbox.$$.txt

# cleanup screen
clear

if [ ! -d $_MailBoxPath ];then
        echo "no mail data"
        exit 1
fi

echo "working on: $_MailBoxPath"

# create folder for backup
mkdir $_MailBoxPath/_backup 2> /dev/null

if [ ! -d $_MailBoxPath/_backup ];then
        echo "no backup folder"
        exit 1
fi

# fetch working user
mysql -uvmail vmail -p$_SQLPW \
-e "SELECT SUBSTRING_INDEX(username, \"@\", 1) FROM mailbox WHERE domain=\"$_DOMAIN\"" > $_USERLIST

# mailbox list
ls -1 $_MailBoxPath > $_MailBoxList

# except list in MailBoxList
sed -i "/_backup/d" $_MailBoxList

# go MailBoxPath
cd $_MailBoxPath

# loop it
echo "========== cleanup box =========="
while read box
do
        grep $box $_USERLIST > /dev/null
        if [[ $? -eq 1 ]]
        then
                echo "mv $box _backup"
        fi
done < $_MailBoxList
echo "============= END ==============="

# Cleanup
rm $_MailBoxList $_USERLIST

 


Check in mailbox

 

cd /var/vmail/MyDoman/MyUser

find . -type d -name cur -exec ls -1 "{}" \; | wc -l

find . -type d -name new -exec ls -1 "{}" \; | wc -l

 


Check User Quota on DB

 

select t1.username, t1.quota, t2.bytes div(1024*1024) from mailbox as t1 join used_quota as t2  on t1.username = t2.username

 


pop3 uidl 保留失敗時

 

1) 從 log 找出 pop3 user - pop3_users.txt

2) 將 inbox 的信放到 Folder MigrationArchive

cd /var/vmail

migrate.sh

#!/bin/bash

set -e

while read USER
do
    echo "Fix $USER"
        archive=$USER/Maildir/.MigrationArchive
        inbox=$USER/Maildir/cur
        if [ -d $inbox ]; then
                echo "Move Folder"
                #mkdir $archive
                #mv $inbox $archive
        fi
        if [ -d $archive/cur ]; then
                        echo "Ok"
        fi
done < "pop3_users.txt"

 

Creative Commons license icon Creative Commons license icon