Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > ISPConfig 3 > Tips/Tricks/Mods

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 15th January 2012, 18: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
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)
Sponsored Links
  #2  
Old 16th January 2012, 16:05
tio289 tio289 is offline
Member
 
Join Date: Mar 2009
Posts: 70
Thanks: 2
Thanked 14 Times in 9 Posts
Default

Hi guys, I have problem with run php scripts from crontab. it do nothing.
__________________
Independent Senior Webdeveloper
Reply With Quote
  #3  
Old 17th January 2012, 14:20
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,743 Times in 2,577 Posts
Default

Does the command work when you run it manually?
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #4  
Old 17th January 2012, 14:27
tio289 tio289 is offline
Member
 
Join Date: Mar 2009
Posts: 70
Thanks: 2
Thanked 14 Times in 9 Posts
Default

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?
__________________
Independent Senior Webdeveloper

Last edited by tio289; 18th January 2012 at 16:55.
Reply With Quote
  #5  
Old 24th January 2012, 19:50
tio289 tio289 is offline
Member
 
Join Date: Mar 2009
Posts: 70
Thanks: 2
Thanked 14 Times in 9 Posts
Default

Fixed conf:

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''/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
__________________
Independent Senior Webdeveloper
Reply With Quote
  #6  
Old 13th March 2012, 21:36
mkubenka mkubenka is offline
Junior Member
 
Join Date: Jan 2011
Posts: 1
Thanks: 1
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
The Following User Says Thank You to mkubenka For This Useful Post:
tio289 (13th March 2012)
  #7  
Old 14th March 2012, 10:15
tio289 tio289 is offline
Member
 
Join Date: Mar 2009
Posts: 70
Thanks: 2
Thanked 14 Times in 9 Posts
Default

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'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''/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;
}
__________________
Independent Senior Webdeveloper
Reply With Quote
  #8  
Old 26th October 2012, 18:18
Plastivore Plastivore is offline
Junior Member
 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
 
Default

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'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''/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...
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Version 1.3 of the ISPConfig 3 Manual is finally available! falko General 44 2nd December 2011 12:04
Complete System Backup of a ISPConfig 3 server The General General 1 8th January 2011 12:37
CPU load locks up box. Apache or MYSQL related. crypted General 61 29th October 2010 22:16
Automatic backup of ISPConfig server patrick3853 General 1 14th July 2009 08:31
Setting up a backup mail server setup with two installations of ISPConfig zitch Tips/Tricks/Mods 7 30th December 2006 10:07


All times are GMT +2. The time now is 06:12.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.