Script untuk menghapus email account dengan jumlah quota tertentu pada qmail

Beberapa hari yang lalu saya mempunyai kesulitan untuk menghapus ribuan email account dengan quota email tertentu. Karena saya hanya boleh menghapus email account dengan quota 10 MB atau kurang (nah lho). Tadinya saya berpikir untuk menghapus satu persatu, tapi begitu saya sadar ada lebih dari 5000 email, saya jadi berpikir kapan akan selesai :((

Akhirnya saya putuskan untuk membuat sebuah script yang dapat digunakan untuk menghapus email dengan mudah cepat, dan tidak repot :p

Berikut script shell yang saya buat :

##!/bin/sh
# Delete email by quota version 1.0 #
# Created July, 10 2008, 10:45 #
# This script allow you to delete email accounts by selected quota only #
# exclude postmaster account. Qmail server associated #
# name : deleteAccByQuota.sh #
#=====================================================#

#configuration
trap “” 1 2 3 15
Path=”/home/vpopmail/bin/”
Domain=$1

ERROR=0
ARGV=”$@”
if [ “x$ARGV” = “x” ]; then
ARGS=”–help”
fi

function error {
echo ” Error : Can’t find domain name or invalid input”
echo “”
help
}
function help {

echo ” help :”
echo ” Syntax deleteAccByQuota.sh ”
echo ” options :”
echo -e ” –help \t or \t -h \t : open help”
echo -e ” –version \t or \t -v \t : Version”

}

function CachingUserQuota {
echo -e “Clear old data \t OK”
`rm -f /tmp/result-mail.txt`
n=0
for list in $($Path””vuserinfo -q -n -D $Domain); do
echo -n -e “caching email username and quota \t – \r”
n=`expr $n + 1`
result=`expr $n % 2`
if [ $result -eq 1 ]; then
tempword=$list
echo -n -e “caching email username and quota \t \\ \r”
elif [ $result -eq 0 ]; then
tempword=$tempword”:”$list
`echo “$tempword” >> /tmp/result-mail.txt`
tempword=””
echo -n -e “caching email username and quota \t | \r”
fi
echo -n -e “caching email username and quota \t / \r”
done
echo -e “caching email username and quota \t OK”

}

# to delete email account with spesific quota
#cat result-mail.txt | grep 10M | cut -d: -f1
function DeleteAccount {
CachingUserQuota
SelQuota=$1
count=0

for AccountPop in $(cat /tmp/result-mail.txt); do
check=`echo $AccountPop | cut -d: -f2`
Accntuser=`echo $AccountPop | cut -d: -f1`

if [ “$check” = “$SelQuota” -a “$Accntuser” != “postmaster” ]; then
#Delete account
echo -e -n “$Accntuser”
echo -e “\r\t\t\t\t\t\t deleted.”
$Path””vdeluser $Accntuser”@”$Domain
count=`expr $count + 1`
#else
#echo “$Accntuser not deleted.”
fi
done

#Delete temp file
`rm -f /tmp/result-mail.txt`
echo “$count account(s) has been deleted.”
}

# See all email quota on the server
function Main {
#cache the email quota on the server
echo “Delete email for the specific quota limit. ”
echo “This script will help you to delete email account(s)”
echo “based on qmail quota limit.”
echo “Choose quota limit : ”
echo “(S on byte , M on Mb)”

#Ref : this below line to count how many users under this domain
#MaxList=`/home/vpopmail/bin/vuserinfo -q -D vs.moe.edu.sg | sort | uniq | wc -l`

n=0
for QuotaList in $($Path””vuserinfo -q -D $Domain | sort | uniq); do
n=`expr $n + 1`
echo -e “$n) $QuotaList.”
done

n=`expr $n + 1`
echo “$n) Quit”

#Make sure input is integer
TestInput=”false”
while [ “$TestInput” = “false” ]; do
echo -n “enter your choice : ”
read choice
case $choice in (*[!0-9]*) printf “No digit found: %s\n” “$v”;;esac > /tmp/check
errormsg=`cat /tmp/check`
rm -f /tmp/check
if [ “x$errormsg” = “x” ]; then
TestInput=”true”
else
echo “error : Invalid input, input must be numeric”
exit
fi
done

i=0
for List in $($Path””vuserinfo -q -D $Domain | sort | uniq); do
i=`expr $i + 1`
#Choose the limit
if [ $i -eq $choice ]; then
size=$List
fi
done

#Quit option
if [ $n -eq $choice ]; then
exit
elif [ $choice -gt $n ]; then
echo “error : Invalid input, the option is unavailable.”
exit
else

#confirmed the option
confirm=no
echo -n “Are you sure to perform this action? [y/n] [default n] ”
read input
if [ “$input” = “y” ]; then
DeleteAccount $size
else
exit
fi
fi
}

#Main program

if [ “$1” = “–help” -o “$ARG” = “–help” -o “x$1” = “x” ]; then
help
elif [ “$1” = “–version” -o “$1” = “-v” ]; then
echo “Version 1.0”
exit

else

$Path””vdominfo $1 > /tmp/check
MSG=`cat /tmp/check | grep “does not exist”`
`rm -f /tmp/check`

if [ “x$MSG” = “x” ]; then
Main;
else
error
fi
fi
#End

Cara eksekusi :

./deleteAccByQuota.sh [nama domain]

Script ini dibuat untuk menghapus email pada Qmail server, environment dan atau variabel (path) bisa di ubah untuk menyesuaikan mengikuti configurasi server.

Semoga bermanfaat

~ oleh r420r pada Juli 16, 2008.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

 
%d blogger menyukai ini: