#!/opt/perl/bin/perl use Cwd; use File::Basename; use Time::Local; use HTTP::Date; use File::Copy; my $prevdir = cwd(); my $archive = "/archive"; my @logfiles = qw (/var/adm/syslog/sys.log /var/adm/syslog/net.log); my $gzipcmd = "/sbin/tar cvf - <##file##> | /usr/local/bin/gzip > <##file##>.tar.gz"; my @Months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); chdir($archive); open(LOG, ">>logrotate.log") || die "Could not open file \"logrotate.log\" : $!\n"; archivefile(); close(LOG); chdir($prevdir); sub archivefile () { my ($now, $mynow, $mydate, $mymonth, $myyear, $mymonthyear, $mytoday); my ($mycmd, $mytemp, $mytemp1, $mytemp2); @bkfiles=(); #$now = time; $now = time - (60*60*12); $mynow = localtime($now); $mydate = (localtime($now))[3]; $mymonth = (localtime($now))[4] + 1; $myyear = (localtime($now))[5] + 1900; $mymonthyear = $Months[$mymonth-1] . $myyear; ($mydate, $mymonth) = corrit($mydate, $mymonth); $mytoday = $mydate . $mymonth . $myyear; #print "$mytoday\n"; if (-d $mymonthyear) { cd($mymonthyear); #print "got dir\n"; } else { mkdir($mymonthyear); cd($mymonthyear); #print "no dir\n"; } printf LOG "%s : Stopping syslog to backup files.\n", gettime(); #print "Syslog stop\n"; system "/sbin/init.d/syslogd stop"; foreach $file (@logfiles) { $mytemp = $file . "\.bak"; #print "my file : $file and my bkfile : $mytemp\n"; copy ($file, $mytemp); if ( $file !=~ /(syslog.log)/) { #print "unlink $file\n"; unlink $file; #system "/usr/bin/touch $file"; } $mytemp=""; } #print "Syslog start\n"; system "/sbin/init.d/syslogd start"; printf LOG "%s : Re-starting syslog server.\n", gettime(); printf LOG "%s : Starting to archive files.\n", gettime(); opendir (DIR, "/var/adm/syslog") || die "Cant't open directory . $! \n"; @bkfiles = grep (/.bak/, readdir (DIR)); closedir (@bkfiles); foreach $bkfile (@bkfiles) { $mytemp = basename($bkfile, ".bak"); $mytemp = $mytemp . "\." . $mytoday; $mytemp1 = $archive . "\/" . $mymonthyear . "\/" . $mytemp; $mytemp2 = "/var/adm/syslog/" . $bkfile; print "my source : $bkfile, my dest file : $mytemp1\n"; copy ($mytemp2, $mytemp1); $mycmd = $gzipcmd; $mycmd =~ s/<##file##>/$mytemp1/; $mycmd =~ s/<##file##>/$mytemp1/; print "$mycmd\n"; system $mycmd; unlink $mytemp1; $mytemp=""; $mytemp1=""; $mytemp2=""; } printf LOG "%s : Archiving completed.\n", gettime(); } sub gettime { my ($now, $mynow); $now = time; $mynow = localtime($now); return ($mynow); } sub trimit { my ($mychar, @mydata) = @_; my ($myitem, $myitem1); my (@myresults, @mytemp)=(); foreach $myitem (@mydata) { #@mytemp = split(/($mychar)/, $myitem); #print "$myitem\n"; chomp($myitem); for $myitem1 (0 .. 20) { $myitem =~ s/$mychar{1,30}/\|/; } #print "$myitem\n"; push (@myresults, $myitem); } return (@myresults); } sub corrit { my ($mydate, $mymonth) = @_; my ($temp1, $temp2); if ($mydate < 10) { $temp1 = "0" . $mydate; } else { $temp1 = $mydate; } if ($mymonth < 10) { $temp2 = "0" . $mymonth; } else { $temp2 = $mymonth; } return ($temp1, $temp2); } sub cd { my ($dir) = @_; my @tmp; if ($dir eq "..") { @tmp = split(/\\/, $curdir); pop(@tmp); $curdir = join("\\", @tmp); chdir("..") || print "Could not change to $curdir\n"; } else { @tmp = split(/\\/, $curdir); push(@tmp, $dir); $curdir = join("\\", @tmp); chdir($curdir) || print "Could not change to $curdir\n"; } } 1;