Brothers,
During my time as a system admin I have found Perl to be a most invaluble tool for all kinds of tasks. One of the best examples of this is trying to clean up old archive logs files from an Oracle database.
Archive log files are created from redo logs; however unlike redo logs archive logs are *not* cyclical. By this I mean that the redo logs will operate in a round robin mode, where the oldest is overwritten with new information. Archive logs, by contrast, keep on growing and can full up disk space if they are allowed to go unmonitored.
Because of this I have a little script below which will examine the file times of files in a directory and delete the files that are oldest (providing that there are more than 5 files in the directory).
I hope some other sys admins may find this useful.
#!/usr/bin/perl
use strict;
my ($dirname, $filecounter, $decision, $filename, $file, $WRITETIME);
my (%list);
$dirname = "/u01/oracle/admin/oraprod/arch//"; # where the ar
+chived files lives
$filecounter = 0; # just a counter really
$decision = 5; # how many files to keep
$filename = "";
# call routines
&ReadDir; # reads in directory
&WriteTime; # write the time of the last command out
if ($filecounter > $decision) {
&GetStat; # finds out filetimes/deletes files
}
# sub WriteTime
sub WriteTime {
system("echo last file run was at `date` >> /tmp/delete");
}
#sub ReadDir
sub ReadDir {
opendir(DIRHANDLE, $dirname) || die "Can't find or open directory $d
+irname";
while (defined($filename = readdir(DIRHANDLE))) {
if (!-d $filename) { # ignore . and ..
$filecounter++; # increment the counter
}
}
closedir(DIRHANDLE);
}
sub GetStat {
%list = (); # create a hash for the elements
$filecounter = 0; # create a variable to hold the counter
# open the directory and get all the VALID files out
opendir(DIRHANDLE, $dirname) || die "Can't find or open directory $d
+irname";
while (defined($filename = readdir(DIRHANDLE))) {
if (!-d $filename) { # ignore . and ..
($WRITETIME) = (stat($filename)) [9];
$list{$filename} = "$WRITETIME"; # make hash for filena
+mes and time
}
}
closedir(DIRHANDLE);
# sort the hash by the associated values descending
foreach $file (sort { $list{$b} <=> $list{$a} } keys %list ) {
$filecounter++;
if ($filecounter > $decision) {
system("echo 'deleted file' >& /tmp/delete");
unlink("$dirname/$file");
}
}