This all assumes that you want to collect info on bounces rather than the bounced email itself:
I have pretty much the same setup with postfix, mysql, and virtual hosts. From n hardware resource perspective the most efficient way to keep track of this is to parse the log files as you are doing. But if you think how you're parsing is to big of a load, you can use an application like Logwatch to do all the parsing for you. Then set up Postfix to ditch the bounced files for you.
Now if you decide you actually want to collect these emails somewhere you can use these settings in the main.cf file:
bounce_notice_recipient = someone@nowhere.com
error_notice_recipient = someone@nowhere.com
And if you want the emails to be completely destroyed you can add a virtual user and adjust you aliases file to send them to dev/null
someone: /dev/null
As for a script and a database I work with PHP and MySQL a lot these days so if I were to use those tools I could create some php code to read in the log file, look for the bounces, and then right them into a database. Then I'd run the code before the mail.log was truncated. In fact, I'll post the code here after I write it for kicks.
Here's some code if you want to run this with php/mysql (I'm sure it could be prettier):
<?php
#parse_logs.php
# load local file into array
$val = file("mail.log");
$pattern = '/status=bounced/';
foreach ($val as &$value) {
if (preg_match($pattern,$value)) {
$a = split('[<>]', $value);
//if you prefer you can also use: preg_match_all('/<(.*)>/', '$value', $matches);
#can be helpful to print the following to the screen during tests
# echo $a[1];
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("postfix_db") or die(mysql_error());
// Insert a row of information into the table "example"
mysql_query("INSERT INTO emails (emailaddress) VALUES('$a[1]') ")
or die(mysql_error());
#again, if you want to see while running manually from cli
#echo "Data Inserted!";
}
#again, if you want to see while running manually from cli
#echo "\n";
}
?>
You could then fire off a cron right before your mail.log is set to be recycled or clear out the log once the cron fires off.
Seems like a lot of effort to keep track of email addresses of bounced emails. You'll need to write mysql queries to then access this info of course.
You could also skip the mysql stuff completely and just pipe the results to a test file or an email address (and could also use a cron)
php parse.php > results.txt
or
php parse_logs.php | /usr/sbin/sendmail someemailaddress@nowhere.com