Subversion Repositories sysadmin_scripts

Rev

Rev 104 | Details | Compare with Previous | Last modification | View Log | RSS feed

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