use DateTime::Duration to correctly convert partial dates in to fields
[BackupPC.git] / bin / BackupPC_fixupBackupSummary
index 5bc5318..c5402dc 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/perl
+#!/usr/bin/perl
 #============================================================= -*-perl-*-
 #
 # BackupPC_fixupBackupSummary: recreate backups file in case
@@ -12,7 +12,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2005  Craig Barratt
+#   Copyright (C) 2005-2009  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -30,7 +30,7 @@
 #
 #========================================================================
 #
-# Version 2.1.0, released 20 Jun 2004.
+# Version 3.2.0, released 31 Jul 2010.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -56,6 +56,17 @@ my $Hosts  = $bpc->HostInfoRead();
 my @hostList;
 
 our(%backupInfo);
+my %opts;
+
+if ( !getopts("l", \%opts) ) {
+    print STDERR <<EOF;
+usage: $0 [-l]
+  Options:
+    -l    legacy mode: try to reconstruct backups from LOG
+          files for backups prior to BackupPC v3.0.
+EOF
+    exit(1);
+}
 
 if ( !@ARGV ) {
     @hostList = sort(keys(%$Hosts));
@@ -86,28 +97,6 @@ foreach my $host ( @hostList ) {
     #
     @Backups = $bpc->BackupInfoRead($host);
 
-    #
-    # Temporary: create backupInfo files in each backup
-    # directory
-    #
-    foreach ( my $i = 0 ; $i < @Backups ; $i++ ) {
-        BackupPC::Storage->backupInfoWrite($dir, $Backups[$i]{num},
-                                           $Backups[$i]);
-        if ( 0 ) {
-            my $bkupNum = $Backups[$i]{num};
-            if ( !-f "$dir/$bkupNum/backupInfo" ) {
-                my($dump) = Data::Dumper->new(
-                         [   $Backups[$i]],
-                         [qw(*backupInfo)]);
-                $dump->Indent(1);
-                if ( open(BKUPINFO, ">", "$dir/$bkupNum/backupInfo") ) {
-                    print(BKUPINFO $dump->Dump);
-                    close(BKUPINFO);
-                }
-            }
-        }
-    }
-
     #
     # Look through the LOG files to get information about
     # completed backups.  The data from the LOG file is
@@ -121,7 +110,7 @@ foreach my $host ( @hostList ) {
     my @files = readdir(DIR);
     closedir(DIR);
     foreach my $file ( @files ) {
-        if ( $file =~ /^LOG(.\d+\.z)?/ ) {
+        if ( $opts{l} && $file =~ /^LOG(.\d+\.z)?/ ) {
             push(@LogFiles, $file);
         } elsif ( $file =~ /^(\d+)$/ ) {
             my $bkupNum = $1;
@@ -154,6 +143,7 @@ foreach my $host ( @hostList ) {
     #
     @LogFiles = sort({-M "$dir/$a" <=> -M "$dir/$b"} @LogFiles);
     my $startTime;
+    my $fillFromNum;
     foreach my $file ( @LogFiles ) {
         my $f = BackupPC::FileZIO->open("$dir/$file", 0, $file =~ /\.z/);
 
@@ -163,13 +153,12 @@ foreach my $host ( @hostList ) {
         }
         print("    Reading $file\n");
         while ( (my $str = $f->readLine()) ne "" ) {
-            if ( $str =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (full|incr|partial) backup started for directory / ) {
-                $startTime = $str;
+            if ( $str =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (full|incr|partial) backup started / ) {
+                $startTime = parsedate($1);
                 next;
             }
             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\)/ );
 
-            my $endTime     = $1;
             my $type        = $2;
             my $bkupNum     = $3;
             my $nFilesTotal = $4;
@@ -177,13 +166,14 @@ foreach my $host ( @hostList ) {
             my $xferErrs    = $6;
             my $badFiles    = $7;
             my $badShare    = $8;
+            my $endTime     = parsedate($1);
             print("    Got $type backup $bkupNum at $endTime\n");
-            next if ( !-d "$dir/$2" );
+            next if ( !-d "$dir/$bkupNum" );
             $BkupFromLOG->{$bkupNum} = {
                 num            => $bkupNum,
                 type           => $type,
-                startTime      => parsedate($startTime),
-                endTime        => parsedate($endTime),
+                startTime      => $startTime,
+                endTime        => $endTime,
                 size           => $sizeTotal,
                 nFiles         => $nFilesTotal,
                 xferErrs       => $xferErrs,
@@ -197,14 +187,12 @@ foreach my $host ( @hostList ) {
                 noFill         => $type eq "incr" ? 1 : 0,
                 level          => $type eq "incr" ? 1 : 0,
                 mangle         => 1,
-                noFill         => $noFill;
-                fillFromNum    => $fillFromNum;
+                fillFromNum    => $fillFromNum,
             };
+            $fillFromNum = $bkupNum if ( $type eq "full" );
         }
     }
 
-    splice(@Backups, 2, 1);
-    
     #
     # Now merge any info from $BkupFromInfo and $BkupFromLOG
     # that is missing from @Backups.
@@ -244,7 +232,8 @@ foreach my $host ( @hostList ) {
 
     if ( $changes ) {
         @Backups = sort({$a->{num} <=> $b->{num}} @Backups);
-        print Dumper \@Backups;
+        # print Dumper \@Backups;
+        $bpc->BackupInfoWrite($host, @Backups);
     } else {
         print("    No changes for host $host\n");
     }