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 -> delete old emails, cron job ?

Reply to this topicStart new topicStart Poll

> delete old emails, cron job ?
cmyk
Posted: Jul 2 2004, 09:27 AM
Quote Post


Member
***

Group: Members
Posts: 76
Member No.: 462
Joined: 13-February 03



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
PMEmail Poster
Top
hschneider
Posted: Jul 2 2004, 09:52 AM
Quote Post


No - I'm not an answering script ...
Group Icon

Group: Admin
Posts: 6631
Member No.: 195
Joined: 19-June 02



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";
  }
}
}                                                                                                                                


--------------------
Bye,
Harald


-- Download XMail Queue Manager 1.46 NOW: XMail Server Tools
-- Cross platform remote queue management!
-- Message analyzing on the fly!
-- Builtin diagnostics knowledge base!
-- Manages multiple mail queues!

Sponsored by
CD-Produktion und DVD-Produktion and Homestaging Saarland - Immobilien schneller verkaufen in der Region Saarland, Rheinland-Pfalz und Luxembourg
PMEmail PosterUsers Website
Top
cmyk
Posted: Jul 2 2004, 10:22 AM
Quote Post


Member
***

Group: Members
Posts: 76
Member No.: 462
Joined: 13-February 03



cool. thanks, harald!
PMEmail Poster
Top
hschneider
Posted: Jul 2 2004, 10:24 AM
Quote Post


No - I'm not an answering script ...
Group Icon

Group: Admin
Posts: 6631
Member No.: 195
Joined: 19-June 02



Just merged 2 older scripts to this one. Pls give some feedback when tested.


--------------------
Bye,
Harald


-- Download XMail Queue Manager 1.46 NOW: XMail Server Tools
-- Cross platform remote queue management!
-- Message analyzing on the fly!
-- Builtin diagnostics knowledge base!
-- Manages multiple mail queues!

Sponsored by
CD-Produktion und DVD-Produktion and Homestaging Saarland - Immobilien schneller verkaufen in der Region Saarland, Rheinland-Pfalz und Luxembourg
PMEmail PosterUsers Website
Top
cmyk
Posted: Jul 2 2004, 10:31 AM
Quote Post


Member
***

Group: Members
Posts: 76
Member No.: 462
Joined: 13-February 03



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
PMEmail Poster
Top
hschneider
Posted: Jul 2 2004, 10:37 AM
Quote Post


No - I'm not an answering script ...
Group Icon

Group: Admin
Posts: 6631
Member No.: 195
Joined: 19-June 02



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


--------------------
Bye,
Harald


-- Download XMail Queue Manager 1.46 NOW: XMail Server Tools
-- Cross platform remote queue management!
-- Message analyzing on the fly!
-- Builtin diagnostics knowledge base!
-- Manages multiple mail queues!

Sponsored by
CD-Produktion und DVD-Produktion and Homestaging Saarland - Immobilien schneller verkaufen in der Region Saarland, Rheinland-Pfalz und Luxembourg
PMEmail PosterUsers Website
Top
hschneider
Posted: Jul 2 2004, 10:41 AM
Quote Post


No - I'm not an answering script ...
Group Icon

Group: Admin
Posts: 6631
Member No.: 195
Joined: 19-June 02



Just fixed a small bug:

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

instead of

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


--------------------
Bye,
Harald


-- Download XMail Queue Manager 1.46 NOW: XMail Server Tools
-- Cross platform remote queue management!
-- Message analyzing on the fly!
-- Builtin diagnostics knowledge base!
-- Manages multiple mail queues!

Sponsored by
CD-Produktion und DVD-Produktion and Homestaging Saarland - Immobilien schneller verkaufen in der Region Saarland, Rheinland-Pfalz und Luxembourg
PMEmail PosterUsers Website
Top
cmyk
Posted: Jul 2 2004, 11:03 AM
Quote Post


Member
***

Group: Members
Posts: 76
Member No.: 462
Joined: 13-February 03



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!
PMEmail Poster
Top
hschneider
Posted: Jul 2 2004, 01:18 PM
Quote Post


No - I'm not an answering script ...
Group Icon

Group: Admin
Posts: 6631
Member No.: 195
Joined: 19-June 02



Thanks. No, this is not the only line I've changed. Pls copy the whole script.


--------------------
Bye,
Harald


-- Download XMail Queue Manager 1.46 NOW: XMail Server Tools
-- Cross platform remote queue management!
-- Message analyzing on the fly!
-- Builtin diagnostics knowledge base!
-- Manages multiple mail queues!

Sponsored by
CD-Produktion und DVD-Produktion and Homestaging Saarland - Immobilien schneller verkaufen in der Region Saarland, Rheinland-Pfalz und Luxembourg
PMEmail PosterUsers Website
Top
cmyk
Posted: Jul 2 2004, 01:46 PM
Quote Post


Member
***

Group: Members
Posts: 76
Member No.: 462
Joined: 13-February 03



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
PMEmail Poster
Top
cmyk
Posted: Jul 2 2004, 02:02 PM
Quote Post


Member
***

Group: Members
Posts: 76
Member No.: 462
Joined: 13-February 03



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
PMEmail Poster
Top
hschneider
Posted: Jul 2 2004, 03:02 PM
Quote Post


No - I'm not an answering script ...
Group Icon

Group: Admin
Posts: 6631
Member No.: 195
Joined: 19-June 02



It is. Thx 4 testing.


--------------------
Bye,
Harald


-- Download XMail Queue Manager 1.46 NOW: XMail Server Tools
-- Cross platform remote queue management!
-- Message analyzing on the fly!
-- Builtin diagnostics knowledge base!
-- Manages multiple mail queues!

Sponsored by
CD-Produktion und DVD-Produktion and Homestaging Saarland - Immobilien schneller verkaufen in der Region Saarland, Rheinland-Pfalz und Luxembourg
PMEmail PosterUsers Website
Top
sc4vengr
Posted: Jul 7 2004, 03:54 PM
Quote Post


Professional
*****

Group: Members
Posts: 206
Member No.: 461
Joined: 12-February 03



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



--------------------
You + webcam - clothes - dignity = $
PMEmail Poster
Top
hschneider
Posted: Jul 7 2004, 05:26 PM
Quote Post


No - I'm not an answering script ...
Group Icon

Group: Admin
Posts: 6631
Member No.: 195
Joined: 19-June 02



Huh? This removes files by date, not by size ...


--------------------
Bye,
Harald


-- Download XMail Queue Manager 1.46 NOW: XMail Server Tools
-- Cross platform remote queue management!
-- Message analyzing on the fly!
-- Builtin diagnostics knowledge base!
-- Manages multiple mail queues!

Sponsored by
CD-Produktion und DVD-Produktion and Homestaging Saarland - Immobilien schneller verkaufen in der Region Saarland, Rheinland-Pfalz und Luxembourg
PMEmail PosterUsers Website
Top
sc4vengr
Posted: Jul 7 2004, 06:06 PM
Quote Post


Professional
*****

Group: Members
Posts: 206
Member No.: 461
Joined: 12-February 03



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


--------------------
You + webcam - clothes - dignity = $
PMEmail Poster
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | XMail Queue Manager | Next Newest »

Reply to this topicStart new topicStart Poll