CRAP! Lets try that again.
#!/opt/perl5/bin/perl -w
use strict;
use File::Copy;
my $VERSION = 0.2;
my $cTestMode = 0;
my $cVerbose = 0;
my $cContinueOnError = 0;
if ( @ARGV ) {
foreach ( @ARGV ) {
if ( ( $_ eq "-help" ) || ( $_ eq "-h" ) ) {
kHelpMessage();
exit( 0 );
}
if ( ( $_ eq "-test" ) || ( $_ eq "-t" ) ) {
$cTestMode = 1;
next;
}
if ( ( $_ eq "-verbose" ) || ( $_ eq "-v" ) ) {
$cVerbose = 1;
}
if ( ( $_ eq "-continue" ) || ( $_ eq "-c" ) ) {
$cContinueOnError = 1;
}
if ( ( $_ eq "-list" ) || ( $_ eq "-l" ) ) {
kListModules();
}
if ( $_ eq "-shell" ) {
kExecShell();
}
if ( $_ eq "-version" ) {
print( "version $VERSION\n" );
exit( 0 );
}
if ( $_ eq "-exec" ) {
exit( 0 );
}
}
}
# create the suffix for backups...
my @cTimeNow = localtime( time() );
my $cTimeSuffix = "$cTimeNow[ 4 ]$cTimeNow[ 3 ]$cTimeNow[ 5 ]";
my $cDestAPath = "/apath";
my $cDestBPath = "/bpath";
my $cFilesADir = "/adir";
my $cFilesBDir = "/bdir";
my $cRSHUsername = 'rshuser';
my $cRSHHostname = 'rshhost';
my %files = ( "fact_bucket.txt" => "a",
"fact_collab.txt" => "a",
"fact_collab_to_delete.txt" => "a",
"part_number_to_delete.txt" => "a",
"resource_to_delete.txt" => "a",
"site_to_delete.txt" => "a",
"broker_customer_supplier.txt" => "b",
"mailing_list.txt" => "b",
"part_number.txt" => "b",
"broker_supplier.txt" => "b",
"resource.txt" => "b",
"domain.txt" => "b",
"role.txt" => "b",
"broker_upDnMember.txt" => "b",
"broker_upDnMemberFile.txt" => "b",
"broker_upDnMemberFileDm.txt" => "b",
"role_info.txt" => "b",
"org.txt" => "b",
"item_attribute.txt" => "b",
"site.txt" => "b" );
my( $new_name, $copy_source, $copy_dest, $rcp_command, $cFileRemoteCom
+pareCommand, $cFileLocalCompareCommand );
foreach ( keys( %files ) ) {
if ( $files{ $_ } eq "a" ) {
$new_name = "$_.$cTimeNow[4]$cTimeNow[3]$cTimeNow[5]";
$copy_source = "$cFilesADir/$_";
$copy_dest = "$cDestAPath/$new_name";
$rcp_command = "rcp $cFilesADir/$_ $cRSHUsername\@$cRSHHostnam
+e:$cDestAPath/$_";
$cFileRemoteCompareCommand = "rexec $cRSHHostname /usr/bin/cks
+um $cDestAPath/$_";
$cFileLocalCompareCommand = "/usr/bin/cksum $cFilesADir/$_";
} else {
$new_name = "$_.$cTimeNow[4]$cTimeNow[3]$cTimeNow[5]";
$copy_source = "$cFilesBDir/$_";
$copy_dest = "$cDestBPath/$new_name";
$rcp_command = "rcp $cFilesBDir/$_ $cRSHUsername\@$cRSHHostnam
+e:$cDestBPath/$_";
$cFileRemoteCompareCommand = "rexec $cRSHHostname /usr/bin/cks
+um $cDestBPath/$_";
$cFileLocalCompareCommand = "/usr/bin/cksum $cFilesBDir/$_";
}
if ( $cVerbose ) {
print( "MSG: new_name: $new_name\nMSG: copy_source: $copy_sour
+ce\nMSG: copy_dest: $copy_dest\nMSG: rcp_command:\n" );
kPrintArray( $rcp_command );
print( "MSG: RemoteCompare: $cFileRemoteCompareCommand\n" );
}
$cTestMode ? kError( "TESTMODE: would have copied $copy_source to
+$copy_dest" ) : copy( $copy_source, $copy_dest );
if ( $cTestMode ) {
kError( "TESTMODE: did not execute RCP." );
kPrintArray( $rcp_command );
} else {
# __exec( $rcp_command );
my $ret_res=`$rcp_command`;
}
if ( !$cTestMode ) {
# the kludge bunnies begin screaming about here...
# This wouldn't be so bad if I could Net::RSH this or somethin
+g but oh well...
my $cRemoteCompareResult = `$cFileRemoteCompareCommand`;
my $cLocalCompareResult = `$cFileLocalCompareCommand`;
my $z = kSplitCheck( $cRemoteCompareResult );
my $x = kSplitCheck( $cLocalCompareResult );
if ( $z == $x ) {
if ( $cVerbose ) {
print( "copied $_ correctly.\n" );
}
} else {
kError( "MAIN: remote checksum does not match local one.\n
+Local: $x\nRemote: $z\n" );
if ( !$cContinueOnError ) {
exit( 1 );
}
}
} else {
kError( "TESTMODE: not executing chksum comparison." );
}
}
#
# Should I make this return 0/1 or should it have output? That is the
+ question...
#
# for now, it's pretty bad.
sub __exec {
system( shift() );
}
sub kError {
print( "ERROR:", shift(), "\n" );
}
sub kSplitCheck {
my @thing = split( ' ', $_ );
if ( $#thing != 2 ) {
kError( "kSplitCheck: passed value != 2 when split." );
if ( !$cContinueOnError ) {
kError( "kSplitCheck: stopping on error." );
exit( 1 );
}
}
return( $thing[ 0 ] );
}
sub kHelpMessage {
print( "Usage:\nchkfiles [ option ] function\n\n" );
print( "Options:\n\t-v -verbose: Show lots of output.\n" );
print( "\t-h or -help: Show this message.\n" );
print( "\t-t or -test: Just test. Dont actually copy anything.\n"
+ );
print( "\t-c or -continue: Continue despite all errors.\n" );
print( "\t-l or -list: List all runnable scripts.\n\n" );
}
sub kPrintArray {
my @h = shift();
my $m = join( ' ', @h );
print( "$m\n" );
}
It's amazing how many errors you notice when you are about to put your code up for public scrutiny. Hopefully everyone wont get so carried away correcting parts of the code that are inefficient that they forget about my origional problem.
Again, thanks in advance.
-cm
|