-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoRestore
executable file
·1206 lines (1205 loc) · 34.4 KB
/
doRestore
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/perl -w
#
# restore backedup files to AWS/S3|Glacier|Freezer or AZ/Hot|Cool|Archive
# or via rclone and on a local disk
#
# <- Last updated: Fri Apr 14 16:25:15 2023 -> SGK
#
# (c) 2021-2023 - Sylvain G. Korzennik, Smithsonian Institution
#
# ---------------------------------------------------------------------------
#
use strict;
## use File::Touch;
use Cwd;
#
my @w = split('/', $0);
my $SCRIPT = pop(@w);
#
local $main::VERNO = '';
local %main::unxCmd = (
'cp' => '/usr/bin/cp',
'df' => '/usr/bin/df',
'find' => '/usr/bin/find',
'tar' => '/usr/bin/tar',
'split' => '/usr/bin/split',
'gzip' => '/usr/bin/gzip',
'gunzip' => '/usr/bin/gunzip',
'lz4' => '/usr/bin/lz4',
'compress' => '/usr/bin/compress',
'uncompress' => '/usr/bin/uncompress',
'bzip2' => '/usr/bin/bzip2',
'bunzip2' => '/usr/bin/bunzip2',
'lzma' => '/usr/local/bin/lzma',
'lzcat' => '/usr/local/bin/lzcat',
'cat' => '/usr/bin/cat',
'chmod' => '/usr/bin/chmod',
'chown' => '/usr/bin/chown',
'chgrp' => '/usr/bin/chgrp',
'ln' => '/usr/bin/ln',
'ls' => '/usr/bin/ls',
'touch' => '/usr/bin/touch',
'xcp' => '/root/bin/xcp',
);
#
my $BINDIR = '.';
if ($#w >= 0) {
$BINDIR = join('/', @w);
}
@INC = (@INC, $BINDIR);
#
require "parseArgs.pl";
require "utils.pl";
require "cloud.pl";
#
my %defOptions = (
'CLOUD' => 'aws:s3_glacier', # default cloud service to use
'DRYRUN' => 0, # if set, will not donwload archives, but show what is needed
'NOREMOVE' => 0, # if set, will not remove downloaded the archives
#
'ARCHLEN' => '6', # X value for X.Xd fmt for I used in archive-$I
'SPLTLEN' => '6', # X value for --suffix-length=X in split
'VERBOSE' => 0, # if set will echo more info
'OUTDIR' => '/tmp', # where to restore the stuff
'USEPERLRE' => 0, # use PERL RE, not shell RE * -> .* and ? -> .
'NOCHOWN' => 0, # do not chown restored split files
'NOCHGRP' => 0, # do not chgrp restored split files
#
'AWS' => "aws",
'AZCLI' => "az-cli",
'AZ_ANAME' => 'azbackup',
'RCLONE' => "rclone",
#
'SCRATCH' => '/scratch/backup',
'RCFILE' => $ENV{HOME}.'/.dobackuprc',
#
'PARSEONLY' => 0,
'VERSION' => '0.99/8 (Apr 14 2023)',
);
#
#
# ---------------------------------------------------------------------------
#
# read a config file
# ------------------
my %defOpts = %defOptions;
# ignore these
my %ignOpts = (
'BASEDIR' => '',
'COMPRESS' => '',
'INCEDIRS' => '',
'KEEPTARLISTS' => '',
'LABEL' => '',
'LEVEL' => '',
'LIMIT_TO' => '',
'MAXCOUNT' => '',
'MAXSIZE' => '',
'NOREMOVE' => '',
'NOUPLOAD' => '',
'NTHREADS' => '',
'SCANWITH' => '',
'EXTRASLEEP' => '',
'SORTBY' => '',
'RCMDATASET' => '',
'UPLOAD_NPASS' => '',
'UPLOAD_STIME' => '',
'TAR_CF_OPTS' => '',
'TAG_HOST' => '',
'TAG_AUTHOR' => '',
);
ReadConfig($SCRIPT, \@ARGV, \%defOpts, \%ignOpts, \%main::unxCmd);
#
my @clouds = qw/aws:glacier
aws:s3_glacier aws:s3_freezer aws:s3_standard
az:archive az:cool az:hot
^rclone: ^ldisk:/;
#
my @LISTOPTS = (
'',
"-usage: $SCRIPT [options]",
'- where options are',
'',
'use-cloud=list('.join(',',@clouds).') ; CLOUD cloud service to use, def.: '.$defOpts{CLOUD},
'use-vault=string ; VAULT vault name {bkup|frzr}-label-xxx-yyyymmdd-hhmm-lx',
'scratch=directory ; VALUE scratch directory, def.: '.$defOpts{SCRATCH},
'out-dir=directory ; OUTDIR directory where to restore def.: '.$defOpts{OUTDIR},
'',
'use-dir=string ; USEDIR set directory for showing or restoring files',
' ; can be a RE when showing files,',
' ; but must be exact when restoring files',
'',
'no-remove ; do not remove the archives',
'no-chown ; do not chown restored split files (when root)',
'no-chgrp ; do not chgrp restored split files',
'use-perl-re ; use PERL style RE for file specifications',
'',
'rc|config-file=file ; FILENAME configuration filename, def.: '.$defOpts{RCFILE},
'n|dry-run ; dry run: find the files and list the archives',
'v|verbose=repeat ; verbose',
'p|parse-only ; parse the args and check them only',
'h|help ; show this help (ignore any remaining arguments)',
'',
' and one of the following actions:',
'show-dirs ; show directories',
'show-files ; show files saved under USEDIR',
'restore-files=string ; FILES restore the list of files (quoted) in USEDIR',
'clean-scratch ; remove archives downloaded to scratch',
'',
'-Ver. '.$defOpts{VERSION});
#
# add options mapping when var name is not upper case(option) w/out all the '-'
my @MAPOPTS = (
'--config-file:RCFILE',
'--tag:TAG_DATE',
'--use-cloud:CLOUD',
'--use-vault:USETHISVAULT',
);
#
my @help = ();
#
# parse the options
# -----------------
my %opts = &ParseArgs($SCRIPT, \@ARGV, \%defOpts, \@LISTOPTS, \@MAPOPTS, \@help);
#
# show help and exit if -h|--help -> $opts{HELP}
# ---------------------------------------------
if (defined $opts{HELP} ) {
print join("\n", @help),"\n";
exit(0);
}
# Validate Opts
# --------------
&ValidateRestoreOpts($SCRIPT, \%opts);
#
# echo options
# ------------
if ($opts{VERBOSE}) {
foreach my $key (sort(keys(%opts))) {
printf STDERR "%12s = %s\n", $key, $opts{$key};
}
if ($opts{VERBOSE} > 1) {
printf STDERR "unix commands:\n";
foreach my $key (sort (keys(%main::unxCmd))) {
printf STDERR "%12s = %s\n", $key, $main::unxCmd{$key};
}
}
}
#
# ran into an error -> exit
# ----------------
if ($opts{STATUS}) {
my $s = ''; if ($opts{NERRORS} > 1 ) { $s = 's'; }
print STDERR "$SCRIPT: exiting b/c of $opts{NERRORS} error$s.\n";
exit(1);
}
#
# parse args only -> exit
# ---------------
if ($opts{PARSEONLY} != 0) {
print STDERR "$SCRIPT --parse-only: exiting.\n";
exit(0);
}
#
# add vault name to scratch
$opts{SCRATCH} .= $opts{VAULT}.'/';
#
# execute requested actions
# ------------------------------------------------------------------------
my $startTime = time();
#
my $now = Now();
print STDERR "+ $now $SCRIPT started\n";
#
my $status = 1;
$opts{NERRORS} = 0;
my $a = '';
if ($opts{SHOWDIRS}) {
#
$a = 'ShowDirsList()';
$status = &ShowDirsList(\%opts);
#
} elsif ($opts{SHOWFILES}) {
#
$a = 'ShowFilesList()';
$status = &ShowFilesList(\%opts);
#
} elsif ($opts{RESTOREFILES}) {
#
$a = 'RestoreFiles()';
$status = &RestoreFiles(\%opts);
#
#} elsif ($opts{SHOWARCHIVES}) {
# #
# $status = &ShowArchList(\%opts);
# #
} elsif ($opts{CLEANSCRATCH}) {
#
$a = 'CleanScratch()';
$status = &CleanScratch(\%opts);
#
} else {
print STDERR "$SCRIPT: internal error #0\n";
}
# print how long it took and exit
# ----
$now = Now();
my $elapsedTime = ElapsedTime($startTime);
#
if ($status) {
print STDERR "= $now $SCRIPT $opts{ACTION} failed - $elapsedTime\n";
} else {
my $xtra = '';
if ($opts{DRYRUN}) { $xtra = ' --dry-run'; }
my $s = &AddS($opts{NERRORS});
print STDERR "= $now $SCRIPT $a$xtra done, $opts{NERRORS} error$s - $elapsedTime\n";
}
exit($status);
#
# ---------------------------------------------------------------------------
#
sub ShowDirsList {
#
# show the list of directories saved in a vault, all args in %opts
#
my %opts = %{$_[0]};
my $status = 1;
#
if ($opts{VERBOSE}) {
print STDERR "$SCRIPT: ShowDirsList()\n";
print STDERR " vault: $opts{VAULT}\n";
print STDERR " cloud: $opts{CLOUD}\n";
}
#
# fetch the archives list if not found in scratch
#
my $msg = '';
my $archivesListFile = $opts{SCRATCH}.'archivesList.txt';
if (! -e $archivesListFile) {
#
# make sure the scratch dir exists
if (! -d $opts{SCRATCH}) {
if ($opts{VERBOSE}) {
print STDERR "$SCRIPT: ShowDirsList() creating $opts{SCRATCH}\n";
}
$status = MkDir($opts{SCRATCH}, $SCRIPT);
if ($status) { return $status; }
}
#
# check if the vault exists
print STDERR "$SCRIPT: ShowDirsList() checking for vault=$opts{VAULT}, cloud=$opts{CLOUD}\n";
$status = checkForVault(%opts);
if ($status) { return $status; }
#
# download the main archives list
my $file = "$opts{VAULT}/archivesList.txt";
my $dest = $opts{SCRATCH};
print STDERR "$SCRIPT: ShowDirsList() downloading the archives list\n vault: $opts{VAULT}\n cloud: $opts{CLOUD}\n";
$status = DownloadFromCloud($SCRIPT, $file, $dest, \*STDERR, \%opts);
if ($status) { return $status; }
#
$msg = ", cloud=$opts{CLOUD}";
} else {
#
print STDERR "$SCRIPT: ShowDirsList() using the archives list in $opts{SCRATCH}\n";
#
}
#
# read the archive list
#
if ($opts{VERBOSE} > 1) { print STDERR "$SCRIPT: ShowDirsList() reading $archivesListFile\n"; }
#
my @names = GetDirsList($archivesListFile);
#
foreach my $name (@names) {
printf " %s\n", $name;
}
#
my $n = $#names + 1;
my $y = 'y'; if ($n > 1) { $y = 'ies'; }
print STDERR "$SCRIPT: ShowDirsList() $n director$y saved in vault=$opts{VAULT}$msg\n";
#
$status = 0;
return $status;
}
#
# ---------------------------------------------------------------------------
#
sub GetDirsList {
#
# read the archive list, no filtering
#
my $archivesListFile = shift();
my %infos = ReadArchivesList($archivesListFile, '', 'infos.tgz$');
my @names = sort(keys(%infos));
return @names;
}
#
# ---------------------------------------------------------------------------
#
#sub ShowArchsList {
# $status = 1;
# return $status;
#}
#
# ---------------------------------------------------------------------------
#
sub ShowFilesList {
#
# show which files are saved in a directory, all args in %opts
#
my %opts = %{$_[0]};
my $status = 1;
#
# add '/' to dir
if ($opts{USEDIR} !~ /\/$/) { $opts{USEDIR} .= '/' };
#
if ($opts{VERBOSE}) {
print STDERR "$SCRIPT: ShowFilesList()\n";
print STDERR " vault: $opts{VAULT}\n";
print STDERR " cloud: $opts{CLOUD}\n";
print STDERR " dir: $opts{USEDIR}\n";
}
#
my $archivesListFile = $opts{SCRATCH}.'archivesList.txt';
$status = &CheckForArchivesListFile($archivesListFile, \%opts);
if ($status) { return $status; }
#
# convert '$dir' to perl RE
#
my $dir;
if ($opts{USEPERLRE}) {
$dir = $opts{USEDIR};
} else {
$dir = ConvertRE($opts{USEDIR});
}
#
# read the archives list
#
my %infos = &ReadArchivesList($archivesListFile, $dir, 'infos.tgz$');
my @dirList = sort(keys(%infos));
my $n = $#dirList + 1;
#
if ($n == 0) {
print STDERR "$SCRIPT: ShowFilesList() no directory matching '$opts{USEDIR}' found\n";
return $status;
}
#
# loop on matching dirs
#
my $s = ''; if ($n > 1) { $s = 'es'; }
print STDERR "$SCRIPT: ShowFilesList() $n match$s for '$opts{USEDIR}' in the archives list\n";
#
my $nFilesTot = 0;
my $nSplitTot = 0;
#
# loop on dir to check for findList.txt (and infos.tgz)
#
foreach my $dir (@dirList) {
#
my $sdir = $opts{SCRATCH}.$dir;
#
#
# do we have the findList.txt
#
if (! -e $sdir.'findList.txt') {
#
my $infosSetTgz = $dir.'infos.tgz';
if (! -e $opts{SCRATCH}.'/'.$infosSetTgz) {
#
print STDERR "$SCRIPT: ShowFilesList() downloading infos set for '$dir'\n";
#
my $file = "$opts{VAULT}/$infosSetTgz";
my $dest = $opts{SCRATCH}.$dir;
$status = DownloadFromCloud($SCRIPT, $file, $dest, \*STDERR, \%opts);
if ($status) { return $status; }
}
#
print STDERR "$SCRIPT: ShowFilesList() extracting files list for '$dir'\n";
#
# need to break dir in head and tail b/c of the way the infos.tgz is written
#
my @w = split('/', $dir);
my $dirExtn = pop(@w);
my $dirBase = join('/', @w);
#
my $tarFile = $dirExtn.'/infos.tgz';
my $extractList = $dirExtn.'/findList.txt';
my $cdTo = $opts{SCRATCH}.$dirBase;
#
$status = ExtractListFromTarSet($tarFile, $extractList, $cdTo, \%opts);
if ($status) { return $status; }
}
#
}
#
# loop on dir to show content
#
foreach my $dir (@dirList) {
printf "==> %s <==\n", $dir;
#
my $sdir = $opts{SCRATCH}.$dir;
#
#
# read the findList - does not reflect what might not have been saved, tho.
#
my %findListInfos = &ParseFindList($sdir);
my @files = sort ( keys(%findListInfos) );
my $nFiles = $#files+1;
my $nSplit = 0;
my $nParts = 0;
my $nArchs = 0;
#
foreach my $file ( @files ) {
#
my $infos = $findListInfos{$file};
#
my @w = split(' ', $infos);
#
my @times = split('/', $w[0]);
my ($sizx, $sparseFtr) = split('/', $w[1]);
my ($usr, $grp) = split('/', $w[2]);
my ($mode, $typ) = split('/', $w[3]);
my ($set, $parts) = split('/', $w[4].'/0'); # make sure $parts = 0 when not split
#
my $size = &FmtSize($sizx);
my $mstr = &ConvertMode($mode);
# which time to show: modify as in %A@/%T@/%C@ == atime, mtime, ctime
# or %C@/%T@ == ctime, mtime
my $tstr = &ConvertTime($times[1]);
#
my $pstr;
if ($parts > 0) {
$pstr = sprintf("%3d/%-3d", $set, $parts);
} else {
$pstr = sprintf("%3d ", $set);
}
printf "%s %s/%s %s %s %s %s\n", $mstr, $usr, $grp, $size, $tstr, $pstr, $file;
#
if ($set > $nArchs) { $nArchs = $set; }
if ($parts > 0) { $nSplit++; $nParts += $parts; }
}
#
$nArchs += 1+$nParts-$nSplit;
#
$nFilesTot += $nFiles;
$nSplitTot += $nSplit;
my @s = ('','','');
if ($nFiles > 1) { $s[0] = 's'; }
if ($nArchs > 1) { $s[1] = 's'; }
if ($nParts > 1) { $s[2] = 's'; }
print STDERR "$SCRIPT: ShowFilesList() $nFiles file$s[0] ".
"($nArchs archive$s[1]), $nSplit splitted ($nParts part$s[2]) for $dir\n";
}
#
if ($#dirList > 0) {
$s = &AddS($nFilesTot);
print STDERR "\n";
print STDERR "$SCRIPT: ShowFilesList() $nFilesTot file$s, $nSplitTot splitted in $opts{USEDIR}\n";
}
return 0;
#
}
#
# ---------------------------------------------------------------------------
#
sub RestoreFiles {
#
# restore files, all args in %opts
#
my %opts = %{$_[0]};
my $status = 1;
#
my $dir = $opts{USEDIR};
if ($dir !~ /\/$/) { $dir .= '/' };
my $sdir = $opts{SCRATCH}.$dir;
#
my $outdir = $opts{OUTDIR};
if ($outdir !~ /\/$/) { $outdir .= '/' };
#
if ($opts{VERBOSE}) {
print STDERR "$SCRIPT: RestoreFiles()\n";
print STDERR " vault: $opts{VAULT}\n";
print STDERR " cloud: $opts{CLOUD}\n";
print STDERR " archdir: $dir\n";
print STDERR " scatch: $sdir\n";
print STDERR " outdir: $outdir\n";
print STDERR " file(s): $opts{RESTOREFILES}\n";
}
#
# read the archivesList.txt
#
my $archivesListFile = $opts{SCRATCH}.'archivesList.txt';
$status = &CheckForArchivesListFile($archivesListFile, \%opts);
if ($status) { return $status; }
#
my %archInfos = &ReadArchivesList($archivesListFile, '', '');
my @archList = keys(%archInfos);
#
my $infosSet = $sdir.'infos.tgz';
#
# convert $dir to PERL RE
my $xdir = $dir;
if ($opts{USEPERLRE} == 0) {
$xdir = ConvertRE($dir);
}
#
## could convert xdir to a real dir using GetDirsList()
## my @names = GetDirsList($archivesListFile);
## print STDERR " grep(/$xdir/, @archList)\n";
#
my @m = grep(/$xdir/, @archList);
#
if (! -e $infosSet) {
if ($opts{VERBOSE}) {
print STDERR "$SCRIPT: ${sdir}infos.tgz not found\n";
}
if ($#m >= 0) {
print STDERR "run '$SCRIPT --show-files $dir' first\n";
} else {
print STDERR "$SCRIPT: '$dir' not found in vault=$opts{VAULT}, cloud=$opts{CLOUD}\n";
print STDERR " run '$SCRIPT --show-dirs' to get the list\n";
}
return 1;
}
#
# build the list of RE/files to restore
#
my @filex = split(' ', $opts{RESTOREFILES});
my @files = ();
#
# read the list from a file if specified as' @filename'
foreach my $file (@filex) {
if ($file =~ /^@/) {
$file =~ s/.//;
if (-e $file) {
my @content = GetFileContent($file);
@files = (@files, @content);
} else {
print STDERR "$SCRIPT: RestoreFiles() file '$file' not found\n";
return 1;
}
} else {
@files = (@files, $file);
}
}
#
# convert @files list to PERL RE and escape [.+] (shell RE), unless --use-perl-re
#
@filex = @files;
@files = ();
#
foreach my $file (@filex) {
#
my $xfile;
if ($opts{USEPERLRE}) {
$xfile = $file;
} else {
$xfile = ConvertRE($file);
}
$xfile = $dir.$xfile;
@files = (@files, $xfile);
}
#
# get the findList info
#
my %findListInfos = &ParseFindList($sdir);
my @lines = keys(%findListInfos);
#
my %archNeeded = ();
my %archSets = ();
#
# loop on the files
#
foreach my $file (@files) {
#
# check if $file is a match
#
my @match = grep(/$file$/, @lines);
my $n = $#match + 1;
#
if ($n > 0) {
#
#
foreach my $match (@match) {
my $infos = $findListInfos{$match};
my @w = split(' ', $infos);
# my ($set, $parts) = split('/', $w[4]);
my $setInfo = $w[4];
$archNeeded{$setInfo}++;
$archSets{$match} = $setInfo
}
}
}
#
my @allMatch = sort(keys(%archSets ));
my @archNeeded = sort(keys(%archNeeded));
my $nTot = $#allMatch+1;
my $nArch = $#archNeeded+1;
#
if ($nTot == 0) {
print STDERR "$SCRIPT: RestoreFiles() file specification '$opts{RESTOREFILES}' lead to $nTot file to restore\n";
return 0;
}
#
my $aa = 'archive is';
if ( $nArch > 1 ) { $aa = 'archives are'; }
my $s = &AddS($nTot);
print STDERR "$SCRIPT: RestoreFiles() $nArch $aa needed to restore $nTot file$s\n";
#
# created the ouput directory if needed
#
if (! -d $outdir) {
#
print STDERR "$SCRIPT: creating $outdir\n";
#
$status = MkDir($outdir, $SCRIPT);
if ($status) { return $status; }
}
#
# loop on the needed archive list
#
foreach my $setInfo (@archNeeded) {
#
my ($set, $parts) = split('/', $setInfo.'/0'); # ibidem, $parts=0
#
#
# loop on the file list to make the list for this set
#
my @files = ();
foreach my $file (@allMatch) {
if ($archSets{$file} eq $setInfo) {
@files = (@files, $file);
}
}
#
# check if have downloaded the archives
#
my $i;
my $ix = $parts; if ($parts == 0) { $ix++; }
#
my @parts = ();
my $nMissing = 0;
for ($i = 0; $i < $ix; $i++) {
#
my $archName;
my $archFmt = '%'.sprintf('%d.%d', $opts{ARCHLEN}, $opts{ARCHLEN}).'d';
my $spltFmt = '%'.sprintf('%d.%d', $opts{SPLTLEN}, $opts{SPLTLEN}).'d';
#
if ($parts == 0) {
$archName = sprintf('archive-'.$archFmt, $set);
} else {
$archName = sprintf('archive-'.$archFmt.'.'.$spltFmt.'.splt', $set, $i);
}
my @match = grep(/^$dir$archName/, @archList);
#
my $n = $#match +1;
if ($n!= 1) {
print STDERR "$SCRIPT: RestoreFiles() - internal error - arch name match = $n not 1\n";
## print STDERR ">> /^$dir$archName/\n >", join("<\n >",@archList),"<\n";
return 1;
}
$archName = $match[0];
#
@parts = (@parts, $opts{SCRATCH}.$archName);
#
my $size = $archInfos{$archName};
#
if ($opts{VERBOSE}) {
print STDERR "RestoreFiles() archive needed:\n";
print STDERR " archive: $archName\n";
print STDERR " size: $size\n";
}
if (! -e $opts{SCRATCH}.$archName) {
#
# this needs the archList for this dir
#
if ($opts{DRYRUN}) {
#
my $xtra = '';
if ($parts > 0) { $xtra = "/$i"; }
print STDERR "$SCRIPT: RestoreFiles() need to download archive #$set$xtra for\n";
$nMissing++;
#
#if ($opts{VERBOSE}) {
# print STDERR " archive: $archName\n";
# print STDERR " size: $size\n";
#}
if ($i+1 == $ix) {
print STDERR " ".join("\n ", @files),"\n";
}
#
} else {
#
my $xtra = '';
if ($parts > 0) { $xtra = "/$i"; }
print STDERR "$SCRIPT: RestoreFiles() downloading archive #$set$xtra ($size)\n";
if ($opts{VERBOSE}) {
print STDERR " archive: $archName\n";
print STDERR " size: $size\n";
}
#
my $file = "$opts{VAULT}/$archName";
my $dest = $sdir;
$status = DownloadFromCloud($SCRIPT, $file, $dest, \*STDERR, \%opts);
if ($status) { return $status; }
#
}
}
}
#
# return here if dry run
#
if ($opts{DRYRUN}) {
my $msg = "all already";
if ($nMissing > 0) {
my $archiveNeed = 'archive still needs';
if ( $nMissing > 1 ) { $archiveNeed = 'archives still need'; }
$msg = "$nMissing $archiveNeed to be";
}
print STDERR "$SCRIPT: RestoreFiles() $msg downloaded\n";
return 0;
}
#
# now extract or concat
#
my $n;
my $s;
my $cmd;
#
# ls -d -l, but what abt - ??
my $ls = "$main::unxCmd{ls} -d"; if ($opts{VERBOSE}) { $ls .= ' -l'; }
#
if ($parts == 0) {
#
# extract @files from $archName
#
my $archName = $parts[0];
#
my $listFn = "/tmp/list.$$";
open(LIST, ">$listFn");
print LIST join("\n", @files), "\n";
close(LIST);
#
$n = $#files+1;
$s = &AddS($n);
if ($opts{VERBOSE} > 1) {
print STDERR "$SCRIPT: RestoreFiles() extracting $n file$s from $archName\n";
}
#
my $extractList = "--files-from=$listFn";
my $tarFile = $archName;
my $cdTo = $outdir;
#
$status = ExtractListFromTarSet($tarFile, $extractList, $cdTo, \%opts);
unlink($listFn);
if ($status) { $opts{NERRORS}++; }
#
print STDERR "$SCRIPT: RestoreFiles() $n file$s restored under $outdir\n";
#
$cmd = "cd $outdir; ";
$cmd .= "$ls ".join(' ', @files);
$status = ExecuteCmd($cmd, $opts{VERBOSE}, \*STDOUT);
if ($status) { $opts{NERRORS}++; }
#
#
} else {
#
# contact @files from archNames
#
my $n = $#files+1;
if ($n != 1) {
print STDERR "$SCRIPT: RestoreFiles() - internal error - no of files = $n not 1\n";
return 1;
}
#
# $#files+1, " file from $parts parts\n";
#
my $file = $files[0];
my $xtra = '';
if ($opts{VERBOSE}) { $xtra = " under $outdir\n"; }
print STDERR "$SCRIPT: RestoreFiles() assembling the $parts parts of $file$xtra\n";
#
# select which code to assemble based on extention
my %codes = ('splt' => $main::unxCmd{cat},
'gz' => $main::unxCmd{gunzip}.' -c',
'lz4' => $main::unxCmd{lz4}.' -c',
'bz2' => $main::unxCmd{bunzip2}.' -c',
'Z' => $main::unxCmd{uncompress}.' -c',
'lzma' => $main::unxCmd{lzcat});
my @w = split('\.', $parts[0]);
my $ext = pop(@w);
# this should not happen, tho
if (! defined $codes{$ext}) {
die "doRestore: RestoreFile() cannot concat *.'$ext' files\n";
}
my $cat = $codes{$ext};
my $cmd = "cd $outdir; $cat ".join(' ', @parts)." > $file";
$status = ExecuteCmd($cmd, $opts{VERBOSE}, \*STDERR);
if ($status) { return $status; }
#
# need to touch/chown/chmod the file
#
my ($timeInfo, $sizeInfo, $ownerInfo, $permInfo, $partInfo) = split(' ', $findListInfos{$file});
# ctime, mtime -> atime, mtime, ctime
# ctime cannot be changed by touch
my @times = split('/', $timeInfo);
my $modTime = $times[1];
my $accTime = $times[1];
if ($#times == 2 ) { $accTime = $times[0]; }
my ($user, $group) = split('/', $ownerInfo);
my ($perm, $type) = split('/', $permInfo);
#
$modTime = &FmtTime($modTime);
$accTime = &FmtTime($accTime);
#
my $ffile = "$outdir/$file";
#
# could use PERL's version via File::Touch
$cmd = "$main::unxCmd{touch} -m -t $modTime $ffile; ";
$cmd .= "$main::unxCmd{touch} -a -t $accTime $ffile; ";
$cmd .= "$main::unxCmd{chmod} 0$perm $ffile;";
## chmod '0'.$perm, $ffile;
#
# must be root to chown
# to use PERL's chown $uid, $gid, $ffile; # uid/gid must be numeric
# run as regular user the chgrp can fail
# and when restoring to a different machine, either chown or chgrp can fail
#
my $id = $<;
if ($id == 0) {
if ($opts{NOCHOWN} == 0) {
$cmd .= "$main::unxCmd{chown} $user.$group $ffile; ";
}
} else {
if ($opts{NOCHGRP} == 0) {
$cmd .= "$main::unxCmd{chgrp} $group $ffile; ";
}
}
#
$status = ExecuteCmd($cmd, $opts{VERBOSE}, \*STDERR);
# !! do not exit on error here !!?
# if ($status) { return $status; }
#
$s = &IfPlural($n);
print STDERR "$SCRIPT: RestoreFiles() $n file$s restored under $outdir\n";
#
$cmd = "cd $outdir; ";
$cmd .= "$ls ".join(' ', @files);
$status = ExecuteCmd($cmd, $opts{VERBOSE}, \*STDOUT);
if ($status) { return $status; }
#
}
#
# deleting the archive, unless --no-remove
#
my $msg = '';
$s = '';
if ($parts > 1) { $s = 's'; $msg ="($parts parts) "; }
#
if ($opts{NOREMOVE}) {
$msg .= "not deleted";
if ($opts{VERBOSE}) {
foreach my $archName (@parts) {
$archName =~ s/$opts{SCRATCH}//;
my $size = $archInfos{$archName};
$msg .= " ($opts{SCRATCH}$archName, $size)";
}
}
} else {
$msg .= "deleted";
unlink(@parts);
}
print STDERR "$SCRIPT: RestoreFiles() archive$s #$set $msg\n";
#
}
#
$s = &AddS($nTot);
#
if ($opts{DRYRUN}) {
print STDERR "$SCRIPT: RestoreFiles() $nTot file$s would have been restored\n";
} else {
print STDERR "$SCRIPT: RestoreFiles() $nTot file$s restored\n";
}
#
return 0;
}
#
# ---------------------------------------------------------------------------
#
sub CleanScratch {
#
# remove downloaded archives in scratch that may have not been dowloaded
# still missing
#
my %opts = %{$_[0]};
my $status = 1;
#
if ($opts{VERBOSE}) {
print STDERR "$SCRIPT: CleanScratch()\n";
print STDERR " vault: $opts{VAULT}\n";
print STDERR " scratch: $opts{SCRATCH}\n";
}
#
if (! -d $opts{SCRATCH}) {
print STDERR "$SCRIPT: CleanScratch() invalid --use-vault and --scratch combo\n";
if ($opts{VERBOSE}) {
print STDERR "$SCRIPT: CleanScratch() directory '$opts{SCRATCH}' not found, or not a directory\n";
}
return $status;
}
#
my $listFile = "/tmp/list.$$";
my $cmd = "$main::unxCmd{find} $opts{SCRATCH} -name 'archive*tgz'";
$cmd .= " > $listFile";
#
$status = ExecuteCmd($cmd, $opts{VERBOSE}, \*STDERR);
my @list = GetFileContent($listFile);
unlink($listFile);
#
if ($status) {
print STDERR "$SCRIPT: CleanScratch() '$cmd' failed\n";
return $status;
}
#
my $n = $#list+1;
my $s = ''; if ($n > 1) { $s = 's'; }
if ($n == 0) {
#
print STDERR "$SCRIPT: CleanScratch() no archive found under $opts{SCRATCH}\n";
#
} else {
#
if ($opts{DRYRUN}) {
#
print STDERR "$SCRIPT: CleanScratch() would delete $n archive$s\n";
#
if ($opts{VERBOSE}) {
print STDERR ' ', join("\n ", @list), "\n";
}
#
} else {
#
print STDERR "$SCRIPT: CleanScratch() deleting $n archive$s under $opts{SCRATCH}\n";
if ($opts{VERBOSE}) {
print STDERR ' ', join("\n ", @list), "\n";
}
my $nDel = unlink(@list);
if ($nDel != $n) {
print STDERR "$SCRIPT: CleanScratch() only $nDel deleted\n";
}
#
}
}
#
$status = 0;
return $status;
}
#
# ---------------------------------------------------------------------------
#