Skip to content

Instantly share code, notes, and snippets.

@mshock
Created May 15, 2012 16:34
Show Gist options
  • Save mshock/2703101 to your computer and use it in GitHub Desktop.
Save mshock/2703101 to your computer and use it in GitHub Desktop.
generate remote copy batch files
#! /usr/bin/perl -w
# chop up list of all files into several batch scripts for xfers
$remote_dir = $ARGV[1];
$local_temp = $ARGV[3];
$local_target = $ARGV[2];
if(scalar(@ARGV)){
if (scalar(@ARGV) == 4 && $ARGV[0] =~ m/\d+/) {
$num_batches = $ARGV[0];
$remote_dir = $ARGV[1];
$local_temp = $ARGV[3];
$local_target = $ARGV[2];
}
else {
print "usage: perl CopyFiles.pl [batches] remote_dir local_dir temp_dir\n";
exit;
}
}
elsif (scalar(@ARGV) == 3) {
$num_batches = 4;
$remote_dir = $ARGV[0];
$local_temp = $ARGV[2];
$local_target = $ARGV[1];
}
open(LOG, '<',"log.txt");
$table_match = 'MarketQA';
# collect all table names
while(<LOG>) {
if (m/$table_match/) {
chomp;
push @tables, (split(/\\/))[2];
}
}
close LOG;
$total_tables = scalar(@tables);
$row_lim = int($total_tables / $num_batches + .99);
print "found $total_tables tables\n";
print "splitting into $num_batches batches of approximately $row_lim\n";
$batchnum = 0;
while ($batchnum++ < $num_batches) {
print "creating batch $batchnum\n";
open(FETCH, '>', "Mapping_Fetch" . $batchnum . ".bat");
open(COPY, '>', "Mapping_Copy" . $batchnum . ".bat");
$written = 0;
$base_index = ($batchnum - 1) * $row_lim;
do {
if (!$tables[$base_index + $written]) {
last;
}
$table = $tables[$base_index + $written];
print "\t$table [" . ($base_index + $written) . "]\n";
print FETCH "Copy $remote_dir$table.dat $local_temp$table.dat\n";
print COPY "Copy $local_temp$table.dat $local_target$table.dat\n";
$written++;
} while ($written % $row_lim);
close FETCH;
close COPY;
}
open BC,'>', "Start_Mapping_Copy.bat";
open BF,'>', "Start_Mapping_Fetch.bat";
for ($curr = 1; $curr <= $batchnum; $curr++) {
print BC "start \"MapCopy$curr\" Mapping_Copy$curr.bat\n";
print BF "start \"MapFetch$curr\" Mapping_Fetch$curr.bat\n";
}
close BC;
close BF;
@mshock
Copy link
Author

mshock commented May 15, 2012

Divides up a list of (table) files into a specified number of batch files. Two sets of batch scripts are created, one for copying from remote a temp directory, and one for copying from the temp directory to the local production target directory. Then generates 2 batch files which will launch all their corresponding children. Should probably convert to use robocopy at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment