Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pfa_maildir_cleanup.pl 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/perl
  2. #
  3. ##
  4. ## pfa_maildir_cleanup.pl
  5. ##
  6. ## (c) 2004 by Stephen Fulton (sfulton@esoteric.ca)
  7. ##
  8. ## based on a script by Petr Znojemsky (thanks!)
  9. ##
  10. ## Simple script to remove maildirs/domains not listed in a MySQL database.
  11. ## Set up for use with those using PostfixAdmin, but can be adapted.
  12. ##
  13. ## Edit the variables between the ##EDIT## to match your setup.
  14. ##
  15. ## USE AT YOUR OWN RISK. I ASSUME NO RESPONSIBILITY.
  16. ##
  17. use DBI;
  18. use File::Path;
  19. ##EDIT##
  20. $root_path = "/home/mail";
  21. $logfile = "/var/log/removed_maildirs.log";
  22. $db_host = "localhost";
  23. $db_database = "database";
  24. $db_user = "username";
  25. $db_password = 'password';
  26. ##END EDIT##
  27. $connectionInfo = "DBI:mysql:database=$db_database;$db_host:3306";
  28. ## Read a list of domain directories in the root path /remote/mail1
  29. opendir(DIRHANDLE, $root_path) || die "Cannot access directory $maildir_path: $!";
  30. my @directories = ();
  31. foreach $directory (sort readdir(DIRHANDLE)) {
  32. push (@directories, $directory);
  33. }
  34. closedir(DIRHANDLE);
  35. ## Strip the "." and ".." from the directories array
  36. ($dot, $doubledot, @directories) = @directories;
  37. ## For each of the domain directories..
  38. foreach $domain_dir (@directories) {
  39. $complete_domain_path = "$root_path/$domain_dir";
  40. ## Get a list of user directories within each domain directory...
  41. opendir(DOMAINHANDLE, $complete_domain_path) || die "Cannot access directory $complete_domain_path: $!";
  42. my @user_directories = ();
  43. foreach $dir (sort readdir(DOMAINHANDLE)) {
  44. push(@user_directories, $dir);
  45. }
  46. close(DOMAINHANDLE);
  47. ## Now remove any "." or ".." directory entries and construct a domain/maildir variable
  48. ## valid for one iteration of loop.
  49. foreach $user_directory (@user_directories) {
  50. if( not($user_directory eq '..') && not($user_directory eq '.') ) {
  51. $short_user_dir = "$domain_dir/$user_directory/";
  52. ## Here is where the $short_user_dir is compared against the DB entry.
  53. $dbh = DBI->connect($connectionInfo,$db_user,$db_password);
  54. $user_query = "SELECT maildir FROM mailbox WHERE maildir = '$short_user_dir'";
  55. $sth = $dbh->prepare($user_query);
  56. $rows = $sth->execute();
  57. ## If there are no rows that match, then directory is orphaned and can
  58. ## be deleted.
  59. if($rows == 0) {
  60. $maildir_path = "$root_path/$short_user_dir";
  61. open(INFO, ">>$logfile") || die "Cannot write to the logfile: $logfile.";
  62. rmtree($maildir_path);
  63. print INFO localtime()." Maildir ".$maildir_path." has been deleted.\n";
  64. (INFO);
  65. }
  66. $sth->finish;
  67. $dbh->disconnect;
  68. }
  69. }
  70. $dbh2 = DBI->connect($connectionInfo,$db_user,$db_password);
  71. $domain_query = "SELECT domain FROM domain WHERE domain = '$domain_dir'";
  72. $sth2 = $dbh2->prepare($domain_query);
  73. $domain_rows = $sth2->execute();
  74. if($domain_rows == 0) {
  75. open(INFO, ">>$logfile") || die "Cannot write to the logfile: $logfile.";
  76. rmtree($complete_domain_path);
  77. print INFO localtime()." Domain directory ".$complete_domain_path." has been deleted.\n";
  78. close(INFO);
  79. }
  80. $sth2->finish;
  81. $dbh2->disconnect;
  82. }