
15th January 2012, 18:54
|
|
Member
|
|
Join Date: Mar 2009
Posts: 68
Thanks: 2
Thanked 14 Times in 9 Posts
|
|
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', false, MYSQL_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
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?
|
|
The Following 3 Users Say Thank You to tio289 For This Useful Post:
|
falko (16th January 2012),
mkubenka (13th March 2012),
nveid (17th January 2012)
|

16th January 2012, 16:05
|
|
Member
|
|
Join Date: Mar 2009
Posts: 68
Thanks: 2
Thanked 14 Times in 9 Posts
|
|
Hi guys, I have problem with run php scripts from crontab. it do nothing.
|

17th January 2012, 14:20
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,665
Thanks: 1,896
Thanked 2,592 Times in 2,443 Posts
|
|
Does the command work when you run it manually?
|

17th January 2012, 14:27
|
|
Member
|
|
Join Date: Mar 2009
Posts: 68
Thanks: 2
Thanked 14 Times in 9 Posts
|
|
yes. I try to make sh script like /usr/local/ispconfig/server.sh, but w/o success.
Code:
s2:/etc/postfix# ls -la
total 120
drwxr-xr-x 2 root root 4096 Jan 16 08:43 .
drwxr-xr-x 93 root root 4096 Jan 16 08:44 ..
-rw-r--r-- 1 root root 319 Jan 15 19:00 dynamicmaps.cf
-rw-r--r-- 1 root root 804 Jan 15 13:15 main.cf
-rwxr-x--- 1 root root 970 Jan 16 08:45 map_relay.php
-rwxr-x--- 1 root root 236 Jan 16 08:44 map_relay.sh
-rw-r--r-- 1 root root 5302 Jan 15 11:31 master.cf
-rw-r--r-- 1 root root 0 Jan 9 14:48 mime_header_checks
-rw-r--r-- 1 root root 0 Jan 9 14:48 nested_header_checks
-rw-r--r-- 1 root root 18992 May 4 2011 postfix-files
-rwxr-xr-x 1 root root 8729 May 4 2011 postfix-script
-rwxr-xr-x 1 root root 24256 May 4 2011 post-install
-rw-r--r-- 1 root root 708 Jan 16 16:02 relay_domains
-rw-r--r-- 1 root root 12288 Jan 17 14:00 relay_domains.db
-rw-r--r-- 1 root root 1573 Jan 16 16:02 relay_recipients
-rw-r--r-- 1 root root 12288 Jan 17 14:00 relay_recipients.db
Code:
s2:/etc/postfix# cat map_relay.sh
#!/bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin
. /etc/profile
/usr/bin/php -q /etc/postfix/map_relay.php
postmap hash:/etc/postfix/relay_domains
postmap hash:/etc/postfix/relay_recipients
I fixed problem by adding absolute path to maps in PHP script
To Moderators: Can I edit first post please?
Last edited by tio289; 18th January 2012 at 16:55.
|

24th January 2012, 19:50
|
|
Member
|
|
Join Date: Mar 2009
Posts: 68
Thanks: 2
Thanked 14 Times in 9 Posts
|
|
Fixed conf:
PHP Code:
<?php
// file /etc/postfix/map_relay.php
$link = mysql_connect('mx1.example.com', 'username', 'password', false, MYSQL_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', '/etc/postfix/relay_domains');
$result = mysql_query('SELECT `source` FROM `mail_forwarding` WHERE `active` = "y"');
create_map($result, 'source', '/etc/postfix/relay_recipients');
$result = mysql_query('SELECT `email` FROM `mail_user` WHERE `disabledeliver` = "n"');
create_map($result, 'email', '/etc/postfix/relay_recipients', 'a');
exec('postmap hash:/etc/postfix/relay_domains & postmap hash:/etc/postfix/relay_recipients');
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;
}
crontab:
Code:
0 * * * * /usr/bin/php -q /etc/postfix/map_relay.php
|

13th March 2012, 21:36
|
|
Junior Member
|
|
Join Date: Jan 2011
Posts: 1
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Don't put `$mydomain` in `mydestination` if you want backup your domain too. It took me hours to find it out. Check http://www.postfix.org/STANDARD_CONF...ME.html#backup to find out more.
So, to backup even example.com main.cf should be:
Code:
# file /etc/postfix/main.cf
myhostname = mx2.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname
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
|
|
The Following User Says Thank You to mkubenka For This Useful Post:
|
|

14th March 2012, 10:15
|
|
Member
|
|
Join Date: Mar 2009
Posts: 68
Thanks: 2
Thanked 14 Times in 9 Posts
|
|
Fixed PHP script (I add full path to postmap command because cron not found it) :
PHP Code:
<?php
$link = mysql_connect('s0.ikfsystems.sk', 'map_relay', 'RRhQe8qa4Qhmezeq', false, MYSQL_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', '/etc/postfix/relay_domains');
$result = mysql_query('SELECT `source` FROM `mail_forwarding` WHERE `active` = "y"');
create_map($result, 'source', '/etc/postfix/relay_recipients');
$result = mysql_query('SELECT `email` FROM `mail_user` WHERE `disabledeliver` = "n"');
create_map($result, 'email', '/etc/postfix/relay_recipients', 'a');
exec('/usr/sbin/postmap hash:/etc/postfix/relay_domains & /usr/sbin/postmap hash:/etc/postfix/relay_recipients');
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;
}
|

26th October 2012, 18:18
|
|
Junior Member
|
|
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Thanks a lot for this quick tutorial.
I have a suggestion to make:
Personally, at first, I did not use aliases. Therefore, I ended up with relay_recipients containing the same entries several times, as they just ended up being appended at the end of the file.
My understanding is that the relay_recipients file is first overwritten using aliases, and only then will the actual mailboxes be appended.
It is better to first look for mailboxes, and only then for the aliases, for the following reasons:
- It seems more logical (but I admit that could just be a point of view!  )
- A mailbox needs to exist first for an alias to be created
- Mailboxes are repeated at the end of the file if no alias is configured in ISPConfig (but we already know that).
The code should therefore become:
PHP Code:
<?php // file /etc/postfix/map_relay.php
$link = mysql_connect('s0.ikfsystems.sk', 'map_relay', 'RRhQe8qa4Qhmezeq', false, MYSQL_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', '/etc/postfix/relay_domains');
$result = mysql_query('SELECT `email` FROM `mail_user` WHERE `disabledeliver` = "n"'); create_map($result, 'email', '/etc/postfix/relay_recipients');
$result = mysql_query('SELECT `source` FROM `mail_forwarding` WHERE `active` = "y"'); create_map($result, 'source', '/etc/postfix/relay_recipients', 'a');
exec('/usr/sbin/postmap hash:/etc/postfix/relay_domains & /usr/sbin/postmap hash:/etc/postfix/relay_recipients');
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; }
Cheers!
Last edited by Plastivore; 26th October 2012 at 18:21.
Reason: Weird meaning sentece corrected to make proper sense...
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 16:29.
|
Recent comments
7 hours 17 min ago
16 hours 45 min ago
17 hours 35 min ago
21 hours 8 min ago
1 day 1 hour ago
1 day 1 hour ago
1 day 4 hours ago
1 day 14 hours ago
1 day 19 hours ago
1 day 20 hours ago