Commit 7ef2d8bd authored by Squidly's avatar Squidly

Polishing mailbox quota updating script

parent 77bda2ca
...@@ -5,83 +5,67 @@ ...@@ -5,83 +5,67 @@
#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
function showhelp() { function showhelp() {
echo "FIXME: some help" echo "$1"
exit exit
} }
# Generate the $maildirs list based on the arguments # 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 # All mails
# FIXME replace it by a select in da DB maildirs=$(mysql_query "select path from mailbox")
maildirs=`find "$ALTERNC_MAIL/" -maxdepth 2 -mindepth 2 -type d` ;;
;; "m")
"m") # An email
# An email if [[ ! "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then
if [[ "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then showhelp "bad mail address provided"
if [[ "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then fi
maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'") if [[! "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then
else showhelp "non existant mail address"
echo "Bad mail provided"
showhelp
fi
else
echo "Bad mail provided"
showhelp
fi fi
;; maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")
"d") ;;
# Expecting a domain "d")
# Expecting a domain
# Check if domain is well-formed # Check if domain is well-formed
if [[ ! "$OPTARG" =~ ^[a-z\-]+(\.[a-z\-]+)+$ ]] ; then if [[ ! "$OPTARG" =~ ^[a-z\-]+(\.[a-z\-]+)+$ ]] ; then
echo "Bad domain provided" showhelp "bad domain provided"
showhelp
fi fi
# Attemp to get from database. # Attemp to get from database.
if [[ ! "$(mysql_query "select domaine from domaines where domaine = '$OPTARG'")" ]]; then if [[ ! "$(mysql_query "select domaine from domaines where domaine = '$OPTARG'")" ]]; then
# Seem to be empty # Seem to be empty
echo "Bad domain provided" showhelp "non existant domain"
showhelp fi
fi
maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG'") maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG'")
;; ;;
"c") "c")
# An account # An account
if [[ "$OPTARG" =~ ^[a-z]*$ ]] ; then if [[! "$OPTARG" =~ ^[a-z]*$ ]] ; then
if [[ "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then showhelp "bad account provided"
maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG") fi
else if [[! "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then
echo "Bad account provided" showhelp "non existant account"
showhelp
fi
else
echo "Bad account provided"
showhelp
fi fi
;; maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG")
"?") ;;
echo "Unknown option $OPTARG - stop processing" "?")
showhelp showhelp "Unknown option $OPTARG - stop processing"
exit ;;
;; ":")
":") showhelp "No argument value for option $OPTARG - stop processing"
echo "No argument value for option $OPTARG - stop processing" ;;
showhelp *)
exit # Should not occur
;; echo
*) showhelp "Unknown error while processing options"
# Should not occur ;;
echo "Unknown error while processing options" esac
showhelp
exit
;;
esac
done done
# Now we have $maildirs, we can work on it # Now we have $maildirs, we can work on it
...@@ -91,24 +75,30 @@ done ...@@ -91,24 +75,30 @@ done
#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
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 continue
fi fi
# We grep only mails, not the others files if [ ! -d "$i" ];then
mails=`find $i -type f | egrep "(^$i)*[0-9]+\.M"` echo "The maildir $i does not exists. It's quota won't be resync"
continue
# This part count the total mailbox size (mails + sieve scripts + ...) fi
size=`du -b -s $i|awk '{print $1}'`
# We grep only mails, not the others files
mail_count=`echo $mails|wc -w` mails=`find $i -type f | egrep "(^$i)*[0-9]+\.M"`
echo "folder : "$i
echo "mail count : "$mail_count # This part count the total mailbox size (mails + sieve scripts + ...)
echo "dir size : "$size size=`du -b -s $i|awk '{print $1}'`
echo ""
#update the mailbox table accordingly mail_count=`echo $mails|wc -w`
mysql_query "UPDATE mailbox SET bytes=$size WHERE path='$i' ; " echo "folder : "$i
mysql_query "UPDATE mailbox SET messages=$mail_count WHERE path='$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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment