2 #============================================================= -*-perl-*-
4 # BackupPC_fixupBackupSummary: recreate backups file in case
9 # Usage: BackupPC_fixupBackupSummary [clients...]
12 # Craig Barratt <cbarratt@users.sourceforge.net>
15 # Copyright (C) 2005-2007 Craig Barratt
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write to the Free Software
29 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #========================================================================
33 # Version 3.1.0, released 25 Nov 2007.
35 # See http://backuppc.sourceforge.net.
37 #========================================================================
41 use lib "/usr/local/BackupPC/lib";
47 use BackupPC::Attrib qw(:all);
48 use BackupPC::FileZIO;
49 use BackupPC::Storage;
51 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
52 my $TopDir = $bpc->TopDir();
53 my $BinDir = $bpc->BinDir();
54 my %Conf = $bpc->Conf();
55 my $Hosts = $bpc->HostInfoRead();
61 if ( !getopts("l", \%opts) ) {
65 -l legacy mode: try to reconstruct backups from LOG
66 files for backups prior to BackupPC v3.0.
72 @hostList = sort(keys(%$Hosts));
77 foreach my $host ( @hostList ) {
78 my(@Backups, $BkupFromLOG, $BkupFromInfo, $BkupNums, @LogFiles);
82 if ( !defined($Hosts->{$host}) ) {
83 print("$host doesn't exist in BackupPC's host file... skipping\n");
87 my $dir = "$TopDir/pc/$host";
88 print("Doing host $host\n");
90 if ( !opendir(DIR, $dir) ) {
91 print("$host: Can't open $dir... skipping $host\n");
96 # Read the backups file
98 @Backups = $bpc->BackupInfoRead($host);
101 # Look through the LOG files to get information about
102 # completed backups. The data from the LOG file is
103 # incomplete, but enough to get some useful info.
105 # Also, try to pick up the new-style of information
106 # that is kept in each backup tree. This info is
107 # complete. This data is only saved after version
110 my @files = readdir(DIR);
112 foreach my $file ( @files ) {
113 if ( $opts{l} && $file =~ /^LOG(.\d+\.z)?/ ) {
114 push(@LogFiles, $file);
115 } elsif ( $file =~ /^(\d+)$/ ) {
117 $BkupNums->{$bkupNum} = 1;
119 next if ( !-f "$dir/$bkupNum/backupInfo" );
125 print(" Reading $dir/$bkupNum/backupInfo\n");
126 if ( !(my $ret = do "$dir/$bkupNum/backupInfo") ) {
127 print(" couldn't parse $dir/$bkupNum/backupInfo: $@\n") if $@;
128 print(" couldn't do $dir/$bkupNum/backupInfo: $!\n")
130 print(" couldn't run $dir/$bkupNum/backupInfo\n");
133 if ( !keys(%backupInfo) || !defined($backupInfo{num}) ) {
134 print(" $dir/$bkupNum/backupInfo is empty\n");
137 %{$BkupFromInfo->{$backupInfo{num}}} = %backupInfo;
142 # Read through LOG files from oldest to newest
144 @LogFiles = sort({-M "$dir/$a" <=> -M "$dir/$b"} @LogFiles);
147 foreach my $file ( @LogFiles ) {
148 my $f = BackupPC::FileZIO->open("$dir/$file", 0, $file =~ /\.z/);
150 if ( !defined($f) ) {
151 print("$host: unable to open file $dir/$file\n");
154 print(" Reading $file\n");
155 while ( (my $str = $f->readLine()) ne "" ) {
156 if ( $str =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (full|incr|partial) backup started / ) {
157 $startTime = parsedate($1);
160 next if ( $str !~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (full|incr|partial) backup (\d+) complete, (\d+) files, (\d+) bytes, (\d+) xferErrs \((\d+) bad files, (\d+) bad shares, (\d+) other\)/ );
164 my $nFilesTotal = $4;
169 my $endTime = parsedate($1);
170 print(" Got $type backup $bkupNum at $endTime\n");
171 next if ( !-d "$dir/$bkupNum" );
172 $BkupFromLOG->{$bkupNum} = {
175 startTime => $startTime,
178 nFiles => $nFilesTotal,
179 xferErrs => $xferErrs,
180 xferBadFile => $badFiles,
181 xferBadShare => $badShare,
186 compress => $Conf{CompressLevel},
187 noFill => $type eq "incr" ? 1 : 0,
188 level => $type eq "incr" ? 1 : 0,
190 fillFromNum => $fillFromNum,
192 $fillFromNum = $bkupNum if ( $type eq "full" );
197 # Now merge any info from $BkupFromInfo and $BkupFromLOG
198 # that is missing from @Backups.
200 # First, anything in @Backups overrides the other data
203 foreach ( my $i = 0 ; $i < @Backups ; $i++ ) {
204 my $bkupNum = $Backups[$i]{num};
205 delete($BkupFromLOG->{$bkupNum});
206 delete($BkupFromInfo->{$bkupNum});
207 delete($BkupNums->{$bkupNum});
211 # Now merge in data from the LOG and backupInfo files.
212 # backupInfo files override LOG files.
216 foreach my $bkupNum ( keys(%$BkupFromLOG) ) {
217 next if ( defined($BkupFromInfo->{$bkupNum}) );
218 print(" Adding info for backup $bkupNum from LOG file\n");
219 push(@Backups, $BkupFromLOG->{$bkupNum});
220 delete($BkupNums->{$bkupNum});
223 foreach my $bkupNum ( keys(%$BkupFromInfo) ) {
224 print(" Adding info for backup $bkupNum from backupInfo file\n");
225 push(@Backups, $BkupFromInfo->{$bkupNum});
226 delete($BkupNums->{$bkupNum});
229 foreach my $bkupNum ( keys(%$BkupNums) ) {
230 print(" *** No info for backup number $bkupNum\n");
234 @Backups = sort({$a->{num} <=> $b->{num}} @Backups);
235 # print Dumper \@Backups;
236 $bpc->BackupInfoWrite($host, @Backups);
238 print(" No changes for host $host\n");