Benutzer-Werkzeuge

Webseiten-Werkzeuge


tachtler:postfix_amavis_mailzu_installieren

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste ÜberarbeitungBeide Seiten der Revision
tachtler:postfix_amavis_mailzu_installieren [2013/12/08 10:01] – [Postfix AMaViS MailZu Konfiguration AMaViS] klaustachtler:postfix_amavis_mailzu_installieren [2013/12/08 10:19] – [Postfix AMaViS MailZu Datenbank Bereinigung] klaus
Zeile 673: Zeile 673:
 gekennzeichnet sind: gekennzeichnet sind:
  
-Hier de notwendigen Konfigurationen von **''/var/www/html/mailzu/scripts/mz_db_clean.pl''** (**nur relevanter Ausschnitt**):+Hier de notwendigen Konfigurationen von **''/var/www/html/mailzu/scripts/mz_db_clean.pl''** (**komplettes Konfigurations-Skript**):
 <code perl> <code perl>
 #!/usr/bin/perl -w #!/usr/bin/perl -w
-# Tachtler 
-# default: #!/usr/local/bin/perl -w 
  
 use strict; use strict;
Zeile 695: Zeile 693:
         'amavis','geheim'         'amavis','geheim'
                        );                        );
- 
  
 # Set this to 1 if you are using the new database schema # Set this to 1 if you are using the new database schema
Zeile 701: Zeile 698:
 # have the foreign key references between the tables and use # have the foreign key references between the tables and use
 # time_iso as a real date type instead of a string. # time_iso as a real date type instead of a string.
 +# Tachtler
 +# default: my($new_dd) = 1; # 1 or undef
 my($new_dd) = undef; # 1 or undef my($new_dd) = undef; # 1 or undef
  
Zeile 720: Zeile 719:
 # MySQL # MySQL
 # Tachtler # Tachtler
 +# default: #my($new_interval) = '7 day';
 +# default: #my($new_partial_interval) = '1 hour';
 my($new_interval) = '7 day'; my($new_interval) = '7 day';
 my($new_partial_interval) = '1 hour'; my($new_partial_interval) = '1 hour';
Zeile 729: Zeile 730:
  
 ##### END OF CONFIGURATION SECTION ###### ##### END OF CONFIGURATION SECTION ######
-... 
-</code> 
  
