Qque suggestion sur update_quota_mail
This commit is contained in:
parent
a65afb62c3
commit
42b8988022
|
@ -4,93 +4,111 @@
|
||||||
#You can call this script either without arguments, inwich case each maildir quotas will be recalculated
|
#You can call this script either without arguments, inwich case each maildir quotas will be recalculated
|
||||||
#or you can call it with a directory reffering to a maildir to just sync one mailbox
|
#or you can call it with a directory reffering to a maildir to just sync one mailbox
|
||||||
|
|
||||||
#gerer les options : tout , 1boite , un domaine, un compte
|
function showhelp() {
|
||||||
|
echo "FIXME: some help"
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Generate the $maildirs list based on the arguments
|
||||||
while getopts "a:m:d:c:" optname
|
while getopts "a:m:d:c:" optname
|
||||||
do
|
do
|
||||||
case "$optname" in
|
case "$optname" in
|
||||||
"a")
|
"a")
|
||||||
|
# All mails
|
||||||
|
# FIXME replace it by a select in da DB
|
||||||
maildirs=`find "$ALTERNC_MAIL/" -maxdepth 2 -mindepth 2 -type d`
|
maildirs=`find "$ALTERNC_MAIL/" -maxdepth 2 -mindepth 2 -type d`
|
||||||
;;
|
;;
|
||||||
"m")
|
"m")
|
||||||
|
# An email
|
||||||
if [[ "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then
|
if [[ "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then
|
||||||
if [[ "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then
|
if [[ "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then
|
||||||
maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")
|
maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")
|
||||||
else
|
else
|
||||||
echo "Bad mail provided"
|
echo "Bad mail provided"
|
||||||
|
showhelp
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "Bad mail provided"
|
echo "Bad mail provided"
|
||||||
|
showhelp
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
"d")
|
"d")
|
||||||
if [[ "$OPTARG" =~ ^[a-z\-]+(\.[a-z\-]+)+$ ]] ; then
|
# Expecting a domain
|
||||||
if [[ "$(mysql_query "select domaine from domaines where domaine = '$OPTARG'")" ]]; then
|
|
||||||
maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG'")
|
# Check if domain is well-formed
|
||||||
else
|
if [[ ! "$OPTARG" =~ ^[a-z\-]+(\.[a-z\-]+)+$ ]] ; then
|
||||||
echo "Bad domain provided"
|
echo "Bad domain provided"
|
||||||
fi
|
showhelp
|
||||||
else
|
|
||||||
echo "Bad domain provided 2"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Attemp to get from database.
|
||||||
|
if [[ ! "$(mysql_query "select domaine from domaines where domaine = '$OPTARG'")" ]]; then
|
||||||
|
# Seem to be empty
|
||||||
|
echo "Bad domain provided"
|
||||||
|
showhelp
|
||||||
|
fi
|
||||||
|
|
||||||
|
maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG'")
|
||||||
;;
|
;;
|
||||||
"c")
|
"c")
|
||||||
|
# An account
|
||||||
if [[ "$OPTARG" =~ ^[a-z]*$ ]] ; then
|
if [[ "$OPTARG" =~ ^[a-z]*$ ]] ; then
|
||||||
if [[ "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then
|
if [[ "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then
|
||||||
maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG")
|
maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG")
|
||||||
else
|
else
|
||||||
echo "Bad account provided"
|
echo "Bad account provided"
|
||||||
|
showhelp
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "Bad account provided"
|
echo "Bad account provided"
|
||||||
|
showhelp
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
"?")
|
"?")
|
||||||
echo "Unknown option $OPTARG - stop processing"
|
echo "Unknown option $OPTARG - stop processing"
|
||||||
|
showhelp
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
":")
|
":")
|
||||||
echo "No argument value for option $OPTARG - stop processing"
|
echo "No argument value for option $OPTARG - stop processing"
|
||||||
|
showhelp
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# Should not occur
|
# Should not occur
|
||||||
echo "Unknown error while processing options"
|
echo "Unknown error while processing options"
|
||||||
|
showhelp
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Now we have $maildirs, we can work on it
|
||||||
|
|
||||||
|
# FIXME add check if maildir is empty
|
||||||
|
|
||||||
#Then we loop through every maildir to get the maildir size
|
#Then we loop through every maildir to get the maildir size
|
||||||
for i in $maildirs ; do
|
for i in $maildirs ; do
|
||||||
|
|
||||||
if [ -d "$i" ];then
|
if [ ! -d "$i" ];then
|
||||||
user=`ls -l $i| tail -n 1|cut -d' ' -f 3`
|
|
||||||
# We grep only mails, not the others files
|
|
||||||
mails=`find $i -type f | egrep "(^$i)*[0-9]+\.M"`
|
|
||||||
|
|
||||||
# This part only count mails size
|
|
||||||
#size=0
|
|
||||||
#for j in $mails
|
|
||||||
#do
|
|
||||||
# size=$(( $size + `du -b $j|awk '{print $1}'`))
|
|
||||||
#done
|
|
||||||
|
|
||||||
# This part count the total mailbox size (mails + sieve scripts + ...)
|
|
||||||
size=`du -b -s $i|awk '{print $1}'`
|
|
||||||
|
|
||||||
mail_count=`echo $mails|wc -w`
|
|
||||||
echo "folder : "$i
|
|
||||||
echo "mail count : "$mail_count
|
|
||||||
echo "dir size : "$size
|
|
||||||
echo ""
|
|
||||||
#update the mailbox table accordingly
|
|
||||||
mysql_query "UPDATE mailbox SET bytes=$size WHERE path='$i' "
|
|
||||||
mysql_query "UPDATE mailbox SET messages=$mail_count WHERE path='$i' "
|
|
||||||
else
|
|
||||||
echo "The maildir $i does not exists. It's quota won't be resync"
|
echo "The maildir $i does not exists. It's quota won't be resync"
|
||||||
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# We grep only mails, not the others files
|
||||||
|
mails=`find $i -type f | egrep "(^$i)*[0-9]+\.M"`
|
||||||
|
|
||||||
|
# This part count the total mailbox size (mails + sieve scripts + ...)
|
||||||
|
size=`du -b -s $i|awk '{print $1}'`
|
||||||
|
|
||||||
|
mail_count=`echo $mails|wc -w`
|
||||||
|
echo "folder : "$i
|
||||||
|
echo "mail count : "$mail_count
|
||||||
|
echo "dir size : "$size
|
||||||
|
echo ""
|
||||||
|
#update the mailbox table accordingly
|
||||||
|
mysql_query "UPDATE mailbox SET bytes=$size WHERE path='$i' ; "
|
||||||
|
mysql_query "UPDATE mailbox SET messages=$mail_count WHERE path='$i' ; "
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue