- 论坛徽章:
- 0
|
Dear ALL,
here is my crontab content by user "root"
4 4-23 * * * cd /usr/gm/lsmscript;/usr/gm/lsmscript/lsm.pl -section frequent >;/dev/null 2 >;/dev/null.
lsm.pl
#!/usr/bin/perl
require "readtxt.pl";
require "util.pl";
require "initfieldinfo.pl";
my($i, $sDestination, $sSrcFile, $tmp, $sTmpDir, $sTmpArc, $sTmpStr, $iNumArgv, $sErr, $iNewErr);
$iNewErr = 1;
############################################################################
# To check whether the LSM is already running
if (open(LOG, "</tmp/_lsmmode") {
close (LOG);
print "LSM is already running\n";
# MessLog ($sLogFile, "LSM is already running\n"
exit (1);
}
############################################################################
# The setup file
my $gInFile = "./lsmsetup.inf";
############################################################################
# Read the setup section
@section_info = &OpenInf($gInFile, "setup"
foreach $tmp (@section_info)
{
if ($tmp =~ /^(.*)=(.*)/)
{
#sTmpDir is used to store the tmp destination
#where the file is copy to the LSM.
if ($1 eq "tempdir" {
$sTmpDir = $2;
`chmod -R 777 $sTmpDir`;
`rm -R $sTmpDir*`;
}
elsif ($1 eq "logfile" {
$sLogFile = $2;
}
}
}
############################################################################
# Check whether the log file can be opened
if (not open (LOG, ">;>;$sLogFile") {
print "\nCannot open the log file $sLogFile\n";
exit 1;
}
else {
close(LOG);
}
############################################################################
# To check whether the parameters input is correct
$iNumArgv = @ARGV;
if ($iNumArgv != 2) {
usageError();
}
if ($ARGV[0] eq "-section" {
@section_info = &OpenInf($gInFile, $ARGV[1]);
$i = @section_info;
if ($i eq "0" {
print "Invalid time section has been input\n";
MessLog ($sLogFile, "Invalid time section has been input\n", $iNewErr);
$iNewErr = 0;
exit 1;
}
}
else {
usageError();
Messlog ($sLogFile, "Invalid time section has been input\n"
$iNewErr = 0;
exit 1;
}
############################################################################
# Read the configuration
#@section_info = &OpenInf($gInFile, "files"
foreach $tmp (@section_info)
{
$tmp =~ tr/ //d;
chomp $tmp;
if ($tmp =~ /^(.*)=(.*)/)
{
#$1 store each header of the configuration
#which is used to define the tmp dir of each
#configuration
$i = $1;
#$2 store the configuration
$sTmpStr = $2;
$sTmpStr =~ /^(.*),(.*),(.*),(.*)/g;
#$2 stores the destination path
#$3 stores the owner of the file
#$4 stores the mode of the file
$Src{$i} = $1;
$Dest{$i} = $2;
$Owner{$i} = $3;
$Mode{$i} = $4;
#tmp destination file for the zip arch
$sTmpArc = "$sTmpDir"."$i.tar";
# $1 here is the source file directory
# lsmzip.s is a script that go to the $1 directory and then zip the file
# This script has nothing to print out if its action success.
print "$i\n";
print "Src: $Src{$i} sTMpARc: $sTmpArc\n";
if ($i =~ /^dir*/) {
$sErr = `cd $Src{$i}; tar cvf $sTmpArc *`;
if ($sErr eq "") {
print "Error occur 6\n";
MessLog($sLogFile, "There is error when processing tar cvf $sTmpArc *\n", $iNewErr);
$iNewErr = 0;
#remove all the file in the tmp file
# `chmod -R 777 $sTmpDir`;
# `rm -R $sTmpDir/*`;
# exit 1;
}
}
# elsif ($i =~ /^file*/) {
# print "file wo\n";
# $sErr = `tar cvf $sTmpArc $1`;
# }
}
}
###########################################################################
# unzip the zip file in the temp destination to the real destination
foreach $tmp (sort keys %Dest)
{
#Extract the tar file to a tmp directory
if ($tmp =~ /^dir*/) {
$sTmpArc = "$sTmpDir"."$tmp".".tar";
$sErr = `tar xvf $sTmpArc -C $Dest{$tmp}`;
if ($sErr eq "") {
print "Error occur 1\n";
MessLog ($sLogFile, "There is error when extracting $sTmpArc to $Dest{$tmp}\n", $iNewErr);
$iNewErr = 0;
}
#Change the owner of the files
$sErr = `chown -R $Owner{$tmp} $Dest{$tmp} 2>;&1`;
if (!($sErr eq "")) {
print "Error occur 2\n";
MessLog($sLogFile, "$sErr\n", $iNewErr);
$iNewErr = 0;
}
#Change the mode of the files
$sErr = `chmod -R $Mode{$tmp} $Dest{$tmp} 2>;&1`;
if (!($sErr eq "")) {
print "Error occur 3\n";
MessLog($sLogFile, "$sErr\n", $iNewErr);
$iNewErr = 0;
}
}
elsif ($tmp =~ /^file*/) {
print "cp $Src{$tmp} $Dest{$tmp} 2>;&1 \n";
$sErr = `cp -f $Src{$tmp} $Dest{$tmp} 2>;&1`;
if (!($sErr eq "")) {
print "Error occur 4\n";
MessLog ($sLogFile, "$sErr\n", $iNewErr);
$iNewErr = 0;
}
#Change the owner of the files
$sErr = `chown $Owner{$tmp} $Dest{$tmp} 2>;&1`;
if (!($sErr eq "")) {
print "Error occur 5\n";
MessLog($sLogFile, "$sErr\n", $iNewErr);
$iNewErr = 0;
}
#Change the mode of the files
$sErr = `chmod $Mode{$tmp} $Dest{$tmp} 2>;&1`;
if (!($sErr eq "")) {
print "Error occur 6\n";
MessLog($sLogFile, "$sErr\n", $iNewErr);
$iNewErr = 0;
}
}
else {
MessLog ($sLogFile, "Invalid configuration of the setup file.\n", $iNewErr);
}
}
#Delete all temp files.
`chmod -R 777 $sTmpDir`;
`rm -R $sTmpDir*`;
print "LSM finishes\n";
sub MessLog
{
my ($fFile, $sReturn, $iNew)=@_;
my $data=shift;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdat) = localtime(time);
my $dd = "";
my $ti = "";
$year = substr($year,1,2);
$mon = $mon + 1;
$mon = sprintf("%02d",$mon);
$mday = sprintf("%02d", $mday);
$dd = $year."/".$mon."/".$mday;
$sec = sprintf("%02d", $sec);
$min = sprintf("%02d", $min);
$hour = sprintf("%02d", $hour);
$ti = $hour.":".$min.":".$sec;
if (open(LOG, ">;>;$fFile"))
{
if ($iNew eq "1") {
print LOG "**** [$dd - $ti]:";
print LOG "\n";
}
print LOG "$sReturn\n";
print LOG "\n";
close LOG;
}
else
{
print "The file $tFile cannot be opened\n";
}
}
sub usageError() {
print "\nSyntax error\n\n";
print "Usage: perl lsm.pl -section [time]\n\n";
exit(1);
}
lsmsetup.inf
[frequent]
dir1=/mnt/2nd/u/z01/daily/,/u/z01/daily/,z01:group,755
dir2=/mnt/2nd/u/z01/tty/,/u/z01/tty/,z01:group,755 |
|