#!/usr/bin/perl -w
#-----------------------------------------------------------------------------#
#  Copyright (C) 2001 - 2002 Multi-Tech Systems Inc., USA
#  All Rights Reserved
#  Multi-Tech Systems, Inc., 2205, Woodale Dr, Mounds View MN USA
#-----------------------------------------------------------------------------#
#  ModName: Web GUI - Interface
#  Version: 3.25
#  Date: 2005-10-05
#  Modification History - Begin
#  	20040330 : Javeed 
#  	We need to backup iptable logs seperately.
#  Modification History - End

# Description 	: This script will zip up all the log files everyday

# Rotation of logs will happen every hour from the cron job. At that time, if a
# file reaches the specified size, it will be rotated. Before the file is rotated
# it has to be added to that day's backup tar file. All the rotated log files
# will be added in that day's tar file.
# At the end of the day, there will be a forced logrotate which will forcefully
# rotate all the files, and then zip up the tar file and create a gz file.

$logfilename = $ARGV[0];

# This will be called at the end of the day. 
if ($logfilename eq "finalzip")
{
	while (1)
	{
		$kernelrotate = `ps -eo "%y %p %a" | grep -v grep | grep "/usr/sbin/logrotate /etc/kernelrotate.conf" | tr -s " " | cut -d " " -f2`;
		chomp ($kernelrotate);
                                                                                                                             
		$logrotate = `ps -eo "%y %p %a" | grep -v grep | grep "/usr/sbin/logrotate /etc/logrotate.conf" | tr -s " " | cut -d " " -f2`;
		chomp ($logrotate);
	
		if ( (($kernelrotate eq "") || ($kernelrotate eq " ")) && (($logrotate eq "") || ($logrotate eq " ")))
		{
			last;
		}
		else
		{
			sleep (5);
		}
	}
	
	# Force a logrotate for all files
	system ("/usr/sbin/logrotate -f /etc/logrotate.conf");
	sleep (2);
   	system ("/usr/sbin/logrotate -f /etc/kernelrotate.conf");
 	sleep (2);
	# Special case for squid access log - delete the sargfile used
	system ("rm -f /etc/multiconf/sargfile");

	#chdir ("/var/log/backup");
	#$datestr = `date +%Y-%m-%d`;
	#chomp ($datestr);

	# Form the name for the zip file
	#$tarfilename = $datestr . "\." . "tar";
	#$gzipfilename = $datestr . "\." . "tar" . "\." . "gz";

	# Zip up that day's tar file
	#system ("gzip -f $gzipfilename $tarfilename");
# 20040330 javeed	
	#chdir ("/var/log/backup/iptables");
	#system ("gzip -f $gzipfilename $tarfilename");
	system ("rm -f /var/log/backup/z*");
	system ("rm -f /var/log/backup/iptables/z*");
	exit;
}

# Go to the log directory
chdir ("/");
$datestr = `date +%Y-%m-%d`;
chomp ($datestr);

# Form the name for the zip file
#$zipfilename = $datestr . "\." . "tar";
$zipfilename = $datestr . "\." . "zip";

#The tar file name will be of the format DD-Mon-YYYY.tar
#The log files that go into the tar file will have names <filename>.HH-MM-SS
$timestr = `date +%H_%M`;
chomp ($timestr);

#Get the file name alone
$file = `basename $logfilename`;
chomp ($file);

#Get the directory where the log file is located
@directory = split (/$file/, $logfilename);
$path = $directory[0];