-Der Aufruf des Scriptswelches ein Perl-Script istmuss mit einem Parameter für die Angabe des Datenbank-Typshier [[http://www.mysql.de/|MySQL]] erfolgenwas auch eine Abfrage wie nachfolgend dargestellt zeigt+# Options array 
-<code+my(%opt); 
-/var/www/html/mailzu/scripts/mz_db_clean.pl -h + 
-Usage: +my(@modules); 
-        mz_db_clean.pl [--verbose|-v] [--database|-db <dbtype>+my(@missing); 
-        mz_db_clean.pl --help|-h + 
 +my(@dsn) = split(/:/,$storage_sql_dsn[0],-1); 
 +push(@modules'DBD::'.$dsn[1])  if uc($dsn[0]) eq 'DBI'; 
 + 
 +for my $m (@modules) { 
 +  local($_) = $m; 
 +  $_ .= /^auto::/ ? '.al' : '.pm'  if !/\.(pm|pl|al)\z/; 
 +  s[::][/]g; 
 +  eval { require $_ } or push(@missing$m); 
 +
 + 
 +die "ERRORMISSING module(s):\n" . join('', map { "  $_\n" } @missing) if @missing; 
 + 
 +sub build_queries($) { 
 +  my($dbtype) = shift; 
 +  # Return a hash of queries to be run 
 +  my(%query) = ( 
 +               # Old schema queries 
 +               'del_d_flag' => 
 +                 'DELETE FROM msgrcpt ' . 
 +                 'WHERE rs=\'D\'', 
 +               'del_partial_msg' => 
 +                 'DELETE FROM msgs ' . 
 +                 "WHERE time_num $partial_interval " . 
 +                 'AND content IS NULL', 
 +               'del_old_mail_ids' =
 +                 'DELETE FROM msgs ' . 
 +                 "WHERE time_num < $interval", 
 +               'del_msgs_mail_ids' => 
 +                 'DELETE FROM msgs ' . 
 +                 'WHERE NOT EXISTS ' . 
 +                 ' (SELECT 1 FROM msgrcpt ' . 
 +                 '  WHERE msgrcpt.mail_id=msgs.mail_id)', 
 +               'del_quarantine' => 
 +                 'DELETE FROM quarantine ' . 
 +                 'WHERE NOT EXISTS '. 
 +                 ' (SELECT 1 FROM msgs ' . 
 +                 '  WHERE msgs.mail_id=quarantine.mail_id)', 
 +               'del_msgrcpt' => 
 +                 'DELETE FROM msgrcpt ' . 
 +                 'WHERE NOT EXISTS ' . 
 +                 ' (SELECT 1 FROM msgs ' . 
 +                 '  WHERE msgs.mail_id=msgrcpt.mail_id)', 
 + 
 +               New schema queries 
 +               'del_d_flag_new' => 
 +                 'DELETE FROM msgs ' . 
 +                 'WHERE mail_id IN ' . 
 +                 '  (SELECT DISTINCT mail_id ' . 
 +                 '   FROM msgrcpt WHERE rs=\'D\')', 
 + 
 +               # Generic queries 
 +               'del_maddr' => 
 +                 'DELETE FROM maddr ' . 
 +                 'WHERE NOT EXISTS ' 
 +                 '  (SELECT sid FROM msgs WHERE sid=id) ' . 
 +                 '   AND NOT EXISTS'
 +                 '  (SELECT rid FROM msgrcpt WHERE rid=id)' 
 + 
 +             ); 
 + 
 +  if ($dbtype eq 'pgsql') { 
 +    $query{'vacuum_analyze'} = 'VACUUM ANALYZE'; 
 +    # New schema queries 
 +    $query{'del_old_mail_ids_new'} = 'DELETE FROM msgs ' . 
 +                                     'WHERE time_iso < now() ' . 
 +                                     "interval '$new_interval'"; 
 +    $query{'del_partial_msg_new'} = 'DELETE FROM msgs ' . 
 +                                     'WHERE time_iso < now() ' . 
 +                                     "- interval '$new_partial_interval' " . 
 +                                     ' AND content IS NULL'; 
 +  } 
 + 
 +  if ($dbtype eq 'mysql') { 
 +    # New schema queries 
 +    $query{'del_old_mail_ids_new'} = 'DELETE FROM msgs ' . 
 +                                     'WHERE time_iso < UTC_TIMESTAMP() ' . 
 +                                     "- interval $new_interval"; 
 +    $query{'del_partial_msg_new'} = 'DELETE FROM msgs ' . 
 +                                     'WHERE time_iso < UTC_TIMESTAMP() ' . 
 +                                     "- interval $new_partial_interval " . 
 +                                     ' AND content IS NULL'; 
 + 
 +    # Old schema queries 
 +    $query{'del_msgs_mail_ids'} = 'DELETE msgs FROM msgs ' . 
 +                                  'LEFT JOIN msgrcpt USING(mail_id) ' . 
 +                                  'WHERE msgrcpt.mail_id IS NULL'; 
 +    $query{'del_quarantine'} = 'DELETE quarantine FROM quarantine ' . 
 +                               'LEFT JOIN msgs USING(mail_id) '. 
 +                               'WHERE msgs.mail_id IS NULL'; 
 +    $query{'del_msgrcpt'} = 'DELETE msgrcpt FROM msgrcpt ' . 
 +                            'LEFT JOIN msgs USING(mail_id) ' . 
 +                            'WHERE msgs.mail_id IS NULL'; 
 +  } 
 + 
 +  my(%post_query) = ( 
 +                'vacuum_analyze' => 'VACUUM ANALYZE' 
 +                    ); 
 + 
 +  # Order of execution IS IMPORTANT! 
 +  my(@query_order) = qw (del_d_flag del_partial_msg del_msgs_mail_ids 
 +                         del_old_mail_ids del_quarantine del_msgrcpt 
 +                         del_maddr 
 +                        ); 
 + 
 +  @query_order = qw (del_d_flag_new del_partial_msg_new 
 +                         del_old_mail_ids_new del_maddr 
 +                        ) if $new_dd; 
 + 
 +  my(@post_query_order); 
 +  push(@post_query_order, 'vacuum_analyze') if $dbtype eq 'pgsql' && $postop_vacuum; 
 + 
 +  return (\%query,\@query_order,\%post_query,\@post_query_order); 
 +
 + 
 + 
 +sub usage { 
 +  print "Usage:\n"; 
 +  print "\tmz_db_clean.pl [--verbose|-v] [--database|-db <dbtype>]\n"; 
 +  print "\tmz_db_clean.pl --help|-h \n\n"; 
 +  print "\tThe database configuration parameter is REQUIRED!\n\n"; 
 +  print "\tPossible parameters for the \'--database\' option is \'mysql\'\n"
 +        "\tand 'pgsql'.\n"; 
 +  exit; 
 +
 + 
 +sub main { 
 +  Getopt::Long::Configure('no_ignore_case'); 
 +  GetOptions(\%opt, 'help|h', 'database|db=s', 'verbose|v', 
 +            ) or exit(1); 
 +  usage if $opt{help}; 
 +  usage if not $opt{database}; 
 +  my($dbh) = connect_to_sql(@storage_sql_dsn); 
 +  my($query,$query_order,$p_query,$p_query_order) = build_queries($opt{database}); 
 +  my($sth_ref) = prepare_queries($dbh,$query,$p_query); 
 +  my($result) = exec_queries($dbh,$sth_ref,$query,$p_query, 
 +                             $query_order,$p_query_order 
 +                            ); 
 +  print "Database cleanup successful\n" if $result; 
 +  $dbh->disconnect; 
 +
 + 
 +sub exec_queries($$$$$) { 
 +  my($dbh,$sth_ref,$query,$p_query,$query_o,$p_query_o) = @_; 
 +  my($affected); 
 + 
 +  $dbh->begin_work; 
 +  eval { 
 +    foreach (@$query_o) { 
 +      if ($opt{verbose}) { 
 +        print "Executing... " . localtime() . "\n"; 
 +        print $query->{$_} . "\n"; 
 +      } 
 +      $affected = $sth_ref->{$_}->execute or die "Query '$_' did not execute"; 
 +      print "$affected rows affected\n" if $opt{verbose}; 
 +    } 
 +  }; 
 +  if ($@ ne '') { 
 +    $dbh->rollback; 
 +    print "There was an error executing a query! $@\n" . 
 +          "No records modified by database maintenance\n"
 +          "Rollback complete.\n"; 
 +    return undef 
 +  } else { 
 +    $dbh->commit; 
 +  } 
 + 
 +  eval { 
 +    foreach (@$p_query_o) { 
 +      if ($opt{verbose}) { 
 +        print "Executing... " . localtime() . "\n"; 
 +        print $p_query->{$_} . "\n"; 
 +      } 
 +      $affected = $sth_ref->{$_}->execute or die "Query '$_' did not execute"; 
 +      print "$affected rows affected\n" if $opt{verbose}; 
 +    } 
 +  }; 
 +  if ($@ ne '') { 
 +    print "There was an error executing an optional query! $@\n" . 
 +    return undef 
 +  } 
 + 
 +  return 1; 
 + 
 +
 + 
 +sub connect_to_sql(@) { 
 +  my(@sql_dsn) = @_; 
 +  my($dsn, $username, $password) = @sql_dsn; 
 +  print "Connecting to SQL database server\n" if $opt{verbose}; 
 +  print "Trying dsn '$dsn'\n" if $opt{verbose}; 
 +  my($dbh) = DBI->connect($dsn, $username, $password, 
 +#     {PrintError => 1, RaiseError => 0, Taint => 1, AutoCommit => 0} ); 
 +     {PrintError => 1, RaiseError => 0, Taint => 1} ); 
 +  if ($dbh) { 
 +    print "Connection to '$dsn' succeeded\n" if $opt{verbose}; 
 +  } else { 
 +    die "Unable to connect to '$dsn'!\n"; 
 +  } 
 +  $dbh; 
 +}
  
-        The database configuration parameter is REQUIRED!+sub prepare_queries($$$) { 
 +  my($dbh) = shift; 
 +  my($query) = shift; 
 +  my($p_query) = shift; 
 +  my(%sths); 
 +  foreach my $query_set ($query, $p_query) { 
 +    foreach (keys %$query) { 
 +      $sths{$_} = $dbh->prepare($query->{$_}); 
 +    } 
 +  } 
 +  \%sths 
 +}
  
-        Possible parameters for the '--database' option is 'mysql' +main;
-        and 'pgsql'.+
 </code> </code>
  
tachtler/postfix_amavis_mailzu_installieren.txt · Zuletzt geändert: 2013/12/08 10:30 von klaus