XMailforum is a readonly knowledge archive now.

Registering as a new user or answering posts is not possible anymore.

Might the force be with you, to find here what you are looking for.

2019-09-20 - hschneider, Admin

Cookie Disclaimer: This forum uses only essential, anonymous session cookies (xmailforum*), nothing to be scared of.

XMail Forum [Powered by Invision Power Board]
Printable Version of Topic
Click here to view this topic in its original format
XMail Forum > XMail Queue Manager > delete old emails, cron job ?


Posted by: cmyk Jul 2 2004, 09:27 AM
hi all,

i searched the forum and google for something like this, but i couldn't find anything.
sorry, if this has been asked before...
i am looking for a script, that could be called by cron.daily and which deletes all emails older than 1 month.

cheers,
phil

Posted by: hschneider Jul 2 2004, 09:52 AM
Untested, but this should work:

CODE

#!/usr/bin/perl
#
# purgespool.pl 1.03
#
# Clears XMail spool folder from frozen messages ...
#
# (c)2003-2004 Harald Schneider
#

#--------------------------------------------------------------
#--- Custom configuration goes here ---------------------------
#--------------------------------------------------------------

$EXP_DAYS = 30; # Purge after x days.
$SPOOLDIR = '/var/MailRoot/spool'; # Spool folder

#--------------------------------------------------------------
#--- End of custom config -------------------------------------
#--------------------------------------------------------------


&dorec([$SPOOLDIR],"procFiles",".*\$");
print "\n-- DONE!\n";
exit(0);

sub dorec {
my $delim = '/';
  local(*paths) = $_[0];
  local($path);

  $fun = $_[1];
  $pattern = $_[2];

  foreach $path (@paths) {

       next if $path =~ m%/\.$%; # Skip current
       next if $path =~ m%/\.\.$%; #  and parent directory

       if (-f $path && ($path =~ /$pattern/o)) {
           &$fun($path);
       } elsif (-d $path) {
           opendir(DIR,$path) || die "Can't open $path - $!";
           local(@files) = readdir(DIR);
           closedir(DIR);
           foreach (@files) { # Add directory part
               $_ = $path . $delim . $_;
           }
           &dorec(*files,$fun,$pattern);
       }
  }
}

sub procFiles() {
my $path = shift;

if ($path =~ /froz/) {
  $tstamp = (stat($path))[9];
  if($tstamp < (time - (60*60*24*$EXP_DAYS)) ) {
    unlink($path);
    print "REMOVED $path\n";
    $path =~ s/\/froz\//\/slog\//;
    unlink($path);
    print "REMOVED $path\n";
  }
}
}                                                                                                                                

Posted by: cmyk Jul 2 2004, 10:22 AM
cool. thanks, harald!

Posted by: hschneider Jul 2 2004, 10:24 AM
Just merged 2 older scripts to this one. Pls give some feedback when tested.

Posted by: cmyk Jul 2 2004, 10:31 AM
umm... actually i was looking for something which deletes old messages, left on the server in the domains directory, not the spool directory. i have a clean-frozen-messages-script.
i am absolutely *not* familiar with perl. :-(

phil

Posted by: hschneider Jul 2 2004, 10:37 AM
I see ... then this one is offtopic here tongue.gif
Try this, but backup before testing:

CODE

#!/usr/bin/perl
#
# purgemaildir.pl 1.00
#
# Clears POP boxes from messages older than x days.
#
# (c)2004 Harald Schneider
#

#--------------------------------------------------------------
#--- Custom configuration goes here ---------------------------
#--------------------------------------------------------------

$EXP_DAYS = 30; # Purge after x days.
$DOMDIR= '/var/MailRoot/domains'; # domains folder

#--------------------------------------------------------------
#--- End of custom config -------------------------------------
#--------------------------------------------------------------


&dorec([$DOMDIR],"procFiles",".*\$");
print "\n-- DONE!\n";
exit(0);

sub dorec {
my $delim = '/';
 local(*paths) = $_[0];
 local($path);

 $fun = $_[1];
 $pattern = $_[2];

 foreach $path (@paths) {

      next if $path =~ m%/\.$%; # Skip current
      next if $path =~ m%/\.\.$%; #  and parent directory

      if (-f $path && ($path =~ /$pattern/o)) {
          &$fun($path);
      } elsif (-d $path) {
          opendir(DIR,$path) || die "Can't open $path - $!";
          local(@files) = readdir(DIR);
          closedir(DIR);
          foreach (@files) { # Add directory part
              $_ = $path . $delim . $_;
          }
          &dorec(*files,$fun,$pattern);
      }
 }
}

sub procFiles() {
my $path = shift;

if ($path =~ /Maildir\/new/) {
 $tstamp = (stat($path))[9];
 if($tstamp < (time - (60*60*24*$EXP_DAYS)) ) {
   unlink($path);
   print "REMOVED $path\n";
 }
}
}                                                                                                                              


Now you have both ... cool.gif

Posted by: hschneider Jul 2 2004, 10:41 AM
Just fixed a small bug:

if ($path =~ /Maildir\/new/) {

instead of

if ($path =~ /MailDir\/new/) {

Posted by: cmyk Jul 2 2004, 11:03 AM
hehe...

CODE
DOMDIR= '/var/MailRoot/domains; # domains folder


missing a "'" ?
but i guess, that line and the comments above was the only thing you changed... :-)

will test and report!

Posted by: hschneider Jul 2 2004, 01:18 PM
Thanks. No, this is not the only line I've changed. Pls copy the whole script.

Posted by: cmyk Jul 2 2004, 01:46 PM
right! :) should have looked more closely.

anyways... it's running great so far!
i've tested it commenting out the unlink line. just to play it save.
it listed the correct messages to be removed. perfect!

harald, thank you!

phil

Posted by: cmyk Jul 2 2004, 02:02 PM
oh yeah... and just to play it safe...

here's the "script" in /etc/cron.daily/

------------------------------------------------------
#!/bin/sh

/usr/sbin/xpurgemaildir.pl
/usr/sbin/xpurgespool.pl
------------------------------------------------------

i guess that's okay?

phil

Posted by: hschneider Jul 2 2004, 03:02 PM
It is. Thx 4 testing.

Posted by: sc4vengr Jul 7 2004, 03:54 PM
Hi guys

CODE

#!/usr/bin/perl
#
# purgemaildir.pl 1.00
#
# Clears POP boxes from messages older than x days.
#
# (c)2004 Harald Schneider
#

#--------------------------------------------------------------
#--- Custom configuration goes here ---------------------------
#--------------------------------------------------------------

$EXP_DAYS = 30; # Purge after x days.
$DOMDIR= '/mailsrv/MailRoot.backup/domains'; # domains folder

#--------------------------------------------------------------
#--- End of custom config -------------------------------------
#--------------------------------------------------------------


&dorec([$DOMDIR],"procFiles",".*\$");
print "\n-- DONE!\n";
exit(0);
                                                        [ Read 64 lines ]

[root@mail logtools]# pico purgeoldmails.pl

  UW PICO(tm) 4.2                                       File: purgeoldmails.pl                                                    

         local(@files) = readdir(DIR);
         closedir(DIR);
         foreach (@files) { # Add directory part
             $_ = $path . $delim . $_;
         }
         &dorec(*files,$fun,$pattern);
     }
}
}

