1 |
#!/usr/bin/perl |
2 |
|
3 |
# try to recover pc/hostname/backups file from existing transfer logs |
4 |
|
5 |
use strict; |
6 |
#use lib "__INSTALLDIR__/lib"; |
7 |
use lib "/data/backuppc/lib"; |
8 |
|
9 |
use BackupPC::Lib; |
10 |
use BackupPC::FileZIO; |
11 |
|
12 |
use Data::Dumper; |
13 |
|
14 |
my $bpc = BackupPC::Lib->new(undef, undef, 1) or die "BackupPC::Lib->new failed\n"; |
15 |
|
16 |
my $TopDir = $bpc->TopDir(); |
17 |
my %Conf = $bpc->Conf(); |
18 |
|
19 |
my $host = shift @ARGV || die "usage: $0 hostname\n"; |
20 |
|
21 |
my $pc_dir = "$TopDir/pc/$host"; |
22 |
|
23 |
print "working in $pc_dir\n"; |
24 |
|
25 |
sub extract_num($) { |
26 |
my $file = shift || die; |
27 |
my $nr = $1 if ($file =~ m/XferLOG\.(\d+)/); |
28 |
return $nr || die; |
29 |
} |
30 |
|
31 |
my @logs = sort { |
32 |
extract_num($a) <=> extract_num($b) |
33 |
} glob("$pc_dir/XferLOG*"); |
34 |
|
35 |
print "found ", $#logs + 1, " XferLOG files for $host\n"; |
36 |
|
37 |
print "\t",join("\n\t", @logs),"\n"; |
38 |
|
39 |
my $backups_file = "$pc_dir/backups"; |
40 |
|
41 |
if (-e $backups_file) { |
42 |
|
43 |
my @old_backups = $bpc->BackupInfoRead($host); |
44 |
print "found ", $#old_backups + 1, " backups in $pc_dir/backups\n"; |
45 |
|
46 |
#print Dumper(\@old_backups); |
47 |
|
48 |
} else { |
49 |
print "WARNING: backups file $backups_file doesn't exist, creating new one\n"; |
50 |
} |
51 |
|
52 |
foreach my $xfer_log (@logs) { |
53 |
|
54 |
my $num = extract_num($xfer_log); |
55 |
my $type = 'incr'; |
56 |
|
57 |
my $attrib_file = "$pc_dir/$num/attrib"; |
58 |
die "can't find $attrib_file: $!" unless (-e $attrib_file); |
59 |
|
60 |
my $startTime = (stat($attrib_file))[9]; # mtime |
61 |
my $endTime = (stat($attrib_file))[10]; # ctime |
62 |
my $sizeTotal = 0; |
63 |
my $nFilesTotal = 0; |
64 |
my $nFilesExist = 0; |
65 |
my $sizeExist = 0; |
66 |
|
67 |
print "working on $xfer_log $host #$num\n"; |
68 |
|
69 |
my $xfer_fh = BackupPC::FileZIO->open($xfer_log, 0, $Conf{CompressLevel}); |
70 |
while ( my $line = $xfer_fh->readLine() ) { |
71 |
chomp($line); |
72 |
if ($line =~ m#^\s+(create|pool|delete)\s+(.)\s\d\d\d\s+\d+/\d+\s+(\d+)\s(.+)$#) { |
73 |
my ($type,$is_dir,$size,$path) = ($1,$2,$3); |
74 |
#print "$type [$is_dir] $size $path\n"; |
75 |
|
76 |
next if ($is_dir eq 'd'); |
77 |
|
78 |
if ($type eq 'pool') { |
79 |
$sizeExist += $size; |
80 |
$nFilesExist++; |
81 |
} elsif ($type ne 'delete') { |
82 |
$sizeTotal += $size; |
83 |
$nFilesTotal++; |
84 |
} |
85 |
|
86 |
} else { |
87 |
print "#SKIP:$line<-\n"; |
88 |
} |
89 |
} |
90 |
$xfer_fh->close(); |
91 |
|
92 |
my $this_backup = { |
93 |
num => $num, |
94 |
type => $type, |
95 |
startTime => $startTime, |
96 |
endTime => $endTime, |
97 |
size => $sizeTotal, |
98 |
nFiles => $nFilesTotal, |
99 |
xferErrs => 0, # bogus |
100 |
xferBadFile => 0, # bogus |
101 |
xferBadShare => 0, # bogus |
102 |
nFilesExist => $nFilesExist, |
103 |
sizeExist => $sizeExist, |
104 |
sizeExistComp => $sizeExist, # bogus |
105 |
tarErrs => 0, # bogus |
106 |
compress => $Conf{CompressLevel} , |
107 |
noFill => $type eq "incr" ? 1 : 0, |
108 |
level => $type eq "incr" ? 1 : 0, |
109 |
mangle => 1, # name mangling always on for v1.04+ |
110 |
}; |
111 |
|
112 |
print Dumper($this_backup); |
113 |
} |
114 |
|