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
|
#! /usr/bin/env perl
#
# Copyright (C) Dr. Heinz-Josef Claes (2008-2022)
# hjclaes@web.de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
$main::STOREBACKUPVERSION = undef;
use strict;
sub libPath
{
my $file = shift;
my $dir;
# Falls Datei selbst ein symlink ist, solange folgen, bis aufgelst
if (-f $file)
{
while (-l $file)
{
my $link = readlink($file);
if (substr($link, 0, 1) ne "/")
{
$file =~ s/[^\/]+$/$link/;
}
else
{
$file = $link;
}
}
($dir, $file) = &splitFileDir($file);
$file = "/$file";
}
else
{
print STDERR "<$file> does not exist!\n";
exit 1;
}
$dir .= "/../lib"; # Pfad zu den Bibliotheken
my $oldDir = `/bin/pwd`;
chomp $oldDir;
if (chdir $dir)
{
my $absDir = `/bin/pwd`;
chop $absDir;
chdir $oldDir;
return (&splitFileDir("$absDir$file"));
}
else
{
print STDERR "<$dir> does not exist, exiting\n";
}
}
sub splitFileDir
{
my $name = shift;
return ('.', $name) unless ($name =~/\//); # nur einfacher Dateiname
my ($dir, $file) = $name =~ /^(.*)\/(.*)$/s;
$dir = '/' if ($dir eq ''); # gilt, falls z.B. /filename
return ($dir, $file);
}
my ($req, $prog) = &libPath($0);
unshift @INC, "$req";
require 'checkParam2.pl';
require 'checkObjPar.pl';
require 'prLog.pl';
require 'version.pl';
require 'fileDir.pl';
require 'forkProc.pl';
require 'humanRead.pl';
require 'dateTools.pl';
require 'evalTools.pl';
require 'storeBackupLib.pl';
my $checkSumFile = '.md5CheckSums';
my $tmpdir = '/tmp'; # default value
$tmpdir = $ENV{'TMPDIR'} if defined $ENV{'TMPDIR'};
=head1 NAME
storeBackupSearch.pl - locates different versions of a file saved with storeBackup.pl.
=head1 SYNOPSIS
storeBackupSearch.pl -g configFile
storeBackupSearch.pl -b backupDirDir [-f configFile]
[-s rule] [--absPath] [-w file] [--parJobs number]
[-d level] [--once] [--print] [-T tmpdir] [backupRoot . . .]
=head1 DESCRIPTION
You need some basic understanding of linux and perl to use it.
=head1 OPTIONS
=over 8
=item B<--generate>, B<-g>
generate a config file
=item B<--print>
print configuration read from configuration file and stop
=item B<--configFile>, B<-f>
configuration file (instead of or
additionally to parameters)
=item B<--backupDir> B<-b>
top level directory of all backups
=item B<--searchRule>, B<-s>
rule for searching
see README: 'including / excluding files and directories'
=item B<--absPath>, B<-a>
write result with absolute path names
=item B<--writeToFile>, B<-w>
write search result also to file
=item B<--parJobs>, B<-p>
number of parallel jobs, default = chosen automatically
=item B<--debug>, B<-d>
debug level, possible values are 0, 1, 2, default = 0
=item B<--once>, B<-o>
show every file found only once (depending on md5 sum)
=item B<--tmpdir>, B<-T>
directory for temporary files, default is </tmp>
=item backupRoot
Root directories of backups where to search relative
to backupDir. If no directories are specified, all
backups below backupDir are chosen.
=back
=head1 COPYRIGHT
Copyright (c) 2008-2022 by Heinz-Josef Claes (see README).
Published under the GNU General Public License v3 or any later version
=cut
my $Help = &::getPod2Text($0);
my $templateConfigFile = <<EOC;
# configuration file for storeBackupSearch.pl, version $main::STOREBACKUPVERSION
# You can set a value specified with '-cf_key' (eg. logFiles) and
# continue at the next lines which have to begin with a white space:
# logFiles = /var/log/messages /var/log/cups/access_log
# /var/log/cups/error_log
# One ore more white spaces are interpreted as separators.
# You can use single quotes or double quotes to group strings
# together, eg. if you have a filename with a blank in its name:
# logFiles = '/var/log/my strage log'
# will result in one filename, not in three.
# If an option should have *no value*, write:
# logFiles =
# If you want the default value, uncomment it:
# #logFile =
# You can also use environment variables, like \$XXX or \${XXX} like in
# a shell. Single quotes will mask environment variables, while double
# quotes will not.
# You can mask \$, {, }, ", ' with a backslash (\\), eg. \\\$
# Lines beginning with a '#' or ';' are ignored (use this for comments)
#
# You can overwrite settings in the command line. You can remove
# the setting also in the command by using the --unset feature, eg.:
# '--unset doNotDelete' or '--unset --doNotDelete'
# *** param must exist ***
# top level directory of all linked backups
;backupDir=
# *** param must exist ***
# rule for searching
# !!! see README file 'including / excluding files and directories'
# EXAMPLE:
# searchRule = ( '\$size > &::SIZE("3M")' and '\$uid eq "hjc"' ) or
# ( '\$mtime > &::DATE("3d4h")' and not '\$file =~ m#/tmp/#' )'
;searchRule=
# root directory of backup relative to backupDir directory
;backupRoot=
# directory for temporary file, default is /tmp
;tmpDir=
# write result with absolute path names
# default is 'no', possible values are 'yes' and 'no'
;absPath=
# write search result also to file
;writeToFile=
# number of parallel jobs, default = chosen automatically
;parJobs=
# debug level, possible values are 0, 1, 2, default = 0
;debug=
# show every found file only once (depending on md5 sum)
# default is 'no', possible values are 'yes' and 'no'
;once=
EOC
;
&printVersion(\@ARGV, '-V', '--version');
my $CheckPar =
CheckParam->new('-configFile' => '-f',
'-allowLists' => 'yes',
# '-listMapping' => 'backupRoot',
'-list' => [Option->new('-name' => 'configFile',
'-cl_option' => '-f',
'-cl_alias' => '--configFile',
'-param' => 'yes'),
Option->new('-name' => 'generate',
'-cl_option' => '-g',
'-cl_alias' => '--generate',
'-param' => 'yes',
'-only_if' =>
'not [configFile] and not [backupDir] and not [backupRoot] and not [searchRule]'),
Option->new('-name' => 'backupDir',
'-cl_option' => '-b',
'-cl_alias' => '--backupDir',
'-cf_key' => 'backupDir',
'-param' => 'yes'),
Option->new('-name' => 'searchRule',
'-cl_option' => '-s',
'-cl_alias' => '--searchRule',
'-cf_key' => 'searchRule',
'-param' => 'yes',
'-quoteEval' => 'yes'),
Option->new('-name' => 'writeAbsPath',
'-cl_option' => '-a',
'-cl_alias' => '--absPath',
'-cf_key' => 'absPath',
'-cf_noOptSet' => ['yes', 'no']),
Option->new('-name' => 'writeToFile',
'-cl_option' => '-w',
'-cl_alias' => '--writeToFile',
'-cf_key' => 'writeToFile',
'-param' => 'yes'),
Option->new('-name' => 'parJobs',
'-cl_option' => '-p',
'-cl_alias' => '--parJobs',
'-cf_key' => 'parJobs',
'-param' => 'yes',
'-pattern' => '\A[1-9]\d*\Z'),
Option->new('-name' => 'debug',
'-cl_option' => '-d',
'-cl_alias' => '--debug',
'-cf_key' => 'debug',
'-default' => 0,
'-pattern' => '\A[012]\Z'),
Option->new('-name' => 'once',
'-cl_option' => '-o',
'-cl_alias' => '--once',
'-cf_key' => 'once',
'-cf_noOptSet' => ['yes', 'no']),
Option->new('-name' => 'print',
'-cl_option' => '--print'),
Option->new('-name' => 'tmpdir',
'-cl_option' => '-T',
'-cl_alias' => '--tmpdir',
'-cf_key' => 'tmpDir',
'-default' => $tmpdir),
Option->new('-name' => 'backupRoot',
'-cl_option' => '--backupRoot',
'-cf_key' => 'backupRoot',
'-param' => 'yes'),
# hidden options
Option->new('-name' => 'printAll',
'-cl_option' => '--printAll',
'-hidden' => 'yes'),
Option->new('-name' => 'readNoLines',
'-cl_option' => '--readNoLines',
'-cf_key' => 'readNoLines',
'-hidden' => 'yes',
'-default' => 20000)
]
);
$CheckPar->check('-argv' => \@ARGV,
'-help' => $Help
);
# Auswertung der Parameter
my $configFile = $CheckPar->getOptWithPar('configFile');
my $generateConfigFile = $CheckPar->getOptWithPar('generate');
my $print = $CheckPar->getOptWithoutPar('print');
my $backupDir = $CheckPar->getOptWithPar('backupDir');
my $writeToFile = $CheckPar->getOptWithPar('writeToFile');
my $searchRule = $CheckPar->getOptWithPar('searchRule'); # vector
my $writeAbsPath = $CheckPar->getOptWithoutPar('writeAbsPath');
my $parJobs = $CheckPar->getOptWithPar('parJobs');
my $debug = $CheckPar->getOptWithPar('debug');
my $once = $CheckPar->getOptWithoutPar('once');
$tmpdir = $CheckPar->getOptWithPar('tmpdir');
my (@backupRoot) = ($CheckPar->getOptWithPar('backupRoot'));
my $printAll = $CheckPar->getOptWithoutPar('printAll');
$print = 1 if $printAll;
my $readNoLines = $CheckPar->getOptWithPar('readNoLines');
unless ($parJobs)
{
local *FILE;
if (open(FILE, "/proc/cpuinfo"))
{
my $l;
$parJobs = 1;
while ($l = <FILE>)
{
$parJobs++ if $l =~ /processor/;
}
close(FILE);
}
$parJobs = 2 if $parJobs < 2;
}
if ($generateConfigFile)
{
my $answer = 'yes';
if (-e $generateConfigFile)
{
do
{
print "<$generateConfigFile> already exists. Overwrite?\n",
"yes / no -> ";
$answer = <STDIN>;
chomp $answer;
} while ($answer ne 'yes' and $answer ne 'no');
}
exit 0 if $answer eq 'no';
local *FILE;
open(FILE, "> $generateConfigFile") or
die "could not write to <$generateConfigFile>";
print FILE $templateConfigFile;
close(FILE);
exit 0;
}
if ($print)
{
$CheckPar->print('-showHidden' => $printAll);
}
my $prLog = printLog->new('-kind' => ['I:INFO', 'W:WARNING', 'E:ERROR',
'S:STATISTIC', 'D:DEBUG', 'V:VERSION'],
'-tmpdir' => $tmpdir);
$main::__prLog = $prLog; # used in rules
$prLog->fork($req);
$prLog->print('-kind' => 'V',
'-str' => ["storeBackupSearch.pl, $main::STOREBACKUPVERSION"]);
$prLog->print('-kind' => 'E',
'-str' => ["missing parameters backupDir and searchRule\n$Help"],
'-exit' => 1)
unless defined $backupDir and defined $searchRule;
$prLog->print('-kind' => 'E',
'-str' => ["missing parameter backupDir\n$Help"],
'-exit' => 1)
unless defined $backupDir;
$prLog->print('-kind' => 'E',
'-str' => ["backupDir directory <$backupDir> does not exist " .
"or is not accesible"],
'-exit' => 1)
unless -r $backupDir;
$prLog->print('-kind' => 'E',
'-str' => ["missing parameter searchRule\n$Help"],
'-exit' => 1)
unless defined $searchRule;
my $sRule = evalInodeRule->new('-line' => $searchRule,
'-keyName' => 'search',
'-debug' => $debug,
'-prLog' => $prLog);
$prLog->print('-kind' => 'I',
'-str' => ["searching with rule", ' ' .
join(' ', @{$sRule->getLine()})]);
if ($print)
{
exit 0;
}
my $allLinks = lateLinks->new('-dirs' => [$backupDir],
'-kind' => 'recursiveSearch',
'-verbose' => 0,
'-prLog' => $prLog);
my $allStbuDirs = $allLinks->getAllStoreBackupDirs();
# filter the relevant backups
my (@dirsToSearch) = ();
if (@backupRoot)
{
my $d;
foreach $d (@backupRoot)
{
unless ($d =~ m#\A/#)
{
$d = "$backupDir/$d";
}
$prLog->print('-kind' => 'E',
'-str' => ["directory <$d> does not exist " .
"or is not accesible"],
'-exit' => 1)
unless -r $d;
$d = &::absolutePath($d);
$prLog->print('-kind' => 'E',
'-str' => ["directory <$d> is not a subdirectory " .
"of backupDir <$backupDir>"],
'-exit' => 1)
unless &::isSubDir($backupDir, $d);
# now get all dirs from @$allStbuDirs below $d
my $a;
foreach $a (@$allStbuDirs)
{
push @dirsToSearch, $a
if $a =~ /\A$d\//s or $a =~ /\A$d\z/s;
}
}
(@dirsToSearch) = sort { $a cmp $b } @dirsToSearch;
}
else
{
(@dirsToSearch) = sort { $a cmp $b } @$allStbuDirs;
}
$prLog->print('-kind' => 'E',
'-str' => ["nothing to search, no backup directories specified"],
'-exit' => 1)
unless @dirsToSearch;
{
my (@out, $d);
foreach $d (@dirsToSearch)
{
push @out, " $d";
}
$prLog->print('-kind' => 'I',
'-str' => ["backup directories to search", @out]);
}
my $parFork = parallelFork->new('-maxParallel' => $parJobs,
'-prLog' => $prLog);
my $tinySched = tinyWaitScheduler->new('-prLog' => $prLog);
#
# search through all directories in @dirsToSearch
#
local *FILE;
open(FILE, "> $writeToFile") or
$prLog->print('-kind' => 'E',
'-str' => ["cannot open <$writeToFile> for writing"],
'-exit' => 1)
if $writeToFile;
my ($dirToSearch, %once, $ne, $nb, $s);
foreach $dirToSearch (@dirsToSearch)
{
unless (-r "$dirToSearch/$checkSumFile" or
-r "$dirToSearch/$checkSumFile.bz2")
{
$prLog->print('-kind' => 'E',
'-str' => ["no readable <$checkSumFile> in " .
"<$dirToSearch> ... skipping"]);
next;
}
# if (-f "$dirToSearch/$checkSumFile.notFinished")
unless (&::checkIfBackupWasFinished('-backupDir' => "$dirToSearch",
'-prLog' => $prLog,
'-count' => 40))
{
$prLog->print('-kind' => 'E',
'-str' => ["backup <$dirToSearch> not finished" .
" ... skipping"]);
next;
}
$nb++;
$s = "=== searching in <$dirToSearch>:\n";
print $s;
print FILE $s if $writeToFile and not $writeAbsPath;
my $rcsf =
readCheckSumFile->new('-checkSumFile' => "$dirToSearch/$checkSumFile",
'-prLog' => $prLog,
'-tmpdir' => $tmpdir);
# my $meta = $rcsf->getMetaValField();
# my $postfix = ($$meta{'postfix'})->[0]; # postfix for compression
my $postfix = $rcsf->getInfoWithPar('postfix');
my $jobToDo = 1;
my $parForkToDo = 1;
while ($jobToDo > 0 or $parForkToDo > 0)
{
#
# check for jobs done
#
my $old = $parFork->checkOne();
if ($old)
{
my $tmpName = $old->get('-what' => 'info');
local *IN;
open(IN, $tmpName) or
$prLog->print('-kind' => 'E',
'-str' => ["cannot open temporary file <$tmpName>"],
'-exit' => 1);
my $l;
while ($l = <IN>)
{
chop $l;
my ($md5sum, $size, $mode, $ctime, $mtime, $uid, $gid,
$filename) = split(/\s+/, $l, 8);
if ($once)
{
next if exists $once{$md5sum};
$once{$md5sum} = 1;
}
$filename =~ s/\\0A/\n/og; # restore '\n'
$filename =~ s/\\5C/\\/og; # restore '\\'
if ($writeAbsPath)
{
$s = "$dirToSearch/$filename\n";
}
else
{
$s = "$filename\n";
}
print $s;
print FILE $s if $writeToFile;
}
close(IN);
unlink $tmpName;
}
#
# start a new job
#
if ($jobToDo > 0 and $parFork->getNoFreeEntries() > 0)
{
my (@lineBuffer, $i);
my $done = 0;
# read $readNoLines lines
for ($done = $i = 0 ; $i < $readNoLines ; $i++)
{
my $l = $rcsf->nextBinLine();
unless ($l)
{
$done = 1;
last;
}
$ne++;
push @lineBuffer, $l;
}
$jobToDo = @lineBuffer;
my $tmpName = &::uniqFileName("/$tmpdir/storeBackupSearch-");
if ($jobToDo)
{
$parFork->add_noblock('-function' => \&checkRule,
'-funcPar' =>
[$sRule, \@lineBuffer, $prLog, $checkSumFile,
$tmpName],
'-info' => $tmpName);
$tinySched->reset();
}
}
#
# wait
#
$tinySched->wait();
$parForkToDo = $parFork->getNoUsedEntries();
}
}
close(FILE) or
$prLog->print('-kind' => 'E',
'-str' => ["cannot close <$writeToFile>"],
'-exit' => 1)
if $writeToFile;
my $s = '';
$s = ", skipped " . scalar @dirsToSearch - $nb . " backup(s)"
if @dirsToSearch > $nb;
$prLog->print('-kind' => 'I',
'-str' => ["checked $ne entries in $nb backups$s"]);
exit 0;
########################################
sub checkRule
{
my $sRule = shift;
my $listOfFiles = shift;
my $prLog = shift;
my $checkSumFile = shift;
my $tmpfile = shift;
local *OUT;
unless (open(OUT, "> $tmpfile"))
{
$prLog->print('-kind' => 'E',
'-str' => ["cannot open temporary file <$tmpfile>"]);
return 1; # ERROR
}
my ($l);
my (%type) = ('dir' => 'd',
'symlink' => 'l',
'pipe' => 'p',
'socket' => 's',
'blockdev' => 'b',
'chardev' => 'c');
my ($md5sum, $compr, $devInode, $inodeBackup, $ctime, $mtime, $atime,
$size, $uid, $gid, $mode, $filename);
foreach $l (@$listOfFiles)
{
my (@ret) = readCheckSumFile::evalBinLine($l, $prLog, $checkSumFile);
next if @ret != 12;
($md5sum, $compr, $devInode, $inodeBackup, $ctime, $mtime, $atime,
$size, $uid, $gid, $mode, $filename) = @ret;
my $type = 'f';
$type = $type{$md5sum} if exists $type{$md5sum};
if ($sRule->checkRule($filename, $size, $mode, $ctime, $mtime, $uid,
$gid, $type) == 1)
{
$filename =~ s/\\/\\5C/og; # '\\' stored as \5C
$filename =~ s/\n/\\0A/sog; # '\n' stored as \0A
print OUT "$md5sum $size $mode $ctime $mtime $uid $gid $filename\n";
}
}
close(OUT);
return 0;
}
|