Link to home
Start Free TrialLog in
Avatar of sdruss
sdruss

asked on

Need Perl Script to Archive Older Files

Our team typically uses Perl in our Solaris Oracle database environment.  We need a Perl script, that will receive parameter, such as, “-keep 30, -keep 60, -keep 90”.

Script called (example):  CleanUpOraDbFiles -keep 60
 
Also, PERL script will move gzip up files older than 60 days, and move to a larger file share of the filesystems. Need good example what this script would look like.  Looking to archive-off ( listener.log, audit logs, alert logs, etc.. )
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

This is untested, but you want something like this.

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
use File::Basename;
use File::Copy;

my $from_dir = '.';
my $to_dir   = '/tmp';
my $cut_off  = 60;

GetOptions(
  'from=s' => \$from_dir,
  'to=s'   => \$to_dir,
  'cut=s'  => \$cut_off,
);

print "Archiving files more than $cut_off days old.\n";
print "Moving from $from_dir to $to_dir.\n";

for (glob "$from_dir/*") {
  next if -C $_ < $cut_off;

  print "$_\n";
  system 'gzip', $_;
  move "$_.gz", "$to_dir/" . basename("$_.gz");
}

Open in new window

Be aware that the listener log is special.  It is always open as long as the listener is running.  You cannot just move the log out from underneath the listener.

It has been quite a while since I did it, but I believe that if you do this;

lsnrctl set log_status off
move/rename listener log
lsnrctl set log_status on

That should create a new log.

As far as I recall, that is the only log that behaves that way.
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Since you mention you have gzip files, you already have a script to zip them, correct?

Why write and maintain your own scripts when Solaris comes with a tool to maintain logs for you?

Check out logadm:
https://docs.oracle.com/cd/E23823_01/html/816-5166/logadm-1m.html

I'm running on RHEL and have a similar tool available called logrotate.  It does everything I need it to do:
Copies off and zips logs and removes logs and trace files after the time I set.
Avatar of sdruss

ASKER

Wow, "logadm"  sounds very interesting.  I will take a look. Thanks!
Avatar of sdruss

ASKER

Dave, looks like a nice script.  I will slightly modify and test tomorrow.  Appreciate, thanks!!
Avatar of sdruss

ASKER

For testing purposes .....  What is a quick way to generate 50 dummy files, in 4 or 5 different directories to test?
Untested...
for dir in d1 d2 d3 d4
do
  for i in {1..40}
  do
    touch $d/file$i
  done
done

Open in new window

Avatar of sdruss

ASKER

Thanks.  Still getting test environment set.
Avatar of sdruss

ASKER

Dave Cross:  Not sure the following is working, and also have not seen this syntax before.  Can you explain?

next if -C $_ < $cut_off;
ASKER CERTIFIED SOLUTION
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial