File: storeBackupCheckSource.pl

package info (click to toggle)
storebackup 3.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,396 kB
  • sloc: perl: 24,988; makefile: 44; sh: 37
file content (515 lines) | stat: -rwxr-xr-x 13,301 bytes parent folder | download
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
#!/usr/bin/env perl

#
#   Copyright (C) Dr. Heinz-Josef Claes (2012-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 POSIX;
use strict;
use warnings;



sub libPath
{
    my $file = shift;

    my $dir;

    # Falls Datei selbst ein symlink ist, solange folgen, bis aufgelöst
    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 'storeBackupLib.pl';

my $checkSumFile = '.md5CheckSums';


=head1 NAME

storeBackupCheckSource.pl - compares unchaged files in source with their
                            md5 sums in the backup

=head1 DESCRIPTION

The tool is intended to find files in the source that might have changed over 
time without the users interaction or knowledge, for example by bit rot.

If a file is unchanged (same ctime, mtime, size) in the source directory
compared to the backup, it:

- prints an ERROR message if the md5 sum differs

- prints a WARNING if permissions, uid or gid differs

- prints MISSING if file is not in the source directory (option -v)

- prints INFO if file is identical (option -v)

=head1 SYNOPSIS

    storeBackupCheckSource.pl -s sourceDir -b singleBackupDir [-v]
	      [-w filePrefix]
	      [--logFile
	       [--plusLogStdout] [--suppressTime] [-m maxFilelen]
	       [[-n noOfOldFiles] | [--saveLogs]]

=head1 OPTIONS

=over 8

=item B<--sourceDir>, B<-s>

    source directory of backup when running storeBackup.pl

=item B<--singleBackupDir>, B<-b>

    directory of the backup to compaire sourceDir with
    this must be *one* single backup directory
    (eg. 2012.08.08_02.00.11)

=item B<--verbose>, B<-v>

    also print positive messages (file is identical in source and backup)

=item B<--wrongFileTables>, B<-w>

    write filenames with detected faults in regular files for
    later bug fixing (not automated)
    parameter to this option is a file prefix
    eg. if the file prefix is '/tmp/bugsS-', the following files are
    generated:
    /tmp/bugsS-files.missing.txt
    /tmp/bugsS-md5sums.wrong.txt

=item B<--logFile>, B<-l>

    logFile, Default: stdout

=item B<--plusLogStdout>

    if you specify a log file with --logFile you can
    additionally print the output to STDOUT with this flag

=item B<--suppressTime>

    suppress output of time in logfile

=item B<--maxFilelen>, B<-m>

    maximal length of log file, default = 1e6

=item B<--noOfOldFiles>, B<-n>

    number of old log files, default = 5

=item B<--saveLogs>

    save log files with date and time instead of deleting the
    old (with [-noOfOldFiles])

=item B<--compressWith>

    compress saved log files (e.g. with 'gzip -9').
    default is 'bzip2'

=back

=head1 COPYRIGHT

Copyright (c) 2012-2022 by Heinz-Josef Claes (see README).
Published under the GNU General Public License v3 or any later version

=cut

my $Help = &::getPod2Text($0);

&printVersion(\@ARGV, '-V', '--version');

my $CheckPar =
    CheckParam->new('-allowLists' => 'no',
		    '-list' => [Option->new('-name' => 'singleBackupDir',
					    '-cl_option' => '-b',
					    '-cl_alias' => '--singleBackupDir',
					    '-param' => 'yes',
					    '-must_be' => 'yes'),
                                Option->new('-name' => 'sourceDir',
					    '-cl_option' => '-s',
					    '-cl_alias' => '--sourceDir',
					    '-param' => 'yes',
					    '-must_be' => 'yes'),
				Option->new('-name' => 'verbose',
					    '-cl_option' => '-v',
					    '-cl_alias' => '--verbose'),
				Option->new('-name' => 'wrongFileTables',
					    '-cl_option' => '-w',
					    '-cl_alias' => '--wrongFileTables',
					    '-param' => 'yes'),
				Option->new('-name' => 'logFile',
					    '-cl_option' => '-l',
					    '-cl_alias' => '--logFile',
					    '-param' => 'yes',
					    '-only_if' => 'not [interactive]'),
				Option->new('-name' => 'plusLogStdout',
					    '-cl_option' => '--plusLogStdout',
					    '-only_if' => '[logFile]'),
				Option->new('-name' => 'suppressTime',
					    '-cl_option' => '--suppressTime'),
				Option->new('-name' => 'maxFilelen',
					    '-cl_option' => '-m',
					    '-cl_alias' => '--maxFilelen',
					    '-default' => 1e6,
					    '-pattern' => '\A[e\d]+\Z',
                                            '-only_if' =>'[logFile]'),
				Option->new('-name' => 'noOfOldFiles',
					    '-cl_option' => '-n',
					    '-cl_alias' => '--noOfOldFiles',
					    '-default' => '5',
					    '-pattern' => '\A\d+\Z',
                                            '-only_if' =>"[logFile]"),
                                Option->new('-name' => 'saveLogs',
					    '-cl_option' => '--saveLogs',
                                            '-default' => 'no',
                                            '-only_if' => '[logFile]'),
                                Option->new('-name' => 'compressWith',
					    '-cl_option' => '--compressWith',
					    '-quoteEval' => 'yes',
                                            '-default' => 'bzip2',
                                            '-only_if' =>'[logFile]'),
# hidden options
# used by storeBackupMount.pl
				Option->new('-name' => 'writeToNamedPipe',
					    '-cl_option' => '--writeToNamedPipe',
					    '-param' => 'yes',
					    '-hidden' => 'yes')
				]);

$CheckPar->check('-argv' => \@ARGV,
                 '-help' => $Help
                 );


my $singleBackupDir = $CheckPar->getOptWithPar('singleBackupDir');
my $sourceDir = $CheckPar->getOptWithPar('sourceDir');
my $verbose = $CheckPar->getOptWithoutPar('verbose');
my $wrongFileTables = $CheckPar->getOptWithPar('wrongFileTables');
my $logFile = $CheckPar->getOptWithPar('logFile');
my $plusLogStdout = $CheckPar->getOptWithoutPar('plusLogStdout');
my $withTime = not $CheckPar->getOptWithoutPar('suppressTime');
$withTime = $withTime ? 'yes' : 'no';
my $maxFilelen = $CheckPar->getOptWithPar('maxFilelen');
my $noOfOldFiles = $CheckPar->getOptWithPar('noOfOldFiles');
my $saveLogs = $CheckPar->getOptWithPar('saveLogs');
$saveLogs = $saveLogs ? 'yes' : 'no';
my $compressWith = $CheckPar->getOptWithPar('compressWith');
# hidden options
my $writeToNamedPipe = $CheckPar->getOptWithPar('writeToNamedPipe');


my (@par) = ();
if (defined $logFile)
{
    push @par, ('-file' => $logFile,
		'-multiprint' => 'yes');
}
else
{
    push @par, ('-filedescriptor', *STDOUT);
}

my ($prLogKind) = ['A:BEGIN',
		   'Z:END',
		   'I:INFO',
		   'V:VERSION',
		   'W:WARNING',
		   'E:ERROR'];
my $printLog = printLog->new('-kind' => $prLogKind,
			     @par,
			     '-withTime' => $withTime,
			     '-maxFilelen' => $maxFilelen,
			     '-noOfOldFiles' => $noOfOldFiles,
			     '-saveLogs' => $saveLogs,
			     '-compressWith' => $compressWith);

my $prLog = printLogMultiple->new('-prLogs' => [$printLog]);

if ($plusLogStdout)
{
    my $p = printLog->new('-kind' => $prLogKind,
			  '-filedescriptor', *STDOUT);
    $prLog->add('-prLogs' => [$p]);
}
if ($writeToNamedPipe)
{
    my $np = printLog->new('-kind' => $prLogKind,
			   '-file' => $writeToNamedPipe,
			   '-maxFilelen' => 0);
    $prLog->add('-prLogs' => [$np]);
}


$prLog->print('-kind' => 'E',
	      '-str' => ["cannot access sourceDir <$sourceDir>"],
	      '-exit' => 1)
    unless -d $sourceDir;

$prLog->print('-kind' => 'E',
	      '-str' => ["this is not a full backup",
			 "please run storeBackupUpdateBackup.pl on <$singleBackupDir>"],
	      '-exit' => 1)
    if -e "$singleBackupDir/.storeBackupLinks/linkFile.bz2";


(@main::cleanup) = ($prLog, undef);
$SIG{INT} = \&cleanup;
$SIG{TERM} = \&cleanup;


$prLog->print('-kind' => 'A',
	      '-str' => ["comparing <$singleBackupDir> with <$sourceDir>"]);
$prLog->print('-kind' => 'V',
	      '-str' => ["storeBackupCheckSource.pl, $main::STOREBACKUPVERSION"]);


my $rcsf = readCheckSumFile->new('-checkSumFile' =>
				 "$singleBackupDir/$checkSumFile",
				 '-prLog' => $prLog);

$main::wft =
    writeBugsToFiles->new('-filePrefix' => $wrongFileTables,
			  '-backupDir' => &::absolutePath($sourceDir),
			  '-prLog' => $prLog,
			  '-md5Missing' => 0);

my ($md5sum, $compr, $devInode, $inodeBackup, $ctime, $mtime, $atime,
    $size, $uid, $gid, $mode, $filename);
while ((($md5sum, $compr, $devInode, $inodeBackup, $ctime, $mtime, $atime,
	 $size, $uid, $gid, $mode, $filename) = $rcsf->nextLine()) > 0)
{
    if ($devInode =~ /\ADEVICE/)
    {
	$prLog->print('-kind' => 'I',
		      '-str' => ["ignoring stored device <$filename>"])
	    if $verbose;
	next;
    }

    my $f = "$sourceDir/$filename";
    if (length($md5sum) == 32)   # normal file
    {
	# check if it exists with same time stamp in sourceDir
	if (-e $f)
	{
	    unless (-r $f)
	    {
		$prLog->print('-kind' => 'E',
			      '-str' => ["file <$f> exists but is unreadable"]);
		$main::wft->print(&::absolutePath($f), 'fileMissing');

		next;
	    }
	    my ($actMode, $actUid, $actGid, $actCtime, $actMtime,
		$actAtime, $actSize) =
		    (stat($f))[2, 4, 5, 10, 9, 8, 7];
	    $actMode = 0 unless $actMode;
	    $actMode &= 07777;

	    if ($ctime == $actCtime and $mtime == $actMtime and
		$size == $actSize)
	    {
		my $actMd5 = &::calcMD5($f, $prLog);

		unless ($actMd5)
		{
		    $prLog->print('-kind' => 'E',
				  '-str' => ["cannot calculate md5 sum of <$f>"]);
		    next;
		}
		if ($md5sum eq $actMd5)   # looks good
		{
		    $prLog->print('-kind' => 'I',
				  '-str' => ["<$f> identical to backup"])
			if $verbose;

		    $prLog->print('-kind' => 'W',
				  '-str' =>
				  ["<$f> has same md5 sum but different permissions " .
				   "than in backup"])
			if ($mode ne $actMode);
		    $prLog->print('-kind' => 'W',
				  '-str' =>
				  ["<$f> has same md5 sum but different uid " .
				   "than in backup"])
			if ($uid ne $actUid);
		    $prLog->print('-kind' => 'W',
				  '-str' =>
				  ["<$f> has same md5 sum but different gid " .
				   "than in backup"])
			if ($gid ne $actGid);
		}
		else
		{
		    $prLog->print('-kind' => 'E',
				  '-str' =>
				  ["<$f> has same ctime / mtime / size " .
				   "as in backup but different md5 sums: $actMd5 in " .
				   "sourceDir ; $md5sum in backup"]);
		    $main::wft->print(&::absolutePath($f), 'md5Wrong');
		}
	    }
	    else
	    {
		my $actMd5 = ($size == $actSize) ? &::calcMD5($f, $prLog) : 0;
		if ($md5sum eq $actMd5)
		{
		    $prLog->print('-kind' => 'I',
				  '-str' =>
				  ["<$f> has different ctime / mtime size " .
				   "but same md5 sum"])
			if $verbose;
		}
		else
		{
		    $prLog->print('-kind' => 'I',
				  '-str' => ["<$f> differs from backup"])
			if $verbose
		}
	    }
	}
	else
	{
	    $prLog->print('-kind' => 'M',
			  '-str' =>
			  ["<$filename> is missing in the source directory"])
		if $verbose;
	}
    }
}

my $enc = $prLog->encountered('-kind' => 'W');
my $S = $enc > 1 ? 'S' : '';
if ($enc)
{
    $prLog->print('-kind' => 'W',
		  '-str' => ["-- $enc WARNING$S OCCURRED DURING THE CHECK! --"])
}
else
{
    $prLog->print('-kind' => 'I',
		  '-str' => ["-- no WARNINGS OCCURRED DURING THE CHECK! --"]);
}

$enc = $prLog->encountered('-kind' => 'E');
$S = $enc > 1 ? 'S' : '';
if ($enc)
{
    $prLog->print('-kind' => 'E',
		  '-str' => ["-- $enc ERROR$S OCCURRED DURING THE CHECK! --"]);
}
else
{
    $prLog->print('-kind' => 'I',
		  '-str' => ["-- no ERRORS OCCURRED DURING THE CHECK! --"]);
}

$prLog->print('-kind' => 'Z',
	      '-str' => ["comparing <$singleBackupDir> to <$sourceDir>"]);


if ($prLog->encountered('-kind' => "E"))
{
    exit 1;
}
else
{
    exit 0;
}

exit 0;


##################################################
# package printLogMultiple needs this function
sub cleanup
{
    my $signame = shift;
    my $exit = (shift() || 1);

    exit $exit;
}