use strict; use warnings; use File::Glob ':bsd_glob'; ## This module is not included in ActiveState Perl so run this script with d:\portableperl\portableshell.bat. use File::Copy::Recursive qw(rcopy); #local $File::Copy::Recursive::SkipFlop = 1; my $logfile = 'sync_study_to_prod_test.log'; open my $fh_log, ">", $logfile or die "Couldn't open $logfile for output: $!\n"; print $fh_log "Ran at: ", scalar localtime, "\n"; print "Ran at: ", scalar localtime, "\n"; if( not defined $ENV{SERVER_ENVIRONMENT} or not $ENV{SERVER_ENVIRONMENT} ) { print $fh_log "Environment variable \"SERVER_ENVIRONMENT\" is required to be set up. \n"; die "Environment variable \"SERVER_ENVIRONMENT\" is required to be set up. \n"; } while ( ) { chomp; #skip blank lines next if /^\s*$/; my ($source,$destination) = split /\|/; ## trim whitespace at start or end of paths: $source =~ s/^\s+//; $destination =~ s/^\s+//; $source =~ s/\s+$//; $destination =~ s/\s+$//; ## Resolve environment variables $source =~ s/\%(.+?)\%/$ENV{$1}/g; $destination =~ s/\%(.+?)\%/$ENV{$1}/g; ## verify paths if ( not -e $source ) { print $fh_log "s-Warning: $source not found - skipping.\n"; next; } if ( not -e $destination ) { print $fh_log "d-Warning: $destination not found - skipping.\n"; next; } clear_readonly_bit($source); clear_readonly_bit($destination); my ($num_of_files_and_dirs,$num_of_dirs,$depth_traversed) = rcopy ( $source, $destination); if($num_of_files_and_dirs == 0) { print $fh_log "Error: Couldn't copy $source to $destination : $! -\n"; print $fh_log "--$num_of_files_and_dirs,$num_of_dirs,$depth_traversed\n"; next; } print $fh_log "****Copied $source to $destination\n ($num_of_files_and_dirs files and directories, $num_of_dirs directories, depth = $depth_traversed)\n"; } print $fh_log "Finished at: ", scalar localtime, "\n"; sub clear_readonly_bit { my $folder = shift; foreach my $file ( glob( "$folder/*" )) { #print $fh_log "file: $file\n"; ## test if not writable if( not -w $file ){ print $fh_log "$file is read only.\n--Clearing read-only bit...\n"; ## clear read only bit my $cnt = chmod 0777, $file; if( $cnt == 0){ print $fh_log "--Error-Couldn't clear read-only bit.\n"; } } if ( -d $file ) { ## clear ro bits in files in sub folders. clear_readonly_bit($file); } } } __DATA__ D:\Eterra\Distribution\FileUpdaterServer\%SERVER_ENVIRONMENT%\Bitmaps | %IDMS_ROOT%\bitmaps #### Ran at: Tue Jun 12 10:56:21 2018 Error: Couldn't copy D:\Eterra\Distribution\FileUpdaterServer\PROD\Bitmaps to D:\Eterra\Distribution\XXXCorpXXX\bitmaps : - --,, Finished at: Tue Jun 12 10:56:22 2018 #### Ran at: Tue Jun 12 10:51:56 2018 ****Copied D:\Eterra\Distribution\FileUpdaterServer\PROD\Bitmaps to D:\Eterra\Distribution\XXXCorpXXX\bitmaps (1 files and directories, 1 directories, depth = 1) Finished at: Tue Jun 12 10:51:56 2018 #### my $ok_todo_asper_condcopy = sub { my $org = shift; my $copy = 1; if(exists $CondCopy->{$org}) { if($CondCopy->{$org}{'md5'}) { } if($copy) { } } return $copy; }; #### for my $file (@files) { my ($file_ut) = $file =~ m{ (.*) }xms; my $org = File::Spec->catfile($str, $file_ut); my $new = File::Spec->catfile($end, $file_ut); if( -l $org && $CopyLink ) { carp "Copying a symlink ($org) whose target does not exist" if !-e readlink($org) && $BdTrgWrn; symlink readlink($org), $new or return; } elsif(-d $org) { $recurs->($org,$new,$buf) if defined $buf; $recurs->($org,$new) if !defined $buf; $filen++; $dirn++; } else { if($ok_todo_asper_condcopy->($org)) { if($SkipFlop) { fcopy($org,$new,$buf) or next if defined $buf; fcopy($org,$new) or next if !defined $buf; } else { fcopy($org,$new,$buf) or return if defined $buf; fcopy($org,$new) or return if !defined $buf; } chmod scalar((stat($org))[2]), $new if $KeepMode; $filen++; } } }