View Single Post
  #1  
Old 15th January 2012, 19:54
tio289 tio289 is offline
Member
 
Join Date: Mar 2009
Posts: 70
Thanks: 2
Thanked 14 Times in 9 Posts
Smile Ispconfig 3 and backup MX server w/o mysql

Hi guys, I decided to post guide how to setup backup MX server for master MX running with ispconfig3. Without MySql running on it. So let's go on.

First we install required packages:

Code:
# aptitude install php5 php5-cli php5-mysql postfix
next we configure postfix as relay:

Code:
# file /etc/postfix/main.cf

myhostname = mx2.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, $mydomain

smtpd_banner = $mydomain MX Backup
biff = no

alias_maps = 

relay_domains = hash:/etc/postfix/relay_domains
relay_recipient_maps = hash:/etc/postfix/relay_recipients
queue_run_delay = 200s

smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks,
  reject_non_fqdn_hostname,
  reject_non_fqdn_sender,
  reject_non_fqdn_recipient,
  reject_unauth_destination,
  reject_unauth_pipelining,
  reject_invalid_hostname,
  reject_rbl_client zen.spamhaus.org
smtpd_helo_required = yes
disable_vrfy_command = yes
smtpd_data_restrictions =
  reject_unauth_pipelining,
  permit
Create new user in master database and grant SELECT permission for dbispconfig database.

them create php script which connects to remote master server and download relay domains and recipient maps.

PHP Code:
<?php
// file /etc/postfix/map_relay.php

$link mysql_connect('mx1.example.com''username''password'falseMYSQL_CLIENT_SSL);

if(!
$link) die;

mysql_select_db('dbispconfig'$link);

$result mysql_query('SELECT `domain` FROM `mail_domain` WHERE `active` = "y"');
create_map($result'domain''relay_domains');

$result mysql_query('SELECT `source` FROM `mail_forwarding` WHERE `active` = "y"');
create_map($result'source''relay_recipients');

$result mysql_query('SELECT `email` FROM `mail_user` WHERE `disabledeliver` = "n"');
create_map($result'email''relay_recipients''a');

function 
create_map($result$key$file$type 'w')
{
    if(
mysql_num_rows($result) == 0)return false;
    
    
$content '';
    
    while(
$row mysql_fetch_array($result))
    {
        
$content .= $row[$key]."\tOK\n";
    }
    
    
write_file($file$content$type);
}

function 
write_file($file$content$type 'w')
{
    
$handle fopen($file$type);
    
fwrite($handle$content);
    
fclose($handle);
    
    return;
}
Of course, you must change connect settings. You see that I use SSL when connecting to remote db. If you don't need it, simple remove last two args from mysql_connect function. If you want use SSL, them you must configure remote db to use SSL So on remote db config add these lines:
Code:
# file /etc/mysql/my.cnf

ssl-cert=/path/to/server.crt
ssl-key=/path/to/server.key
and restart mysql server.

If you don't know ho to create certs look at this http://docs.google.com/View?id=dhp2k7sw_35gx9b5ffn

We test it now
Code:
# php /etc/postfix/map_relay.php
# postmap hash:/etc/postfix/relay_domains
# postmap hash:/etc/postfix/relay_recipients
# /etc/init.d/postfix restart
If all is ok, them we can add it to crontab

Code:
# crontab -e
add this line: (this update maps every hour)
Code:
0 * * * * /usr/bin/php /etc/postfix/map_relay.php && postmap hash:/etc/postfix/relay_domains && postmap hash:/etc/postfix/relay_recipients
Any enhancements?
__________________
Independent Senior Webdeveloper
Reply With Quote
Sponsored Links