-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRestore_Script.pl
1617 lines (1450 loc) · 53.3 KB
/
Restore_Script.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
########################################################################
#Script Name : Restore_Script.pl
########################################################################
use lib map{if(__FILE__ =~ /\//) { substr(__FILE__, 0, rindex(__FILE__, '/'))."/$_";} else { "./$_"; }} qw(Idrivelib/lib);
$incPos = rindex(__FILE__, '/');
$incLoc = ($incPos>=0)?substr(__FILE__, 0, $incPos): '.';
unshift (@INC,$incLoc);
use FileHandle;
use Sys::Hostname;
use POSIX;
use Helpers;
Helpers::initiateMigrate();
require 'Header.pl';
use constant false => 0;
use constant true => 1;
#use Constants 'CONST';
require Constants;
# use of constants
use constant CHILD_PROCESS_STARTED => 1;
use constant CHILD_PROCESS_COMPLETED => 2;
use constant LIMIT => 2*1024;
use constant FILE_MAX_COUNT => 1000;
use constant RELATIVE => "--relative";
use constant NORELATIVE => "--no-relative";
# use constant SEARCH => "Search";
use constant SPLIT_LIMIT_SEARCH_OUTPUT => 6;
use constant SPLIT_LIMIT_ITEMS_OUTPUT => 2;
use constant SPLIT_LIMIT_INFO_LINE => 3;
use constant RESTORE_PID_FAIL => 5;
use constant OUTPUT_PID_FAIL => 6;
use constant PID_NOT_EXIST => 7;
use constant RESTORE_SUCCESS => 8;
use constant REMOTE_SEARCH_FAIL => 12;
use constant REMOTE_SEARCH_CMD_ERROR => 13;
use constant REMOTE_SEARCH_OUTPUT_PARSE_FAIL => 14;
use constant REMOTE_SEARCH_SUCCESS => 15;
use constant CREATE_THOUSANDS_FILES_SET_SUCCESS => 16;
use constant REMOTE_SEARCH_THOUSANDS_FILES_SET_ERROR => 17;
# Index number for arrayParametersStatusFile
use constant COUNT_FILES_INDEX => 0;
use constant SYNC_COUNT_FILES_INDEX => 1;
use constant ERROR_COUNT_FILES => 2;
use constant FAILEDFILES_LISTIDX => 3;
use constant RETRY_ATTEMPT_INDEX => 4;
#use constant ERR_MSG_INDEX => 4;
use constant EXIT_FLAG_INDEX => 4;
my @commandArgs = ('--silent', 'dashboard', Constants->CONST->{'versionRestore'},'SCHEDULED');
if ($#ARGV >= 0){
if (!validateCommandArgs(\@ARGV,\@commandArgs)){
print Constants->CONST->{'InvalidCmdArg'}.$lineFeed;
cancelProcess();
}
}
# Status File Parameters
my @statusFileArray = ( "COUNT_FILES_INDEX",
"SYNC_COUNT_FILES_INDEX",
"ERROR_COUNT_FILES",
"FAILEDFILES_LISTIDX",
"RETRY_ATTEMPT_INDEX",
"EXIT_FLAG"
#"ERR_MSG_INDEX"
);
#Indicates whether child process#
#has started/completed #
my $childProcessStatus : shared;
$childProcessStatus = undef;
#Check if EVS Binary exists.
my $silentFlag = 0;
if ($ARGV[0] eq '--silent' or ${ARGV[0]} eq 'dashboard'){
$silentFlag = 1;
}
$confFilePath = $usrProfilePath."/$userName/".Constants->CONST->{'configurationFile'};
loadUserData(); # $restoreHost variable is not getting populated
if ($silentFlag == 0 and $ARGV[0] != Constants->CONST->{'versionRestore'} and $ARGV[0] ne 'SCHEDULED'){ #To prevent the calling of headerDisplay() subroutine.
headerDisplay($0);
}
my $errorFilePresent = false;
my $invalidCharPresent = false;
my $lineCount;
my $prevLineCount;
my $cancelFlag = false;
my $headerWrite = 0;
my $restoreUtfFile = '';
my $generateFilesPid = undef;
my $displayProgressBarPid = undef;
my $prevTime = time();
my $pidOperationFlag = "main";
my $countErrorFile = 0; #Count of files which could not be restored due to specified errors #
my $maxNumRetryAttempts = 1000; #Maximum number of times the script should try to restore in case of errors#
my $filesonlycount = 0;
my $prevFailedCount = 0;
my $totalSize = 0;
my $relative = 0;
my $noRelIndex = 0;
my $exitStatus = 0;
my $retrycount = 0;
#my $RestoreItemCheck = $jobRunningDir."/"."RestoresetFile.txt.item";
my $RestoresetFile_new = '';
my $RestoresetFile_relative = "RestoreFileName_Rel";
my $filesOnly = "RestoreFileName_filesOnly";
my $noRelativeFileset = "RestoreFileName_NoRel";
our $exit_flag = 0;
$jobType = "Restore";
my $retry_failedfiles_index = 0;
my $engineID = 1;
my @RestoreForkchilds;
#Subroutine that processes SIGINT, SIGTERM and SIGTSTP#
#signal received by the script during restore#
$SIG{INT} = \&process_term;
$SIG{TERM} = \&process_term;
$SIG{TSTP} = \&process_term;
$SIG{QUIT} = \&process_term;
$SIG{PWR} = \&process_term;
$SIG{KILL} = \&process_term;
$SIG{USR1} = \&process_term;
#Assigning Perl path
my $perlPath = `which perl`;
chomp($perlPath);
if($perlPath eq ''){
$perlPath = '/usr/local/bin/perl';
}
my $RestoreFileName = $RestoresetFile;
chmod $filePermission, $RestoreFileName;
# Trace Log Entry #
my $curFile = basename(__FILE__);
#Verifying if Restore scheduled or manual job
my $isScheduledJob = 0;
if($ARGV[0] eq "SCHEDULED") {
$pwdPath = $pwdPath."_SCH";
$pvtPath = $pvtPath."_SCH";
$isScheduledJob = 1;
$taskType = "Scheduled";
}else{
$taskType = "Manual";
if(!defined(${ARGV[0]}) or ${ARGV[0]} ne 'dashboard'){
if(getAccountConfStatus($confFilePath)){
Helpers::sendFailureNotice($userName,'update_restore_progress',$taskType);
exit(0);
}
else{
if(getLoginStatus($pwdPath)){
Helpers::sendFailureNotice($userName,'update_restore_progress',$taskType);
exit(0);
}
}
}
# $CurrentRestoresetSoftPath = $RestoresetFileSoftPath;
}
if(${ARGV[0]} eq '--silent') {
$Configuration::displayHeader = 0;
Helpers::isLoggedin() or Helpers::retreat(["\n", 'login_&_try_again']);
}
if (! checkIfEvsWorking($dedup)){
print Constants->CONST->{'EvsProblem'}.$lineFeed if($taskType eq "Manual");
Helpers::traceLog(Constants->CONST->{'EvsProblem'});
Helpers::sendFailureNotice($userName,'update_restore_progress',$taskType);
exit 0;
}
# traceLog(qq(File: $curFile));
#Defining and creating working directory
$jobRunningDir = "$usrProfilePath/$userName/Restore/DefaultRestoreSet";
$Configuration::jobRunningDir = $jobRunningDir; # Added by Senthil on Nov 26, 2018
if(!-d $jobRunningDir) {
mkpath($jobRunningDir);
chmod $filePermission, $jobRunningDir;
}
exit 1 if(!checkEvsStatus(Constants->CONST->{'RestoreOp'}));
$pidPath = "$jobRunningDir/pid.txt";
#Checking if another job in progress
if(!pidAliveCheck()){
$pidMsg = "$jobType job is already in progress. Please try again later.\n";
print $pidMsg if($taskType eq "Manual");
Helpers::traceLog($pidMsg);
exit 1;
}
#Loading global variables
my $RestoreItemCheck = $jobRunningDir."/"."RestoresetFile.txt.item"; #"RestoresetFile.txt.item";
$statusFilePath = "$jobRunningDir/STATUS_FILE";
$search = "$jobRunningDir/Search";
my $info_file = "$jobRunningDir/info_file";
$retryinfo = "$jobRunningDir/$retryinfo";
$evsTempDirPath = "$jobRunningDir/evs_temp";
$evsTempDir = $evsTempDirPath;
my $failedfiles = $versionRestoresetFile."/".$failedFileName;
$idevsOutputFile = "$jobRunningDir/output.txt";
$idevsErrorFile = "$jobRunningDir/error.txt";
$RestoresetFile_relative = $jobRunningDir."/".$RestoresetFile_relative;
$noRelativeFileset = $jobRunningDir."/".$noRelativeFileset;
$filesOnly = $jobRunningDir."/".$filesOnly;
my $fileForSize = "$jobRunningDir/TotalSizeFile";
#my $incSize = "$jobRunningDir/transferredFileSize.txt";
my $trfSizeAndCountFile = "$jobRunningDir/trfSizeAndCount.txt";
my $utf8Files = $jobRunningDir."/utf8.txt_";
my $engineLockFile = $jobRunningDir.'/'.ENGINE_LOCKE_FILE;
my $progressDetailsFile = $jobRunningDir.$pathSeparator."PROGRESS_DETAILS";
my $jobCancelFile = $jobRunningDir.'/cancel.txt';
my $summaryFilePath = "$jobRunningDir/".Constants->CONST->{'fileDisplaySummary'};
#Renaming the log file if restore process terminated improperly
Helpers::checkAndRenameFileWithStatus($jobRunningDir);
# pre cleanup for all intermediate files and folders.
Helpers::removeItems([$RestoresetFile_relative."*", $noRelativeFileset."*", $filesOnly."*", $info_file, $retryinfo, "ERROR", $statusFilePath."*", $failedfiles."*", $progressDetailsFile."*", $jobCancelFile, $summaryFilePath]);
$errorDir = $jobRunningDir."/ERROR";
if(!-d $errorDir) {
my $ret = mkdir($errorDir);
if($ret ne 1) {
Helpers::traceLog("Couldn't create $errorDir: $!");
exit 1;
}
chmod $filePermission, $errorDir;
}
# Deciding Restore set File based on normal restore or version restore
if($ARGV[0] eq Constants->CONST->{'versionRestore'}) {
$RestoreFileName = $jobRunningDir."/versionRestoresetFile.txt";
}
my $serverAddress = verifyAndLoadServerAddr();
if ($serverAddress == 0){
exit_cleanup($errStr);
}
#my $encType = checkEncType($isScheduledJob); # This function has been called inside getOperationFile() function.
#createUpdateBWFile(); #Commented by Senthil: 13-Aug-2018
my $isEmpty = checkPreReq($RestoreFileName,$jobType,$taskType,'NORESTOREDATA');
#Helpers::retreat($errStr) if($isEmpty and $isScheduledJob == 0 and $silentFlag == 0);
if($isEmpty and $isScheduledJob == 0 and $silentFlag == 0) {
unlink($pidPath);
Helpers::retreat($errStr) ;
}
createLogFiles("RESTORE");
#$info_file = $jobRunningDir."/info_file";
$failedfiles = $jobRunningDir."/".$failedFileName;
createRestoreTypeFile();
Helpers::setUsername($userName) if(defined($userName) && $userName ne '');
if (Helpers::loadAppPath() and Helpers::loadServicePath() and Helpers::isLoggedin() and Helpers::loadNotifications()) {
Helpers::setNotification('update_restore_progress', ((split("/", $outputFilePath))[-1]));
Helpers::saveNotifications();
}
if(${ARGV[0]} eq ""){ #Only for Manual Restore.
emptyLocationsQueries();
}
$location = $restoreLocation;
$mail_content_head = writeLogHeader($isScheduledJob);
if($isScheduledJob == 0 and $silentFlag == 0 and !$isEmpty) {
getCursorPos();
}
startRestore() unless($isEmpty);
exit_cleanup($errStr);
#****************************************************************************************************
# Subroutine Name : startRestore
# Objective : This function will fork a child process to generate restoreset files and get
# count of total files considered. Another forked process will perform main
# restore operation of all the generated restoreset files one by one.
# Added By :
# Modified By : Senthil Pandian
#*****************************************************************************************************/
sub startRestore {
$generateFilesPid = fork();
if(!defined $generateFilesPid) {
$errStr = "Unable to start generateRestoresetFiles operation";
Helpers::traceLog("Cannot fork() child process, Reason:$!");
return;
}
generateRestoresetFiles() if($generateFilesPid == 0);
if($isScheduledJob == 0 and !$silentFlag){
$displayProgressBarPid = fork();
if(!defined $displayProgressBarPid) {
traceLog(Constants->CONST->{'ForkErr'}."$lineFeed", __FILE__, __LINE__);
$errStr = "Unable to start generateBackupsetFiles operation";
return;
}
if($displayProgressBarPid == 0) {
$pidOperationFlag = "DisplayProgress";
while(1){
displayProgressBar($progressDetailsFile);
if(!-e $pidPath){
last;
}
#select(undef, undef, undef, 0.100);
Helpers::sleepForMilliSec(100); # Sleep for 100 milliseconds
}
displayProgressBar($progressDetailsFile,Helpers::getTotalSize($fileForSize));
exit(0);
}
}
close(FD_WRITE);
open(my $handle, '>', $engineLockFile) or traceLog("\n Could not open file '$engineLockFile' $! \n", __FILE__, __LINE__);
close $handle;
chmod $filePermission, $engineLockFile;
my $exec_cores = getSystemCpuCores();
START:
if (-e $info_file){
if(!open(FD_READ, "<", $info_file)) {
$errStr = Constants->CONST->{'FileOpnErr'}." $info_file to read, Reason:$!";
Helpers::traceLog($errStr);
return;
}
my $lastFlag = 0;
while (1) {
if(!-e $pidPath){
last;
}
if($line eq "") {
$line = <FD_READ>;
}
if($line eq "") {
sleep(1);
seek(FD_READ, 0, 1); #to clear eof flag
next;
}
chomp($line);
$line =~ m/^[\s\t]+$/;
#space and tab space also trim
if($lastFlag eq 1) {
last;
}
if($line =~ m/^TOTALFILES/) {
$totalFiles = $line;
$totalFiles =~ s/TOTALFILES//;
$lastFlag = 1;
$line = "";
last;
}
else {
$isEngineRunning = isEngineRunning($pidPath.'_'.$engineID);
if(!$isEngineRunning){
while(1){
last if(!-e $pidPath or !isAnyEngineRunning($engineLockFile));
$exec_loads = get_load_average();
if($exec_loads > $exec_cores){
sleep(20);
next;
}
last;
}
if($retry_failedfiles_index != -1){
$retry_failedfiles_index++;
if($retry_failedfiles_index > 2000000000){
$retry_failedfiles_index = 0;
}
}
$restorePid = fork();
if(!defined $restorePid) {
$errStr = Constants->CONST->{'ForkErr'}.$whiteSpace.Constants->CONST->{"EvsChild"}.$lineFeed;
return RESTORE_PID_FAIL;
}
elsif($restorePid == 0) {
my $retType = doRestoreOperation($line,$taskType,$engineID,$retry_failedfiles_index);
exit(0);
}
else{
push (@RestoreForkchilds, $restorePid);
if(defined($exec_loads) and ($exec_loads > $exec_cores)){
sleep(2);
}
else{
sleep(1);
}
}
$line = "";
}
}
if($totalEngineBackup > 1)
{
$engineID++;
if($engineID > $totalEngineBackup){
$engineID = 1;
sleep(2);
}
}
Helpers::killPIDs(\@RestoreForkchilds,0);
if( !-e $pidPath) {
last;
}
}
waitForEnginesToFinish();
close FD_READ;
$nonExistsCount = Helpers::readInfoFile('FAILEDCOUNT');
waitpid($generateFilesPid,0);
undef @linesStatusFile;
if($totalFiles == 0 or $totalFiles !~ /\d+/) {
if(-e $info_file){
$totalFiles = Helpers::readInfoFile('TOTALFILES');
if($totalFiles == 0 or $totalFiles !~ /\d+/){
Helpers::traceLog("Unable to get total files count");
}
}
}
if(-s $retryinfo > 0 && -e $pidPath && $retrycount <= $maxNumRetryAttempts && $exitStatus == 0) {
if($retrycount == $maxNumRetryAttempts) {
for(my $i=1; $i<= $totalEngineBackup; $i++){
if(-e $statusFilePath."_".$i and -s $statusFilePath."_".$i>0){
readStatusFile($i);
my $index = "-1";
$statusHash{'FAILEDFILES_LISTIDX'} = $index;
putParameterValueInStatusFile($i);
undef @linesStatusFile;
}
}
$retry_failedfiles_index = -1;
}
move($retryinfo, $info_file);
updateRetryCount();
#append total file number to info
if(!open(INFO, ">>",$info_file)){
$errStr = Constants->CONST->{'FileOpnErr'}." $info_file, Reason $!".$lineFeed;
return;
}
print INFO "TOTALFILES $totalFiles\n";
print INFO "FAILEDCOUNT $nonExistsCount\n";
close INFO;
chmod $filePermission, $info_file;
$engineID = 1;
goto START;
}
}
}
#****************************************************************************************************
# Subroutine Name : checkRestoreItem.
# Objective : This function will check if restore items are files or folders
# Added By : Dhritikana
#*****************************************************************************************************/
sub checkRestoreItem {
if(!open(RESTORELIST, $RestoreFileName)){
Helpers::traceLog(Constants->CONST->{'FileOpnErr'}." $RestoreFileName , Reason: $!");
return 0;
}
if(!open(RESTORELISTNEW, ">", $RestoreItemCheck)){
Helpers::traceLog(Constants->CONST->{'FileOpnErr'}." $RestoreItemCheck , Reason: $!");
return 0;
}
$tempRestoreHost = $restoreHost;
if($dedup eq 'on'){
$tempRestoreHost = "";
}
while(<RESTORELIST>) {
chomp($_);
$_ =~ s/^\s+//;
if($_ eq "") {
next;
}
my $rItem = "";
if(substr($_, 0, 1) ne "/") {
$rItem = $tempRestoreHost."/".$_;
} else {
$rItem = $tempRestoreHost.$_;
}
print RESTORELISTNEW $rItem.$lineFeed;
}
close(RESTORELIST);
close(RESTORELISTNEW);
GETSTAT:
my @itemsStat = ();
my $checkItemUtf = getOperationFile( Constants->CONST->{'ItemStatOp'}, $RestoreItemCheck);
if(!$checkItemUtf) {
Helpers::traceLog($errStr);
return @itemsStat;
}
$checkItemUtf =~ s/\'/\'\\''/g;
$idevsutilCommandLine = "'$idevsutilBinaryPath'".$whiteSpace.$idevsutilArgument.$assignmentOperator."'".$checkItemUtf."'".$whiteSpace.$errorRedirection;
my @itemsStat = `$idevsutilCommandLine`;
# update server address if cmd failed due to wrong evs server address
if(updateServerAddr()){
goto GETSTAT;
}
unlink($checkItemUtf);
unlink($RestoreItemCheck);
return @itemsStat;
}
#****************************************************************************************************
# Subroutine Name : enumerateRemote.
# Objective : This function will search remote files for folders.
# Added By : Avinash Kumar.
# Modified By : Dhritikana
#*****************************************************************************************************/
sub enumerateRemote {
my $remoteFolder = $_[0];
my $searchForRestore = 1;
if( !-e $pidPath) {
return 0;
}
# remove / from begining for folder to avoid // while creating utf8 file.
if(substr($remoteFolder, -1, 1) eq "/") {
chop($remoteFolder);
}
# final EVS command to execute
if(! -d $search) {
if(!mkdir($search)) {
$errStr = "Failed to create search directory\n";
return 0;
}
chmod $filePermission, $search;
}
my $searchOutput = $search."/output.txt";
my $searchError = $search."/error.txt";
START:
my $searchUtfFile = getOperationFile(Constants->CONST->{'SearchOp'}, $remoteFolder);
if(!$searchUtfFile) {
return 0;
}
$searchUtfFile =~ s/\'/\'\\''/g;
$idevsutilCommandLine = "'$idevsutilBinaryPath'".$whiteSpace.$idevsutilArgument.$assignmentOperator."'".$searchUtfFile."'".$whiteSpace.$errorRedirection;
# my $commandOutput = `$idevsutilCommandLine`;
# EVS command execute
$res = `$idevsutilCommandLine`;
# traceLog(qq(res - $res));
if("" ne $res and $res !~ /no version information available/i){
$errStr = "search cmd syntax error found\n";
return REMOTE_SEARCH_CMD_ERROR;
}
# update server address if cmd failed due to wrong evs server address
if(updateServerAddr($searchError)){
goto START;
}
if(-s $searchError > 0) {
$errStr = "Remote folder enumeration has failed.\n";
checkExitError($searchError);
writeParameterValuesToStatusFile($fileBackupCount, $fileRestoreCount, $fileSyncCount, $failedfiles_count, $deniedFilesCount, $missingCount, $exit_flag, $failedfiles_index, $engineID);
return REMOTE_SEARCH_FAIL;
}
unlink($searchUtfFile);
# parse serach output.
open OUTFH, "<", $searchOutput or ($errStr = "cannot open :$searchOutput: of search result for $remoteFolder");
if($errStr ne ""){
Helpers::traceLog($errStr);
return REMOTE_SEARCH_OUTPUT_PARSE_FAIL;
}
if($dedup eq 'on'){
while(<OUTFH>){
@fileName = split("\"", $_, 43);
if($#fileName != 42) {
next;
}
$temp = $fileName[41];
replaceXMLcharacters(\$temp);
my $quoted_current_source = quotemeta($current_source);
if($relative == 0) {
if($current_source ne "/") {
if($temp =~ s/^$quoted_current_source//) {
print $filehandle $temp.$lineFeed;
} else {
next;
}
} else {
print $filehandle $temp.$lineFeed;
}
}
else {
if($temp =~ /\^$remoteFolder/) {
$current_source = "/";
print RESTORE_FILE $temp.$lineFeed;
$RestoresetFileTmp = $RestoresetFile_relative;
}
}
$totalFiles++;
$filecount++;
$size = $fileName[5];
$size =~ s/\D+//g;
$size =~ s/\s+//g;
$totalSize += $size;
if($filecount == FILE_MAX_COUNT) {
if( !-e $pidPath) {
last;
}
if(!createRestoreSetFiles1k()){
Helpers::traceLog($errStr);
return REMOTE_SEARCH_THOUSANDS_FILES_SET_ERROR;
}
}
}
} else {
while(<OUTFH>){
@fileName = split(/\] \[/, $_, SPLIT_LIMIT_SEARCH_OUTPUT); # split to get file name.
if($#fileName != 5) {
next;
}
chomp($fileName[5]);
chop($fileName[5]); # remove lat character as ']'.
$temp = $fileName[5];
my $quoted_current_source = quotemeta($current_source);
if($relative == 0) {
if($current_source ne "/") {
if($temp =~ s/^$quoted_current_source//) {
print $filehandle $temp.$lineFeed;
} else {
next;
}
} else {
print $filehandle $temp.$lineFeed;
}
}
else {
if($temp =~ /\^$remoteFolder/) {
$current_source = "/";
print RESTORE_FILE $temp.$lineFeed;
$RestoresetFileTmp = $RestoresetFile_relative;
}
}
$totalFiles++;
$filecount++;
$size = $fileName[1];
$size =~ s/\D+//g;
$size =~ s/\s+//g;
$totalSize += $size;
if($filecount == FILE_MAX_COUNT) {
if( !-e $pidPath) {
last;
}
if(!createRestoreSetFiles1k()){
# traceLog($errStr);
return REMOTE_SEARCH_THOUSANDS_FILES_SET_ERROR;
}
}
}
}
Helpers::traceLog($errStr);
return REMOTE_SEARCH_SUCCESS;
}
#****************************************************************************************************
# Subroutine Name : generateRestoresetFiles.
# Objective : This function will generate restoreset files.
# Added By : Dhritikana
#*****************************************************************************************************/
sub generateRestoresetFiles {
#check if running for restore version pl, in that case no need of generate files.
if($RestoreFileName =~ m/versionRestore/) {
if(!open(RFILE, "<", $RestoreFileName)) {
my $errStr = "Couldn't open file $RestoreFileName to read, Reason: $!\n";
Helpers::traceLog($errStr);
}
my $Rdata = '';
while(<RFILE>) {
$Rdata .= $_;
}
($versonedFile, $totalSize) = split(/\n/, $Rdata);
close(RFILE);
if(!open(WFILE, ">", $RestoreFileName)) {
my $errStr = "Couldn't open file $RestoreFileName to write, Reason: $!\n";
Helpers::traceLog($errStr);
}
print WFILE $versonedFile.$lineFeed;
close(WFILE);
$totalFiles = 1;
$current_source = "/";
#print FD_WRITE "$RestoreFileName ".NORELATIVE." $current_source\n";
print FD_WRITE "$current_source' '".NORELATIVE."' '$RestoreFileName\n";
goto GENEND;
}
my $traceExist = $errorDir."/traceExist.txt";
if(!open(TRACEERRORFILE, ">>", $traceExist)) {
Helpers::traceLog(Constants->CONST->{'FileOpnErr'}." $traceExist, Reason: $!.");
}
chmod $filePermission, $traceExist;
$pidOperationFlag = "GenerateFile";
my @itemsStat = checkRestoreItem();
chomp(@itemsStat);
@itemsStat = uniqueData(@itemsStat);
my $checkItems = join (' ',@itemsStat);
$filesonlycount = 0;
my $j = 0;
my $idx = 0;
if($#itemsStat ge 1) {
chomp(@itemsStat);
if($dedup eq 'on'){
foreach my $tmpLine (@itemsStat) {
if( !-e $pidPath) {
last;
}
if($tmpLine =~ /connection established/){
next;
}
#my ($key,$value) = split(/\="/, $tmpLine);
$tmpLine =~ s/\"\/\>//;
my @fields = split(/\="/, $tmpLine);
replaceXMLcharacters(\$fields[2]);
if($fields[1] =~ /directory exists/) {
#print "directory exists";
chop($fields[2]);
if($relative == 0) {
$noRelIndex++;
$RestoresetFile_new = $noRelativeFileset."$noRelIndex";
$filecount = 0;
$sourceIdx = rindex ($fields[2], '/');
$source[$noRelIndex] = substr($fields[2],0,$sourceIdx);
if($source[$noRelIndex] eq "") {
$source[$noRelIndex] = "/";
}
$current_source = $source[$noRelIndex];
if(!open $filehandle, ">>", $RestoresetFile_new){
$errStr = "Unable to get list of files to restore.\n";
Helpers::traceLog("cannot open $RestoresetFile_new to write");
goto GENEND;
}
chmod $filePermission, $RestoresetFile_new;
}
my $resEnumerate = 0;
$resEnumerate = enumerateRemote($fields[2]);
if(!$resEnumerate){
Helpers::traceLog(qq($errStr $fields[2]));
goto GENEND;
}
elsif(REMOTE_SEARCH_CMD_ERROR == $resEnumerate or REMOTE_SEARCH_FAIL == $resEnumerate or REMOTE_SEARCH_OUTPUT_PARSE_FAIL == $resEnumerate){
my $searchErrMsg = "[".(localtime)."]". "[".$fields[2]."] Failed. Reason: Search has failed for the item.$lineFeed";
Helpers::traceLog("Search command failed due to syntax error for the folder ". $fields[2]);
appendErrorToUserLog($searchErrMsg);
}
elsif(REMOTE_SEARCH_THOUSANDS_FILES_SET_ERROR == $resEnumerate){
Helpers::traceLog("Error in creating 1k files ". $fields[2]);
goto GENEND;
}
if($relative == 0 && $filecount>0) {
autoflush FD_WRITE;
#print FD_WRITE "$RestoresetFile_new#".RELATIVE."#$current_source\n";
print FD_WRITE "$current_source' '".RELATIVE."' '$RestoresetFile_new\n";
}
} elsif($fields[1] =~ /file exists/) {
#print "file exists";
my $propertiesFile = getOperationFile(Constants->CONST->{'PropertiesOp'}, $fields[2]);
my $tmp_idevsutilBinaryPath = $idevsutilBinaryPath;
$tmp_idevsutilBinaryPath =~ s/\'/\'\\''/g;
my $tmp_propertiesFile = $propertiesFile;
$tmp_propertiesFile =~ s/\'/\'\\''/g;
# EVS command to execute for properties
my $propertiesCmd = "\'$tmp_idevsutilBinaryPath\'".$whiteSpace.$idevsutilArgument.$assignmentOperator."\'$tmp_propertiesFile\'".$whiteSpace.$errorRedirection;
my $commandOutput = `$propertiesCmd`;
# traceLog($commandOutput);
unlink $propertiesFile;
$commandOutput =~ m/(size)(.*)/;
my $size = $2;
$size =~ s/\D+//g;
$totalSize += $size;
$current_source = "/";
print RESTORE_FILE $fields[2].$lineFeed;
if($relative == 0) {
$filesonlycount++;
$filecount = $filesonlycount;
}
else {
$filecount++;
}
$totalFiles++;
if($filecount == FILE_MAX_COUNT) {
$filesonlycount = 0;
if(!createRestoreSetFiles1k("FILESONLY")){
goto GENEND;
}
}
} elsif ($fields[1] =~ /No such file or directory/) {
#print "No such file or directory";
$totalFiles++;
$nonExistsCount++;
my $rfl = index($fields[2], '/', 1);
my $mfile = (length($fields[2]) > 2)? substr($fields[2], $rfl) : $fields[2];
print TRACEERRORFILE "[".(localtime)."] [FAILED] [$mfile]. Reason: No such file or directory".$lineFeed;
next;
}
}
} else {
foreach my $tmpLine (@itemsStat) {
if( !-e $pidPath) {
last;
}
my @fields = split("\\] \\[", $tmpLine, SPLIT_LIMIT_ITEMS_OUTPUT);
my $total_fields = @fields;
if($total_fields == SPLIT_LIMIT_ITEMS_OUTPUT) {
$fields[0] =~ s/^.//; # remove starting character [ from first field
$fields[$fields_in_progress-1] =~ s/.$//; # remove last character ] from last field
$fields[0] =~ s/^\s+//; # remove spaces from beginning from required fields
$fields[1] =~ s/^\s+//;
if ($fields[1] eq "." or $fields[1] eq "..") {
next;
}
if($fields[0] =~ /directory exists/) {
chop($fields[1]);
if($relative == 0) {
$noRelIndex++;
$RestoresetFile_new = $noRelativeFileset."$noRelIndex";
$filecount = 0;
$sourceIdx = rindex ($fields[1], '/');
$source[$noRelIndex] = substr($fields[1],0,$sourceIdx);
if($source[$noRelIndex] eq "") {
$source[$noRelIndex] = "/";
}
$current_source = $source[$noRelIndex];
if(!open $filehandle, ">>", $RestoresetFile_new){
$errStr = "Unable to get list of files to restore.\n";
Helpers::traceLog("cannot open $RestoresetFile_new to write ");
goto GENEND;
}
chmod $filePermission, $RestoresetFile_new;
}
my $resEnumerate = 0;
$resEnumerate = enumerateRemote($fields[1]);
if(!$resEnumerate){
Helpers::traceLog(qq($errStr $fields[1]));
goto GENEND;
}
elsif(REMOTE_SEARCH_CMD_ERROR == $resEnumerate or REMOTE_SEARCH_FAIL == $resEnumerate or REMOTE_SEARCH_OUTPUT_PARSE_FAIL == $resEnumerate){
my $searchErrMsg = "[".(localtime)."]". "[".$fields[1]."] Failed. Reason: Search has failed for the item.$lineFeed";
Helpers::traceLog("Search command failed due to syntax error for the folder ". $fields[1]);
appendErrorToUserLog($searchErrMsg);
}
elsif(REMOTE_SEARCH_THOUSANDS_FILES_SET_ERROR == $resEnumerate){
Helpers::traceLog("Error in creating 1k files ". $fields[1]);
goto GENEND;
}
if($relative == 0 && $filecount>0) {
autoflush FD_WRITE;
#print FD_WRITE "$RestoresetFile_new#".RELATIVE."#$current_source\n";
print FD_WRITE "$current_source' '".RELATIVE."' '$RestoresetFile_new\n";
}
} elsif($fields[0] =~ /file exists/) {
my $propertiesFile = getOperationFile(Constants->CONST->{'PropertiesOp'}, $fields[1]);
my $tmp_idevsutilBinaryPath = $idevsutilBinaryPath;
$tmp_idevsutilBinaryPath =~ s/\'/\'\\''/g;
my $tmp_propertiesFile = $propertiesFile;
$tmp_propertiesFile =~ s/\'/\'\\''/g;
# EVS command to execute for properties
my $propertiesCmd = "\'$tmp_idevsutilBinaryPath\'".$whiteSpace.$idevsutilArgument.$assignmentOperator."\'$tmp_propertiesFile\'".$whiteSpace.$errorRedirection;
my $commandOutput = `$propertiesCmd`;
# traceLog($commandOutput);
unlink $propertiesFile;
$commandOutput =~ m/(size)(.*)/;
my $size = $2;
$size =~ s/\D+//g;
$totalSize += $size;
$current_source = "/";
print RESTORE_FILE $fields[1].$lineFeed;
if($relative == 0) {
$filesonlycount++;
$filecount = $filesonlycount;
}
else {
$filecount++;
}
$totalFiles++;
if($filecount == FILE_MAX_COUNT) {
$filesonlycount = 0;
if(!createRestoreSetFiles1k("FILESONLY")){
goto GENEND;
}
}
} elsif ($fields[0] =~ /No such file or directory/) {
$totalFiles++;
$nonExistsCount++;
print TRACEERRORFILE "[".(localtime)."] [FAILED] [$fields[1]]. Reason: No such file or directory".$lineFeed;
next;
}
}
}
}
}
else{
checkExitError($idevsErrorFile);
writeParameterValuesToStatusFile($fileBackupCount, $fileRestoreCount, $fileSyncCount, $failedfiles_count, $deniedFilesCount, $missingCount, $exit_flag, $failedfiles_index, $engineID);
}
if($relative == 1 && $filecount > 0){
#print FD_WRITE "$RestoresetFile_new#".RELATIVE."#$current_source \n"; #[dynamic]
print FD_WRITE "$current_source' '".RELATIVE."' '$RestoresetFile_new \n"; #[dynamic]
}
elsif($filesonlycount >0){
$current_source = "/";
#print FD_WRITE "$RestoresetFile_Only#".NORELATIVE."#$current_source\n"; #[dynamic]
print FD_WRITE "$current_source' '".NORELATIVE."' '$RestoresetFile_Only\n"; #[dynamic]
}
GENEND:
autoflush FD_WRITE;
print FD_WRITE "TOTALFILES $totalFiles\n";
print FD_WRITE "FAILEDCOUNT $nonExistsCount\n";
close(FD_WRITE);
close RESTORE_FILE;
open FILESIZE, ">$fileForSize" or Helpers::traceLog(Constants->CONST->{'FileOpnErr'}." $fileForSize. Reason: $!");
print FILESIZE "$totalSize";
close FILESIZE;
chmod $filePermission, $fileForSize;
$pidOperationFlag = "generateListFinish";
close(TRACEERRORFILE);
exit 0;
}
#****************************************************************************************************
# Subroutine Name : createRestoreSetFiles1kcreateRestoreSetFiles1k.
# Objective : This function will generate 1000 Backetupset Files
# Added By : Pooja Havaldar
# Modified By : Avinash Kumar
#*****************************************************************************************************/
sub createRestoreSetFiles1k {
my $filesOnlyFlag = $_[0];
$Restorefilecount++;
if($relative == 0) {
if($filesOnlyFlag eq "FILESONLY") {
$filesOnlyCount++;
#print FD_WRITE "$RestoresetFile_Only#".NORELATIVE."#$current_source\n"; # 0
print FD_WRITE "$current_source' '".NORELATIVE."' '$RestoresetFile_Only\n"; # 0
$RestoresetFile_Only = $filesOnly."_".$filesOnlyCount;
close RESTORE_FILE;
if(!open RESTORE_FILE, ">", $RestoresetFile_Only) {
Helpers::traceLog(Constants->CONST->{'FileOpnErr'}." $filesOnly to write, Reason: $!.");
return 0;