sub procFiles() {
my $path = shift;

if ($path =~ /Maildir\/new/) {
$tstamp = (stat($path))[9];
if($tstamp < (time - (60*60*24*$EXP_DAYS)) ) {
  unlink($path);
  print "REMOVED $path\n";
}
}
}
         


Am i doing something wrong?

CODE

[root@mail logtools]# cd /mailsrv/MailRoot.backup/domains
[root@mail domains]# find . > /mailsrv/logtools/oldtree
[root@mail domains]# cd /mailsrv/logtools/
[root@mail logtools]# ./purgeoldmails.pl

-- DONE!
[root@mail logtools]# cd /mailsrv/MailRoot.backup/domains
[root@mail domains]# find . > /mailsrv/logtools/newtree
[root@mail domains]# cd /mailsrv/logtools/
[root@mail logtools]# ls -ial
total 356
491530 drwxr-xr-x    2 root     root         4096 Jul  7 11:00 .
     2 drwxr-xr-x   18 root     root         4096 Jul  7 10:11 ..
491533 -rw-r--r--    1 root     root       168918 Jul  7 11:00 newtree
491532 -rw-r--r--    1 root     root       168918 Jul  7 10:59 oldtree
491531 -rwxr-xr-x    1 root     root         1502 Jul  7 10:48 purgeoldmails.pl
[root@mail logtools]#


both files are the same size, nothing has been removed?

CODE

[root@mail logtools]# uname -a
Linux hostname 2.4.20-20.8 #1 Mon Aug 18 14:39:22 EDT 2003 i686 athlon i386 GNU/Linux
[root@mail logtools]# perl -v

This is perl, v5.8.0 built for i386-linux-thread-multi
[root@mail logtools]#


Redhat 8.0, Xmail-1.20

thanks in advance

fred


Posted by: hschneider Jul 7 2004, 05:26 PM
Huh? This removes files by date, not by size ...

Posted by: sc4vengr Jul 7 2004, 06:06 PM
Yeah i know Harald, i meant that all the files under domains/ are the same as before running the script. Nothing has been changed nor removed.

This creates a text file with all the files under current dir:
[root@mail domains]# find . > /mailsrv/logtools/oldtree

and then a ran the script and did the same command as above:
[root@mail domains]# find . > /mailsrv/logtools/newtree

and i have compared the two files, they are identical.

fred

Posted by: hschneider Jul 7 2004, 06:11 PM
Maybe there is nothing in your POP boxes, which is older than 30 days?

Posted by: sc4vengr Jul 14 2004, 09:07 PM
Thanks for the help Harald, it was indeed the problem, to make sure everything would go without any problems i copied the MailRoot folder to another location and ran the script to scan in that new location. All the files under the new MailRoot got the current date/time so all the messages werent removed. After extracting a tar.gz backup of my MailRoot dir in a temp folder, i ran the script another time and it worked perfectly, thank you for the great program!

fred

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)