Minor tweaks for 3.1.0beta0 release.
[BackupPC.git] / bin / BackupPC_fixupBackupSummary
1 #!/bin/perl
2 #============================================================= -*-perl-*-
3 #
4 # BackupPC_fixupBackupSummary: recreate backups file in case
5 # it was lost.
6 #
7 # DESCRIPTION
8 #  
9 #   Usage: BackupPC_fixupBackupSummary [clients...]
10 #
11 # AUTHOR
12 #   Craig Barratt  <cbarratt@users.sourceforge.net>
13 #
14 # COPYRIGHT
15 #   Copyright (C) 2005-2007  Craig Barratt
16 #
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.
21 #
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.
26 #
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
30 #
31 #========================================================================
32 #
33 # Version 3.1.0beta0, released 3 Sep 2007.
34 #
35 # See http://backuppc.sourceforge.net.
36 #
37 #========================================================================
38
39 use strict;
40 no  utf8;
41 use lib "/usr/local/BackupPC/lib";
42 use Getopt::Std;
43 use Data::Dumper;
44 use Time::ParseDate;
45
46 use BackupPC::Lib;
47 use BackupPC::Attrib qw(:all);
48 use BackupPC::FileZIO;
49 use BackupPC::Storage;
50
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();
56 my @hostList;
57
58 our(%backupInfo);
59 my %opts;
60
61 if ( !getopts("l", \%opts) ) {
62     print STDERR <<EOF;
63 usage: $0 [-l]
64   Options:
65     -l    legacy mode: try to reconstruct backups from LOG
66           files for backups prior to BackupPC v3.0.
67 EOF
68     exit(1);
69 }
70
71 if ( !@ARGV ) {
72     @hostList = sort(keys(%$Hosts));
73 } else {
74     @hostList = @ARGV;
75 }
76
77 foreach my $host ( @hostList ) {
78     my(@Backups, $BkupFromLOG, $BkupFromInfo, $BkupNums, @LogFiles);
79
80     $BkupFromInfo = {};
81     $BkupFromLOG  = {};
82     if ( !defined($Hosts->{$host}) ) {
83         print("$host doesn't exist in BackupPC's host file... skipping\n");
84         next;
85     }
86
87     my $dir = "$TopDir/pc/$host";
88     print("Doing host $host\n");
89
90     if ( !opendir(DIR, $dir) ) {
91         print("$host: Can't open $dir... skipping $host\n");
92         next;
93     }
94
95     #
96     # Read the backups file
97     #
98     @Backups = $bpc->BackupInfoRead($host);
99
100     #
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.
104     #
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
108     # 2.1.2.
109     #
110     my @files = readdir(DIR);
111     closedir(DIR);
112     foreach my $file ( @files ) {
113         if ( $opts{l} && $file =~ /^LOG(.\d+\.z)?/ ) {
114             push(@LogFiles, $file);
115         } elsif ( $file =~ /^(\d+)$/ ) {
116             my $bkupNum = $1;
117             $BkupNums->{$bkupNum} = 1;
118
119             next if ( !-f "$dir/$bkupNum/backupInfo" );
120
121             #
122             # Read backup info
123             #
124             %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")
129                                                         unless defined $ret;
130                 print("    couldn't run $dir/$bkupNum/backupInfo\n");
131                 next;
132             }
133             if ( !keys(%backupInfo) || !defined($backupInfo{num}) ) {
134                 print("    $dir/$bkupNum/backupInfo is empty\n");
135                 next;
136             }
137             %{$BkupFromInfo->{$backupInfo{num}}} = %backupInfo;
138         }
139     }
140
141     #
142     # Read through LOG files from oldest to newest
143     #
144     @LogFiles = sort({-M "$dir/$a" <=> -M "$dir/$b"} @LogFiles);
145     my $startTime;
146     my $fillFromNum;
147     foreach my $file ( @LogFiles ) {
148         my $f = BackupPC::FileZIO->open("$dir/$file", 0, $file =~ /\.z/);
149
150         if ( !defined($f) ) {
151             print("$host: unable to open file $dir/$file\n");
152             next;
153         }
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);
158                 next;
159             }
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\)/ );
161
162             my $type        = $2;
163             my $bkupNum     = $3;
164             my $nFilesTotal = $4;
165             my $sizeTotal   = $5;
166             my $xferErrs    = $6;
167             my $badFiles    = $7;
168             my $badShare    = $8;
169             my $endTime     = parsedate($1);
170             print("    Got $type backup $bkupNum at $endTime\n");
171             next if ( !-d "$dir/$bkupNum" );
172             $BkupFromLOG->{$bkupNum} = {
173                 num            => $bkupNum,
174                 type           => $type,
175                 startTime      => $startTime,
176                 endTime        => $endTime,
177                 size           => $sizeTotal,
178                 nFiles         => $nFilesTotal,
179                 xferErrs       => $xferErrs,
180                 xferBadFile    => $badFiles,
181                 xferBadShare   => $badShare,
182                 nFilesExist    => 0,
183                 sizeExist      => 0,
184                 sizeExistComp  => 0,
185                 tarErrs        => 0,
186                 compress       => $Conf{CompressLevel},
187                 noFill         => $type eq "incr" ? 1 : 0,
188                 level          => $type eq "incr" ? 1 : 0,
189                 mangle         => 1,
190                 fillFromNum    => $fillFromNum,
191             };
192             $fillFromNum = $bkupNum if ( $type eq "full" );
193         }
194     }
195
196     #
197     # Now merge any info from $BkupFromInfo and $BkupFromLOG
198     # that is missing from @Backups.
199     #
200     # First, anything in @Backups overrides the other data
201     #
202     #
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});
208     }
209
210     #
211     # Now merge in data from the LOG and backupInfo files.
212     # backupInfo files override LOG files.
213     #
214     my $changes;
215
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});
221         $changes++;
222     }
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});
227         $changes++;
228     }
229     foreach my $bkupNum ( keys(%$BkupNums) ) {
230         print("    *** No info for backup number $bkupNum\n");
231     }
232
233     if ( $changes ) {
234         @Backups = sort({$a->{num} <=> $b->{num}} @Backups);
235         # print Dumper \@Backups;
236         $bpc->BackupInfoWrite($host, @Backups);
237     } else {
238         print("    No changes for host $host\n");
239     }
240 }