Postfix, Dynamic OverQuota User Map Script Using Bash And Inotifywait
I recently experimented with a simple bash script, inotifywait and smtpd_recipient_restrictions (check_recipient_access) to map email users who have exceeded the quota.
Well, during testing, i’ve noticed when using hash/texthash lookup tables, it needed to be reloaded in order smtpd detect changes in table.so i’ve made quick test on mysql_tables it seem updating record on tables will immediately able to be queried
Mapping can be done as follows:
main.cf:
smtpd_recipient_restrictions = check_recipient_access mysql:/etc/postfix/mysql_quota_access.cf, ... ...
mysql_quota_access.cf
user = user password = password hosts = localhost dbname = postfixdb query = SELECT qaction FROM quota WHERE username='%s'
create mysql table called quota:
CREATE TABLE quota ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(100), qaction VARCHAR(100) ) TYPE=innodb;
Here’s the idea, inotifywait will continuously monitor the maildir directory recursively, and updates “qaction” field on “quota” mysql table whenever new mail arrived or whenever there is email deleted from the maildir.
initial map, can be produced by retrieving user information from database.for example, username information in the database “postfixdb” with the table name “mailbox” and field “username”.
# for i in `mysql -u user -ppassword -D postfixdb -e 'SELECT username FROM mailbox' | grep -v username`;do mysql -u user -ppassword -D postfixdb -e "INSERT INTO quota (username, qaction) VALUES ('$i', 'DUNNO')";
With this script,value of qaction field on mysql quota table will change continuously as the user’s maildir contents that keeps changing.
Postfix, One Way Maildir Replication / Backup Using Inotify And Rsync
After I wrote about Maildir replication, using ChironFS and DRBD, this time I will write how to make maildir replication, using a very well known program utility called rsync. basically, rsync itself, does not do realtime replication process. rsync only perform the synchronization/copy process when needed or scheduled by using the crontab. like cp, rsync is used to copy files from one directory to another directory in one system, or to a directory on another system. and vice versa.
How do we make the process of replication/copy that is almost realtime by using rsync?
we will use the inotify-tools (inotifywait) to monitor changes to system files or directories, in this case is the postfix maildir. Inotify has been included in the mainline Linux kernel from release 2.6.13 (June 18, 2005), and could be compiled into 2.6.12 and possibly earlier releases by use of a patch.
What is inotify?
Inotify is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, dnotify, which had similar goals.
OK, without further ado, let’s continue with the first step, install inotify-tools. on my centos machine, it can be done in the following way.
$ sudo yum -y install inotify-tools
Assume that we have two servers, first server contains a postfix + maildir. second servers is used to backup maildir from the first server. using inotifywait, any changes in the maildir on first server will trigger rsync to update the maildir on the backup server. However, first we will make rsync can do the login automatically to the backup server via ssh using Public Key Based Authentication.
On First server
[first_server] $ ssh-keygen -t dsa -f ~/.ssh/identity && cat ~/.ssh/identity.pub | ssh -l postfix second_server -p 12345 'sh -c "cat - >>~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"'