r10317@llin: dpavlin | 2006-03-07 17:24:19 +0100
[BackupPC.git] / bin / BackupPC_recover_meta
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 "no file?";
27         my $nr;
28         if ($file =~ m/XferLOG\.(\d+)/) {
29                 $nr = $1;
30         } else {
31                 die "can't extract number from $file";
32         }
33         return $nr;
34 }
35
36 my @logs = sort {
37         extract_num($a) <=> extract_num($b)
38 } glob("$pc_dir/XferLOG.[0-9]*");
39
40 print "found ", $#logs + 1, " XferLOG files for $host\n";
41
42 print "\t",join("\n\t", @logs),"\n";
43
44 my $backups_file = "$pc_dir/backups";
45
46 if (-e $backups_file) {
47
48         my @old_backups = $bpc->BackupInfoRead($host);
49         print "found ", $#old_backups + 1, " backups in $pc_dir/backups\n";
50
51         #print Dumper(\@old_backups);
52
53 } else {
54         print "WARNING: backups file $backups_file doesn't exist, creating new one\n";
55 }
56
57 my @new_backups;
58
59 foreach my $xfer_log (@logs) {
60
61         my $num = extract_num($xfer_log);
62         my $type = 'incr';
63
64         my $attrib_file = "$pc_dir/$num/attrib";
65         die "can't find $attrib_file: $!" unless (-e $attrib_file);
66
67         my $startTime = (stat($attrib_file))[9]; # mtime
68         my $endTime = (stat($attrib_file))[10]; # ctime
69         my $sizeTotal = 0;
70         my $nFilesTotal = 0;
71         my $nFilesExist = 0;
72         my $sizeExist = 0;
73
74         print "working on $xfer_log $host #$num\n";
75
76         my $xfer_fh = BackupPC::FileZIO->open($xfer_log, 0, $Conf{CompressLevel});
77         while ( my $line = $xfer_fh->readLine() ) {
78                 chomp($line);
79                 if ($line =~ m#^\s+(create|pool|delete|same|link)\s+(.)\s\d\d\d\s+\d+/\d+\s+(\d+)\s(.+)$#) {
80                         my ($type,$is_dir,$size,$path) = ($1,$2,$3);
81                         #print "$type [$is_dir] $size $path\n";
82
83                         next if ($is_dir eq 'd');
84
85                         if ($type eq 'pool' || $type eq 'same') {
86                                 $sizeExist += $size;
87                                 $nFilesExist++;
88                         } elsif ($type ne 'delete') {
89                                 $sizeTotal += $size;
90                                 $nFilesTotal++;
91                         }
92
93                 } else {
94                         print "#SKIP:$line<-\n";
95                 }
96         }
97         $xfer_fh->close();
98
99         my $this_backup = {
100                 num            => $num,
101                 type           => $type,
102                 startTime      => $startTime,
103                 endTime        => $endTime,
104                 size           => $sizeTotal,
105                 nFiles         => $nFilesTotal,
106                 xferErrs       => 0,                    # bogus
107                 xferBadFile    => 0,                    # bogus
108                 xferBadShare   => 0,                    # bogus
109                 nFilesExist    => $nFilesExist,
110                 sizeExist      => $sizeExist,
111                 sizeExistComp  => $sizeExist,           # bogus
112                 tarErrs        => 0,                    # bogus
113                 compress       => $Conf{CompressLevel} ,
114                 noFill         => $type eq "incr" ? 1 : 0,
115                 level          => $type eq "incr" ? 1 : 0,
116                 mangle         => 1,        # name mangling always on for v1.04+
117         };
118
119         push @new_backups, $this_backup;
120         $bpc->BackupInfoWrite($host, @new_backups);
121 }
122