cleanupdirs.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/perl -w
  2. ################################################################################
  3. #
  4. # cleanupdirs 1.2 by jared bell <jared@beol.net>
  5. #
  6. # display/remove maildir & domains directory tree's not listed in the postfix
  7. # mysql database. currently setup for use with postfixadmin, but can be
  8. # adapted. edit settings where it says 'change settings as needed.' by default
  9. # this program will display a list of directories which need deleted, nothing
  10. # is actually deleted. to change this behavior, look into the command line
  11. # arguments.
  12. #
  13. # command line arguments:
  14. # --delete
  15. # force automatic deletion of directories. instead of displaying a list
  16. # of deleted directories, they will be logged in the specified logfile.
  17. # --print
  18. # display deleted directories as well as log them. only valid when
  19. # '--delete' has been specified.
  20. #
  21. # settings:
  22. # $root_path = "/home/vmail";
  23. # if maildir is '/home/vmail/domain.tld/user' then '/home/vmail' is the
  24. # $root_path. if your maildirs are '/home/vmail/user@domain.tld' then
  25. # this program will need to be modified in order to work right.
  26. # $logfile = "/var/log/removed_maildirs.log";
  27. # the logfile to use when $delete_old_dirs is set to 1
  28. # $db_* = "*";
  29. # sets the host, port, database, user and pass to your mysql server
  30. #
  31. # version history:
  32. # 1.2 - removed uneeded settings. added '--print' command line argument
  33. # 1.1 - added '--delete' command line argument
  34. # 1.0 - initial release
  35. #
  36. ################################################################################
  37. use strict;
  38. use DBI;
  39. use File::Path;
  40. use Getopt::Long;
  41. ### change settings as needed, see notes above #################################
  42. our $root_path = "/home/vmail";
  43. our $logfile = "/var/log/removed_maildirs.log";
  44. our $db_hostname = "localhost";
  45. our $db_port = "3306"; # this script currently supports MySQL only
  46. our $db_database = "postfix";
  47. our $db_username = "someuser";
  48. our $db_password = "somepass";
  49. # instead of changing this script, you can put your settings to /etc/mail/postfixadmin/cleanupdirs.conf
  50. # just use perl syntax there to fill the variables listed above (without the "our" keyword). Example:
  51. # $db_username = 'mail';
  52. if (-f "/etc/mail/postfixadmin/cleanupdirs.conf") {
  53. require "/etc/mail/postfixadmin/cleanupdirs.conf";
  54. }
  55. ################################################################################
  56. ### begin program ##############################################################
  57. my(@dirs_to_delete, $logfile_open);
  58. my $delete_old_dirs = 0; # do not delete by default, use cmdline to change this
  59. my $print_also = 0; # also print items when deleting, use cmdline to change this
  60. GetOptions ('delete' => \$delete_old_dirs, 'print' => \$print_also);
  61. my $conn_info = "DBI:mysql:database=$db_database;hostname=$db_hostname;port=$db_port";
  62. my $dbh = DBI->connect($conn_info, $db_username, $db_password)
  63. or die $DBI::errstr;
  64. opendir DOMAINDIR, $root_path
  65. or die "Unable to access directory '$root_path' ($!)";
  66. foreach my $domain_dir (sort readdir DOMAINDIR) {
  67. next if $domain_dir =~ /^\./; # skip dotted dirs
  68. next if (! -d "$root_path/$domain_dir"); # skip everything that is not a directory
  69. my $full_domain_dir = "$root_path/$domain_dir";
  70. opendir USERDIR, $full_domain_dir
  71. or die "Unable to access directory '$full_domain_dir' ($!)";
  72. foreach my $user_dir (sort readdir USERDIR) {
  73. next if $user_dir =~ /^\./; # skip dotted dirs
  74. push @dirs_to_delete, "$full_domain_dir/$user_dir"
  75. if &check_dir("SELECT maildir FROM mailbox WHERE maildir = ?",
  76. "$domain_dir/$user_dir/"); # end slash needed for checkdir
  77. }
  78. push @dirs_to_delete, $full_domain_dir
  79. if &check_dir("SELECT domain FROM domain WHERE domain = ?", $domain_dir);
  80. }
  81. closedir USERDIR;
  82. closedir DOMAINDIR;
  83. $dbh->disconnect;
  84. if (@dirs_to_delete) {
  85. foreach my $to_delete (@dirs_to_delete) {
  86. if ($delete_old_dirs == 1) {
  87. $logfile_open = open LOGFILE, ">> $logfile"
  88. or die "Unable to append logfile '$logfile' ($!)"
  89. unless $logfile_open;
  90. rmtree $to_delete;
  91. print LOGFILE localtime() . " Deleting directory '$to_delete'\n";
  92. print localtime() . " Deleting directory '$to_delete'\n"
  93. if $print_also;
  94. } else {
  95. print localtime() . " Need to delete directory '$to_delete'\n";
  96. }
  97. }
  98. }
  99. close LOGFILE if $logfile_open;
  100. sub check_dir {
  101. my($query, $dir) = @_;
  102. my $sth = $dbh->prepare($query);
  103. my $num_rows = $sth->execute($dir);
  104. $sth->finish;
  105. ($num_rows eq "0E0") ? 1 : 0;
  106. }