#Create the file name with the timestamp appended
$timelogfile = $logfilename . "-" . $timestr;
$file = `basename $timelogfile`;
chomp ($file);
system ("cp $logfilename $timelogfile");
#javeed
$logfile_onlyname = `basename $logfilename`;
chomp($logfile_onlyname);
# senthil on 20040617
if ($logfile_onlyname eq "iptables_log" ) 
{
	if (-e "/var/log/backup/iptables/$zipfilename")
	{
		chdir("/var/log/backup/iptables");
		system("/etc/multiconf/scripts/checkzipsize $file $zipfilename");
#		system ("cp -f /var/log/backup/iptables/$zipfilename $path");
		#system ("tar -C $path -rvf $zipfilename $file");
		chdir($path);
		system ("zip -ry /var/log/backup/iptables/$zipfilename $file");
	}
	else
	{
		#system ("tar -C $path -cvf $zipfilename $file");
		chdir($path);
		system ("zip -y /var/log/backup/iptables/$zipfilename $file");
	}
#	system ("mv -f $zipfilename /var/log/backup/iptables");
	chdir ("/");
}
else
{
	if (-e "/var/log/backup/$zipfilename")
	{
		chdir("/var/log/backup");
		system("/etc/multiconf/scripts/checkzipsize $file $zipfilename");
#		system ("cp -f /var/log/backup/$zipfilename $path");
		chdir($path);
		#system ("tar -C $path -rvf $zipfilename $file");
		system ("zip -ry /var/log/backup/$zipfilename $file");
	}
	else
	{
		#system ("tar -C $path -cvf $zipfilename $file");
		chdir($path);
		system ("zip -y /var/log/backup/$zipfilename $file");
	}
#	system ("mv -f $zipfilename /var/log/backup/");
	chdir ("/");
}
sleep (1);
system ("rm -f $timelogfile");
#if ($logfile_onlyname eq "iptables_log" ) {
#	system ("mv -f $zipfilename /var/log/backup/iptables");
#} else {
#	system ("mv -f $zipfilename /var/log/backup/");
#}

#At any time, only 4 (Total) = 3 (Backup Logs) + 1 (Currently Updating Log) files can be kept in the backup directory. So, if the
#count exceeds 4, remove the older files.
chdir ("/var/log/backup");
$nooffiles = `ls *.zip | wc -l`;
if ($nooffiles > 4)
{
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
		
	#Sort the files in the backup directory and enter the
	#result into a temporary file
	$files = `ls *.zip -crt`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	print TEMPFILE $files;
	close (TEMPFILE);
	open (TEMPFILE, "/etc/multiconf/scripts/$tempfile");
	$diff = $nooffiles - 4;
	while (<TEMPFILE>)
	{
		system ("rm -f $_");
		$diff--;
		if ($diff == 0)
		{
			last;
		}
	}
	system ("rm -f /etc/multiconf/scripts/$tempfile");
}
#javeed have to handle for iptables logs for 3 days only
chdir ("/var/log/backup/iptables");
$nooffiles = `ls *.zip | wc -l`;
if ($nooffiles > 0)
{
	@files = `ls *.zip -ct`;
#	print ( "@files");	
	for ($i=4; $i < @files; $i++) {
		chomp ($files[$i] );
#		print "removing $files[$i] \n";
		unlink $files[$i];
	}
}
system ("rm -f /var/log/backup/z*");
system ("rm -f /var/log/backup/iptables/z*");

system ("rm -f /var/chroot-squid/usr/local/squid/logs/*.1");
system ("rm -f /var/chroot-squid/usr/local/squid/logs/*.2");

#After taking the backup, remove all the older log files
=niranjan
system ("rm -f /var/log/*.2");
system ("rm -f /var/log/*.3");
system ("rm -f /var/log/*.4");
system ("rm -f /var/log/*.5");
system ("rm -f /usr/local/apache/logs/*.5");
system ("rm -f /usr/local/apache/logs/*.4");
system ("rm -f /usr/local/apache/logs/*.3");
system ("rm -f /usr/local/apache/logs/*.2");
system ("rm -f /var/log/snort/*.2");
system ("rm -f /var/log/snort/*.3");
system ("rm -f /var/log/snort/*.4");
system ("rm -f /var/log/snort/*.5");
system ("rm -f /var/chroot-squid/usr/local/squid/logs/*.5");
system ("rm -f /var/chroot-squid/usr/local/squid/logs/*.4");
system ("rm -f /var/chroot-squid/usr/local/squid/logs/*.3");
system ("rm -f /var/chroot-squid/usr/local/squid/logs/*.2");
=cut
