From 479c715e7271e94ab0051311303c676fe35af09f Mon Sep 17 00:00:00 2001 From: Steven Mondji-Lerider Date: Wed, 23 Jan 2013 19:26:42 +0000 Subject: [PATCH] Updating dovecot quota's synching scripts --- src/update_quota_mail.sh | 59 ++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/update_quota_mail.sh b/src/update_quota_mail.sh index 2bc0f412..8917de4b 100755 --- a/src/update_quota_mail.sh +++ b/src/update_quota_mail.sh @@ -1,31 +1,48 @@ -#! /bin/bash - +#!/bin/bash . /usr/lib/alternc/functions.sh -#Fist we set the quotas no 0 (infinite for each already existing account -t=`mysql_query "UPDATE mailbox SET quota='0' WHERE quota IS NULL"` +#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 + +#basic checks +if [ $# -gt 1 ]; then + echo "usage : update_quota_mail.sh (Maildir)." + exit +fi + +if [ $# -eq 1 ];then + if [ ! -d "$1" ];then + echo "$1 is not a directory, aborting." + exit + else + d="$1" + fi +else + #Fist we set the quotas no 0 (infinite for each already existing account + t=`mysql_query "UPDATE mailbox SET quota='0' WHERE quota IS NULL"` + d="$ALTERNC_LOC/mail/*/*" +fi + #Then we loop through every maildir to get the maildir size -for i in $ALTERNC_LOC/mail/*/* ; do +for i in $d ; do - user=`ls -l $i| tail -n 1|cut -d' ' -f 3` - size=`du -s $i|awk '{print $1}'` - q=`mysql_query "SELECT * FROM mailbox where path = '$i' "` + if [ -d "$i" ];then + user=`ls -l $i| tail -n 1|cut -d' ' -f 3` + size=`du -s $i|awk '{print $1}'` - #when counting mails we eclude dovecot specific files - mail_count=`find $i -type f |grep -v dovecot* |wc -l` - echo "folder : "$i - echo "mail count : "$mail_count - echo "dir size : "$size - echo "" - - if [ -z "$q" ]; then - echo "no mail folder found for user $user " - else + #when counting mails we exclude specific files + mail_count=`find $i -type f -printf "%f\n"| egrep '^[0-9]+\.M'|wc -w` + echo "folder : "$i + echo "mail count : "$mail_count + echo "dir size : "$size + echo "" #update the mailbox table accordingly - q=`mysql_query "UPDATE mailbox SET bytes=$size WHERE path='$i' "` - q=`mysql_query "UPDATE mailbox SET messages=$mail_count WHERE path='$i' "` - fi + 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" + fi done