Subversion Repositories sysadmin_scripts

Rev

Rev 104 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 rodolico 1
#! /usr/bin/perl -w
2
 
3
my $TESTING=1;
4
my $CONTROL_FOLDER = '/opt/mailmanage/data';
5
my $CLEANUP_FILE = 'mail_cleanup.list';
6
my $CONFIG_FILE_NAME = 'mailmanage.cfg';
7
my $TEMP_CLEANUP_TIME = 7;
8
 
9
sub getScriptLocation {
10
   use strict;
11
   use File::Spec::Functions qw(rel2abs);
12
   use File::Basename;
13
   return dirname(rel2abs($0));
14
}
15
 
16
sub readConfig {
17
   my $scriptLocation = &getScriptLocation();
18
   my $return = '';
19
   if ( -e "$scriptLocation/$CONFIG_FILE_NAME" ) {
20
      open CFG,"<$scriptLocation/$CONFIG_FILE_NAME" or die "Could not read $scriptLocation/$CONFIG_FILE_NAME: $!\n";
21
      $return = join ( '', <CFG> );
22
      close CFG;
23
   }
24
   return $return;
25
}
26
 
27
 
28
sub getFoldersToProcess {
29
   my $folderListFile = shift;
30
   my @folders = ();
31
   open DATA, "<$folderListFile" or die "Could not read $folderListFile: $!\n";
32
   @folders = grep { ! /^#/ } <DATA>;
33
   close DATA;
34
   chomp @folders;
35
   return @folders;
36
}
37
 
38
sub cleanFolder {
39
   my $folderToProcess = shift;
40
   my @list;
41
   if ( $TESTING ) {
42
      @list = `find $folderToProcess -type f -mtime +$TEMP_CLEANUP_TIME -print`;
43
   } else {
44
      @list = `find $folderToProcess -type f -mtime +$TEMP_CLEANUP_TIME -print -exec rm \\{\\} \\;`;
45
   }
46
   return scalar( @list );
47
}
48
 
49
 
50
$config = &readConfig();
51
eval( $config );
52
$CLEANUP_FILE = "$CONTROL_FOLDER/$CLEANUP_FILE";
53
 
54
my @foldersToProcess = &getFoldersToProcess( $CLEANUP_FILE );
55
 
56
my $totalDeleted = 0;
57
 
58
print "TEST MODE; no files deleted\n" if $TESTING;
59
print "Cleaning files older than $TEMP_CLEANUP_TIME Days\n";
60
foreach my $folder ( @foldersToProcess ) {
61
   my $deleted = &cleanFolder( $folder );
62
   print "$folder\t$deleted\n";
63
   $totalDeleted += $deleted;
64
 }
65
 
66
print "Deleted $totalDeleted files\n";
67
1;