1 |
ahart |
1.1 |
#!/usr/bin/env perl
|
2 |
|
|
|
3 |
|
|
use strict;
|
4 |
|
|
|
5 |
|
|
sub makeFailedFilesScript;
|
6 |
|
|
sub makeFilesToRemoveScript;
|
7 |
|
|
sub makeFilesToCopyScript;
|
8 |
|
|
|
9 |
|
|
my $src = $ARGV[0];
|
10 |
|
|
my $dest = $ARGV[1];
|
11 |
|
|
$src =~ s/^([^\/]*)\/*$/$1/;
|
12 |
|
|
$dest =~ s/^([^\/]*)\/*$/$1/;
|
13 |
|
|
my @fileList = `find $src -type f`;
|
14 |
|
|
my @filesToRemove;
|
15 |
|
|
my @filesToCopy;
|
16 |
|
|
my @failedFiles;
|
17 |
|
|
foreach my $file (@fileList)
|
18 |
|
|
{
|
19 |
|
|
$file =~ s/^$src\/*(.*)$/$1/;
|
20 |
|
|
$file =~ s/\n//g;
|
21 |
|
|
if (!(-e "$dest/$file"))
|
22 |
|
|
{
|
23 |
|
|
push (@filesToCopy, $file);
|
24 |
|
|
next;
|
25 |
|
|
}
|
26 |
|
|
my $srcFileSize = `ls -l $src/$file`;
|
27 |
|
|
my $destFileSize = `ls -l $dest/$file`;
|
28 |
|
|
$srcFileSize =~ s/^[^ ]* *[^ ]* *[^ ]* *[^ ]* *([^ ]*) *.*$/$1/;
|
29 |
|
|
$destFileSize =~ s/^[^ ]* *[^ ]* *[^ ]* *[^ ]* *([^ ]*) *.*$/$1/;
|
30 |
|
|
$srcFileSize =~ s/\n//g;
|
31 |
|
|
$destFileSize =~ s/\n//g;
|
32 |
|
|
if ($srcFileSize == $destFileSize)
|
33 |
|
|
{
|
34 |
|
|
push (@filesToRemove, $file) if $srcFileSize == $destFileSize;
|
35 |
|
|
}
|
36 |
|
|
else
|
37 |
|
|
{
|
38 |
|
|
print "$src/$file failed to copy:\n";
|
39 |
|
|
print " source size: $srcFileSize, destination size: $destFileSize\n";
|
40 |
|
|
push (@failedFiles, $file);
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
|
|
my $nFilesToRemove = @filesToRemove;
|
44 |
|
|
my $nFilesToCopy = @filesToCopy;
|
45 |
|
|
my $nFailedFiles = @failedFiles;
|
46 |
|
|
my $response;
|
47 |
|
|
print "\n$nFailedFiles files failed verification\n";
|
48 |
|
|
print "$nFilesToRemove files passed verification\n";
|
49 |
|
|
print "$nFilesToCopy files are missing\n\n";
|
50 |
|
|
if ($nFailedFiles > 0)
|
51 |
|
|
{
|
52 |
|
|
print "Create a script to copy failed files? (Y/n): ";
|
53 |
|
|
$response = <STDIN>;
|
54 |
|
|
$response =~ s/\n//g;
|
55 |
|
|
$response = "y" if !$response;
|
56 |
|
|
makeFailedFilesScript (\@failedFiles, $src, $dest) if lc (substr ($response, 0, 1)) eq "y";
|
57 |
|
|
}
|
58 |
|
|
if ($nFilesToRemove > 0 && $nFailedFiles == 0 && $nFilesToCopy == 0)
|
59 |
|
|
{
|
60 |
|
|
print "Create a script to remove passed files? (Y/n): ";
|
61 |
|
|
$response = <STDIN>;
|
62 |
|
|
$response =~ s/\n//g;
|
63 |
|
|
$response = "y" if !$response;
|
64 |
|
|
makeFilesToRemoveScript (\@filesToRemove, $src, $dest) if lc (substr ($response, 0, 1)) eq "y";
|
65 |
|
|
}
|
66 |
|
|
if ($nFilesToCopy > 0 && ($nFilesToRemove != 0 || $nFailedFiles != 0))
|
67 |
|
|
{
|
68 |
|
|
print "Create a script to copy missing files? (Y/n): ";
|
69 |
|
|
$response = <STDIN>;
|
70 |
|
|
$response =~ s/\n//g;
|
71 |
|
|
$response = "y" if !$response;
|
72 |
|
|
makeFilesToCopyScript (\@filesToCopy, $src, $dest) if lc (substr ($response, 0, 1)) eq "y";
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
sub
|
76 |
|
|
makeFailedFilesScript
|
77 |
|
|
{
|
78 |
|
|
my $fileList = shift;
|
79 |
|
|
my $src = shift;
|
80 |
|
|
my $dest = shift;
|
81 |
|
|
|
82 |
|
|
my $postfix;
|
83 |
|
|
do
|
84 |
|
|
{
|
85 |
|
|
$postfix = int (rand (32767));
|
86 |
|
|
}
|
87 |
|
|
while (-e "cpCopyFailed_$postfix.sh");
|
88 |
|
|
open (SCRIPT, ">cpCopyFailed_$postfix.sh");
|
89 |
|
|
print SCRIPT "#!/usr/bin/env bash\n\n";
|
90 |
|
|
foreach my $file (@$fileList)
|
91 |
|
|
{
|
92 |
|
|
my $tmpDest = $dest . "/" . $file;
|
93 |
|
|
my @tmpDest = split ("/", $tmpDest);
|
94 |
|
|
delete $tmpDest[-1];
|
95 |
|
|
$tmpDest = join ("/", @tmpDest);
|
96 |
|
|
print SCRIPT "rm -f $tmpDest/$file\n";
|
97 |
|
|
print SCRIPT "cp $src/$file $tmpDest/\n"
|
98 |
|
|
}
|
99 |
|
|
close (SCRIPT);
|
100 |
|
|
chmod (0755, "cpCopyFailed_$postfix.sh");
|
101 |
|
|
print "Created cpCopyFailed_$postfix.sh\n";
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
sub
|
105 |
|
|
makeFilesToRemoveScript
|
106 |
|
|
{
|
107 |
|
|
my $fileList = shift;
|
108 |
|
|
my $src = shift;
|
109 |
|
|
my $dest = shift;
|
110 |
|
|
|
111 |
|
|
my $postfix;
|
112 |
|
|
do
|
113 |
|
|
{
|
114 |
|
|
$postfix = int (rand (32767));
|
115 |
|
|
}
|
116 |
|
|
while (-e "cpRemove_$postfix.sh");
|
117 |
|
|
open (SCRIPT, ">cpRemove_$postfix.sh");
|
118 |
|
|
print SCRIPT "#!/usr/bin/env bash\n\n";
|
119 |
|
|
foreach my $file (@$fileList)
|
120 |
|
|
{
|
121 |
|
|
print SCRIPT "rm -f $src/$file\n";
|
122 |
|
|
}
|
123 |
|
|
close (SCRIPT);
|
124 |
|
|
chmod (0755, "cpRemove_$postfix.sh");
|
125 |
|
|
print "Created cpRemove_$postfix.sh\n";
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
sub
|
129 |
|
|
makeFilesToCopyScript
|
130 |
|
|
{
|
131 |
|
|
my $fileList = shift;
|
132 |
|
|
my $src = shift;
|
133 |
|
|
my $dest = shift;
|
134 |
|
|
|
135 |
|
|
my $postfix;
|
136 |
|
|
do
|
137 |
|
|
{
|
138 |
|
|
$postfix = int (rand (32767));
|
139 |
|
|
}
|
140 |
|
|
while (-e "cpCopyMissing_$postfix.sh");
|
141 |
|
|
open (SCRIPT, ">cpCopyMissing_$postfix.sh");
|
142 |
|
|
print SCRIPT "#!/usr/bin/env bash\n\n";
|
143 |
|
|
foreach my $file (@$fileList)
|
144 |
|
|
{
|
145 |
|
|
my $tmpDest = $dest . "/" . $file;
|
146 |
|
|
my @tmpDest = split ("/", $tmpDest);
|
147 |
|
|
delete $tmpDest[-1];
|
148 |
|
|
$tmpDest = join ("/", @tmpDest);
|
149 |
|
|
print SCRIPT "cp $src/$file $tmpDest/\n";
|
150 |
|
|
}
|
151 |
|
|
close (SCRIPT);
|
152 |
|
|
chmod (0755, "cpCopyMissing_$postfix.sh");
|
153 |
|
|
print "Created cpCopyMissing_$postfix.sh\n";
|
154 |
|
|
}
|