File: storeBackupConvertBackup.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 (444 lines) | stat: -rwxr-xr-x 10,821 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
#! /usr/bin/env perl

#
#   Copyright (C) Dr. Heinz-Josef Claes (2002-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/>.
#


use IO::Handle;
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 'version.pl';
require 'fileDir.pl';

my $md5CheckSums = '.md5CheckSums';

=head1 NAME

storeBackupConvertBackup.pl - converts old backups created with storeBackup.pl to the newest version,

=head1 SYNOPSIS

storeBackupConvertBackup.pl storeBackup-dir

=head1 DESCRIPTION

This program converts old backups created with storeBackup.pl to the newest version,
currently version 1.3.
you can see the version by typing:

head -1 < ...<storeBackupDir>/date_time/.md5CheckSums.info

or if that file does not exist:

bzip2 -d < ...<storeBackupDir>/date_time/.md5CheckSums.bz2 | head -1

(if you do not see '###version=...', it is version 1.0

=head1 COPYRIGHT

Copyright (c) 2002-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');

die $Help if (@ARGV != 1);

my $dir = shift @ARGV;
die "directory <$dir> does not exist" unless (-d $dir);

opendir(DIR, $dir) or
    die "cannot open <$dir>";
my ($entry, @entries);
while ($entry = readdir DIR)
{
    my $e = "$dir/$entry";
    next if (-l $e and not -d $e);
    push @entries, $entry;
}
closedir(DIR);

my $flag = 0;
my $i = 1;
foreach $entry (sort @entries)
{
    next unless $entry =~
	/\A(\d{4})\.(\d{2})\.(\d{2})_(\d{2})\.(\d{2})\.(\d{2})\Z/o;

    $flag = 1;   # irgendein directory gefunden

    my $e = "$dir/$entry";
    my $compress = 0;

againForNextVersion:
    if (-f "$e/$md5CheckSums.info") # ab Version 1.2
    {
	if (-f "$e/$md5CheckSums.bz2")  # komprimierte Version liegt vor, nehmen
	{
	    $compress = 1;
	}
	elsif (-f "$e/$md5CheckSums")
	{
	    $compress = 0;
	}
	else
	{
	    print "cannot open <$e/$md5CheckSums\[.bz2\]>\n";
	    next;
	}

	open(INFO, "$e/$md5CheckSums.info") or
	    die "cannot open <$e/$md5CheckSums.info";
	my $v;
	my $l = <INFO>;
	chop $l;
	if ($l =~ /^version=(\S+)/)
	{
	    $v = $1;
	}
	else
	{
	    print "cannot find version information in $e/$md5CheckSums.info\n";
	    next;
	}
	if ($v eq '1.3')
	{
	    print "$entry: version <$v> => ok.\n";
	}
	elsif ($v eq '1.2')
	{
	    print "$entry: version <$v> converting to 1.3 ...";
	    STDOUT->autoflush(1);
	    my ($uid, $gid, $mode);
	    if ($compress == 1)
	    {
		($uid, $gid, $mode) = (stat("$e/$md5CheckSums.bz2"))[4, 5, 2];
		unlink "$e/$md5CheckSums.new.bz2", "$e/$md5CheckSums.info";
		open(FILE, "bzip2 -d < \'$e/$md5CheckSums.bz2\' |") or
		    die "cannot open $e/$md5CheckSums.bz2";
		open(NEW, "| bzip2 > \'$e/$md5CheckSums.new.bz2\'") or
		    die "cannot bzip2 > $e/$md5CheckSums.new.bz2";
	    }
	    else
	    {
		($uid, $gid, $mode) = (stat("$e/$md5CheckSums"))[4, 5, 2];
		unlink "$e/$md5CheckSums.new", "$e/$md5CheckSums.info";
		open(FILE, "< $e/$md5CheckSums") or
		    die "cannot open $e/$md5CheckSums";
		open(NEW, "> $e/$md5CheckSums.new") or
		    die "cannot write $e/$md5CheckSums.new";
	    }
	    $mode &= 07777;
	    $l = <FILE>;     # erste Zeile berlesen
	    print NEW "# contents/md5 compr dev-inode inodeBackup ctime mtime atime size uid gid mode filename\n";

	    open(INFO_NEW, "> $e/$md5CheckSums.info.new") or
		die "cannot write $e/$md5CheckSums.info.new";

	    while ($l = <FILE>)
	    {
		chop $l;
		my ($md5sum, $compr, $devInode, $inodeBackup, $ctime, $mtime, $size,
		    $uid, $gid, $mode, $filename);
		my $d = '[\d-]';      # \d und Minuszeichen
		my $n = ($md5sum, $compr, $devInode, $inodeBackup, $ctime,
			 $mtime, $size, $uid, $gid, $mode, $filename) =
			     $l =~ /^(\w+)\s+(\w+)\s+(\S+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+(.*)/o;
		if ($n != 11)
		{
		    print "cannot read line: <$l>\n";
		}

		my $atime = $mtime;
		print NEW "$md5sum $compr $devInode $inodeBackup $ctime " .
		    "$mtime $atime $size $uid $gid $mode $filename\n";
	    }
	    close(FILE);
	    close(NEW);
	    print INFO_NEW "version=1.3\n";
	    my (@l) = <INFO>;
	    print INFO_NEW "@l";
	    close(INFO_NEW);
	    print "\n";
	    if ($compress == 1)
	    {
		unlink "$e/$md5CheckSums.bz2";
		rename "$e/$md5CheckSums.new.bz2", "$e/$md5CheckSums.bz2";
		chown $uid, $gid, "$e/$md5CheckSums.bz2";
		chmod $mode, "$e/$md5CheckSums.bz2";
	    }
	    else
	    {
		unlink "$e/$md5CheckSums";
		rename "$e/$md5CheckSums.new", "$e/$md5CheckSums";
		chown $uid, $gid, "$e/$md5CheckSums";
		chmod $mode, "$e/$md5CheckSums";
	    }
	    mkdir "$e/.storeBackupLinks", 0777;
	    unlink "$e/$md5CheckSums.info";
	    rename "$e/$md5CheckSums.info.new", "$e/$md5CheckSums.info";
	}
	else
	{
	    print "$entry: unsupported version <$v>\n";
	}
	close(INFO);
    }
    else                            # Version 1.0 und 1.1
    {
	if (-f "$e/$md5CheckSums.bz2")  # komprimierte Version liegt vor, nehmen
	{
	    open(FILE, "bzip2 -d < \'$e/$md5CheckSums.bz2\' |");
	    $compress = 1;
	}
	elsif (-f $e/$md5CheckSums)
	{
	    open(FILE, "< $e/$md5CheckSums");
	}
	else
	{
	    print "cannot open <$e/$md5CheckSums\[.bz2\]>\n";
	    next;
	}
	my $v;
	my $l = <FILE>;
	chop $l;
	if ($l =~ /^###version=(.*)/)
	{
	    $v = $1;
	}
	else
	{
	    $v = '1.0';
	}

	if ($v eq '1.0')
	{
	    print "$entry: version <$v> converting to 1.1 ...";
	    STDOUT->autoflush(1);
	    if ($compress == 1)
	    {
		unlink "$e/$md5CheckSums.new.bz2";
		open(NEW, "| bzip2 > \'$e/$md5CheckSums.new.bz2\'") or
		    die "cannot bzip2 > $e/$md5CheckSums.new.bz2";
	    }
	    else
	    {
		unlink "$e/$md5CheckSums.new";
		open(NEW, "> $e/$md5CheckSums.new") or
		    die "cannot write $e/$md5CheckSums.new";
	    }
	    print NEW "###version=1.1\n";
	    print NEW "###exceptDirsSep=,\n";
	    print NEW "###exceptDirs=\n";

	    my $i = 0;    # Zhler, der inodes simuliert
	    while ($l = <FILE>)
	    {
		if ($l =~ /^###/)
		{
		    print NEW $l;
		    next;
		}

		$i++;
		chop $l;
		my ($md5sum, $compr, $ctime, $mtime, $size, $uid, $gid,
		    $mode, $filename);
		my $d = '[\d-]';      # \d und Minuszeichen
		my $n = ($md5sum, $compr, $ctime, $mtime, $size, $uid,
			 $gid, $mode, $filename) =
		$l =~ /^(\w+)\s+(\w+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+(.*)/o;
		print NEW
		    "$md5sum $compr 1-$i $ctime $mtime $size $uid $gid $mode $filename\n";
	    }
	    close(NEW);
	    if ($compress == 1)
	    {
		unlink "$e/$md5CheckSums.bz2";
		rename "$e/$md5CheckSums.new.bz2", "$e/$md5CheckSums.bz2";
		chmod 0600, "$e/$md5CheckSums.bz2";
	    }
	    else
	    {
		unlink "$e/$md5CheckSums";
		rename "$e/$md5CheckSums.new", "$e/$md5CheckSums";
		chmod 0600, "$e/$md5CheckSums";
	    }
	    print " ok\n";

	    goto againForNextVersion;
	}
	elsif ($v eq '1.1')
	{
	    print "$entry: version <$v> converting to 1.2 ...";
	    STDOUT->autoflush(1);
	    if ($compress == 1)
	    {
		unlink "$e/$md5CheckSums.new.bz2", "$e/$md5CheckSums.info";
		open(NEW, "| bzip2 > \'$e/$md5CheckSums.new.bz2\'") or
		    die "cannot bzip2 > $e/$md5CheckSums.new.bz2";
	    }
	    else
	    {
		unlink "$e/$md5CheckSums.new", "$e/$md5CheckSums.info";
		open(NEW, "> $e/$md5CheckSums.new") or
		    die "cannot write $e/$md5CheckSums.new";
	    }
	    open(INFO, "> $e/$md5CheckSums.info") or
		die "cannot write $e/$md5CheckSums.info";

	    print INFO "version=1.2\n";

	    my $postfix;
	    while ($l = <FILE>)
	    {
		if ($l =~ /^### /)   # Spaltenverzeichnis
		{
		    print NEW "# contents/md5 compr dev-inode inodeBackup " .
			"ctime mtime size uid gid mode filename\n";
		    next;
		}
		elsif ($l =~ /^###(.*?)=(.*)/)
		{
		    print INFO "$1=$2\n";
		    $postfix = $2 if ($1 eq 'postfix');
		    next;
		}

		chop $l;
		my ($md5sum, $compr, $devInode, $ctime, $mtime, $size, $uid, $gid,
		    $mode, $filename);
		my $d = '[\d-]';      # \d und Minuszeichen
		my $n = ($md5sum, $compr, $devInode, $ctime, $mtime, $size, $uid,
			 $gid, $mode, $filename) =
		$l =~ /^(\w+)\s+(\w+)\s+(\S+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+($d+)\s+(.*)/o;
		# Inode im Backup ermitteln
		my $inodeBackup;
		my $f = "$e/$filename";
		$f =~ s/\\0A/\n/og;    # '\n' wiederherstellen
		$f =~ s/\\5C/\\/og;    # '\\' wiederherstellen

		$f .= $postfix if ($compr eq 'c');
		if ($md5sum eq 'symlink')
		{
		    $inodeBackup = (lstat($f))[1];
		}
		else
		{
		    $inodeBackup = (stat($f))[1];
		}

		print NEW
		    "$md5sum $compr $devInode $inodeBackup $ctime $mtime " .
		       "$size $uid $gid $mode $filename\n";
	    }
	    close(NEW);
	    close(INFO);
	    chmod 0600, "$e$md5CheckSums.info";
	    if ($compress == 1)
	    {
		unlink "$e/$md5CheckSums.bz2";
		rename "$e/$md5CheckSums.new.bz2", "$e/$md5CheckSums.bz2";
		chmod 0600, "$e/$md5CheckSums.bz2";
	    }
	    else
	    {
		unlink "$e/$md5CheckSums";
		rename "$e/$md5CheckSums.new", "$e/$md5CheckSums";
		chmod 0600, "$e/$md5CheckSums";
	    }
	    print " ok\n";
	    close(FILE);
	}
	else
	{
	    print "$entry: unsupported version <$v>\n";
	}
    }
}


print "ERROR: no backup directories found\n"
    unless ($flag);