Alternc  latest
Alternc logiel libre pour l'hébergement
update_quota_mail.sh
Go to the documentation of this file.
1 #!/bin/bash
2 . /usr/lib/alternc/functions.sh
3 
4 #You can call this script either without arguments, inwich case each maildir quotas will be recalculated
5 #or you can call it with a directory reffering to a maildir to just sync one mailbox
6 
7 function showhelp() {
8  echo "FIXME: some help"
9  exit
10 }
11 
12 # FIXME: storing THAT amount of data in MAILDIRS (on BIG install like Lautre.net) may crash the shell?
13 
14 # Generate the $maildirs list based on the arguments
15 while getopts "am:d:c:" optname
16 do
17  case "$optname" in
18  "a")
19  # All mails
20  maildirs=$(mysql_query "select userdb_home from dovecot_view order by 1")
21  ;;
22  "m")
23  # An email
24  if [[ "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then
25  if [[ "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then
26  maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG' order by 1")
27  else
28  echo "Bad mail provided"
29  showhelp
30  fi
31  else
32  echo "Bad mail provided"
33  showhelp
34  fi
35  ;;
36  "d")
37  # Expecting a domain
38 
39  # Check if domain is well-formed
40  if [[ ! "$OPTARG" =~ ^[a-z\-]+(\.[a-z\-]+)+$ ]] ; then
41  echo "Bad domain provided"
42  showhelp
43  fi
44 
45  # Attemp to get from database.
46  if [[ ! "$(mysql_query "select domaine from domaines where domaine = '$OPTARG'")" ]]; then
47  # Seem to be empty
48  echo "Bad domain provided"
49  showhelp
50  fi
51 
52  maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG' order by 1")
53  ;;
54  "c")
55  # An account
56  if [[ "$OPTARG" =~ ^[a-z]*$ ]] ; then
57  if [[ "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then
58  maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG order by 1")
59  else
60  echo "Bad account provided"
61  showhelp
62  fi
63  else
64  echo "Bad account provided"
65  showhelp
66  fi
67  ;;
68  "?")
69  echo "Unknown option $OPTARG - stop processing"
70  showhelp
71  exit
72  ;;
73  ":")
74  echo "No argument value for option $OPTARG - stop processing"
75  showhelp
76  exit
77  ;;
78  *)
79  # Should not occur
80  echo "Unknown error while processing options"
81  showhelp
82  exit
83  ;;
84  esac
85 done
86 
87 # Now we have $maildirs, we can work on it
88 
89 # FIXME add check if maildir is empty
90 
91 #Then we loop through every maildir to get the maildir size
92 for i in $maildirs ; do
93 
94  if [ ! -d "$i" ];then
95  echo "The maildir $i does not exists. It's quota won't be resync"
96  continue
97  fi
98 
99  # We grep only mails, not the others files
100  mails=`find $i -type f | egrep "(^$i)*[0-9]+\.M"`
101 
102  # This part count the total mailbox size (mails + sieve scripts + ...)
103  size=`du -b -s $i|awk '{print $1}'`
104 
105  mail_count=`echo $mails|wc -w`
106  echo "folder : "$i
107  echo "mail count : "$mail_count
108  echo "dir size : "$size
109  echo ""
110  #update the mailbox table accordingly
111  MAILADD=`basename $i`
112  MAILADD=${MAILADD/_/@}
113  mysql_query "REPLACE INTO dovecot_quota VALUES('$MAILADD', $size, $mail_count);"
114 done
115 
116 # may cause a problem, let's fix this here :)
117 mysql_query "UPDATE mailbox SET quota=0 WHERE quota IS NULL;"