#!/usr/bin/perl # try to recover pc/hostname/backups file from existing transfer logs use strict; #use lib "__INSTALLDIR__/lib"; use lib "/data/backuppc/lib"; use BackupPC::Lib; use BackupPC::FileZIO; use Data::Dumper; my $bpc = BackupPC::Lib->new(undef, undef, 1) or die "BackupPC::Lib->new failed\n"; my $TopDir = $bpc->TopDir(); my %Conf = $bpc->Conf(); my $host = shift @ARGV || die "usage: $0 hostname\n"; my $pc_dir = "$TopDir/pc/$host"; print "working in $pc_dir\n"; sub extract_num($) { my $file = shift || die "no file?"; my $nr; if ($file =~ m/XferLOG\.(\d+)/) { $nr = $1; } else { die "can't extract number from $file"; } return $nr; } my @logs = sort { extract_num($a) <=> extract_num($b) } glob("$pc_dir/XferLOG.[0-9]*"); print "found ", $#logs + 1, " XferLOG files for $host\n"; print "\t",join("\n\t", @logs),"\n"; my $backups_file = "$pc_dir/backups"; if (-e $backups_file) { my @old_backups = $bpc->BackupInfoRead($host); print "found ", $#old_backups + 1, " backups in $pc_dir/backups\n"; #print Dumper(\@old_backups); } else { print "WARNING: backups file $backups_file doesn't exist, creating new one\n"; } my @new_backups; foreach my $xfer_log (@logs) { my $num = extract_num($xfer_log); my $type = 'incr'; my $attrib_file = "$pc_dir/$num/attrib"; die "can't find $attrib_file: $!" unless (-e $attrib_file); my $startTime = (stat($attrib_file))[9]; # mtime my $endTime = (stat($attrib_file))[10]; # ctime my $sizeTotal = 0; my $nFilesTotal = 0; my $nFilesExist = 0; my $sizeExist = 0; print "working on $xfer_log $host #$num\n"; my $xfer_fh = BackupPC::FileZIO->open($xfer_log, 0, $Conf{CompressLevel}); while ( my $line = $xfer_fh->readLine() ) { chomp($line); if ($line =~ m#^\s+(create|pool|delete|same|link)\s+(.)\s\d\d\d\s+\d+/\d+\s+(\d+)\s(.+)$#) { my ($type,$is_dir,$size,$path) = ($1,$2,$3); #print "$type [$is_dir] $size $path\n"; next if ($is_dir eq 'd'); if ($type eq 'pool' || $type eq 'same') { $sizeExist += $size; $nFilesExist++; } elsif ($type ne 'delete') { $sizeTotal += $size; $nFilesTotal++; } } else { print "#SKIP:$line<-\n"; } } $xfer_fh->close(); my $this_backup = { num => $num, type => $type, startTime => $startTime, endTime => $endTime, size => $sizeTotal, nFiles => $nFilesTotal, xferErrs => 0, # bogus xferBadFile => 0, # bogus xferBadShare => 0, # bogus nFilesExist => $nFilesExist, sizeExist => $sizeExist, sizeExistComp => $sizeExist, # bogus tarErrs => 0, # bogus compress => $Conf{CompressLevel} , noFill => $type eq "incr" ? 1 : 0, level => $type eq "incr" ? 1 : 0, mangle => 1, # name mangling always on for v1.04+ }; push @new_backups, $this_backup; $bpc->BackupInfoWrite($host, @new_backups); }