"Fossies" - the Fresh Open Source Software Archive

Member "BackupPC-4.4.0/bin/BackupPC_fixupBackupSummary" (20 Jun 2020, 7648 Bytes) of package /linux/privat/BackupPC-4.4.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "BackupPC_fixupBackupSummary": 4.3.2_vs_4.4.0.

    1 #!/usr/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-2020  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 3 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, see <http://www.gnu.org/licenses/>.
   29 #
   30 #========================================================================
   31 #
   32 # Version 4.4.0, released 20 Jun 2020.
   33 #
   34 # See http://backuppc.sourceforge.net.
   35 #
   36 #========================================================================
   37 
   38 use strict;
   39 no utf8;
   40 
   41 use lib "__INSTALLDIR__/lib";
   42 use Getopt::Std;
   43 use Data::Dumper;
   44 use Time::ParseDate;
   45 
   46 use BackupPC::Lib;
   47 use BackupPC::XS;
   48 
   49 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
   50 
   51 my $TopDir = $bpc->TopDir();
   52 my $BinDir = $bpc->BinDir();
   53 my %Conf   = $bpc->Conf();
   54 my $Hosts  = $bpc->HostInfoRead();
   55 my @hostList;
   56 
   57 our(%backupInfo);
   58 my %opts;
   59 
   60 if ( !getopts("l", \%opts) ) {
   61     print STDERR <<EOF;
   62 usage: $0 [-l]
   63   Options:
   64     -l    legacy mode: try to reconstruct backups from LOG
   65           files for backups prior to BackupPC v3.0.
   66 EOF
   67     exit(1);
   68 }
   69 
   70 if ( !@ARGV ) {
   71     @hostList = sort(keys(%$Hosts));
   72 } else {
   73     @hostList = @ARGV;
   74 }
   75 
   76 foreach my $host ( @hostList ) {
   77     my(@Backups, $BkupFromLOG, $BkupFromInfo, $BkupNums, @LogFiles);
   78 
   79     $BkupFromInfo = {};
   80     $BkupFromLOG  = {};
   81     if ( !defined($Hosts->{$host}) ) {
   82         print("$host doesn't exist in BackupPC's host file... skipping\n");
   83         next;
   84     }
   85 
   86     my $dir = "$TopDir/pc/$host";
   87     print("Doing host $host\n");
   88 
   89     if ( !opendir(DIR, $dir) ) {
   90         print("$host: Can't open $dir... skipping $host\n");
   91         next;
   92     }
   93 
   94     #
   95     # Read the backups file
   96     #
   97     @Backups = $bpc->BackupInfoRead($host);
   98 
   99     #
  100     # Look through the LOG files to get information about
  101     # completed backups.  The data from the LOG file is
  102     # incomplete, but enough to get some useful info.
  103     #
  104     # Also, try to pick up the new-style of information
  105     # that is kept in each backup tree.  This info is
  106     # complete.  This data is only saved after version
  107     # 2.1.2.
  108     #
  109     my @files = readdir(DIR);
  110     closedir(DIR);
  111     foreach my $file ( @files ) {
  112         if ( $opts{l} && $file =~ /^LOG(.\d+\.z)?/ ) {
  113             push(@LogFiles, $file);
  114         } elsif ( $file =~ /^(\d+)$/ ) {
  115             my $bkupNum = $1;
  116             $BkupNums->{$bkupNum} = 1;
  117 
  118             next if ( !-f "$dir/$bkupNum/backupInfo" );
  119 
  120             #
  121             # Read backup info
  122             #
  123             %backupInfo = ();
  124             print("    Reading $dir/$bkupNum/backupInfo\n");
  125             if ( !(my $ret = do "$dir/$bkupNum/backupInfo") ) {
  126                 print("    couldn't parse $dir/$bkupNum/backupInfo: $@\n") if $@;
  127                 print("    couldn't do $dir/$bkupNum/backupInfo: $!\n")
  128                   unless defined $ret;
  129                 print("    couldn't run $dir/$bkupNum/backupInfo\n");
  130                 next;
  131             }
  132             if ( !keys(%backupInfo) || !defined($backupInfo{num}) ) {
  133                 print("    $dir/$bkupNum/backupInfo is empty\n");
  134                 next;
  135             }
  136             %{$BkupFromInfo->{$backupInfo{num}}} = %backupInfo;
  137         }
  138     }
  139 
  140     #
  141     # Read through LOG files from oldest to newest
  142     #
  143     @LogFiles = sort({ -M "$dir/$a" <=> -M "$dir/$b" } @LogFiles);
  144     my $startTime;
  145     my $fillFromNum;
  146     foreach my $file ( @LogFiles ) {
  147         my $f = BackupPC::XS::FileZIO::open("$dir/$file", 0, $file =~ /\.z/);
  148 
  149         if ( !defined($f) ) {
  150             print("$host: unable to open file $dir/$file\n");
  151             next;
  152         }
  153         print("    Reading $file\n");
  154         while ( (my $str = $f->readLine()) ne "" ) {
  155             if ( $str =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (full|incr|partial) backup started / ) {
  156                 $startTime = parsedate($1);
  157                 next;
  158             }
  159             next
  160               if ( $str !~
  161                 /^(\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\)/
  162               );
  163 
  164             my $type        = $2;
  165             my $bkupNum     = $3;
  166             my $nFilesTotal = $4;
  167             my $sizeTotal   = $5;
  168             my $xferErrs    = $6;
  169             my $badFiles    = $7;
  170             my $badShare    = $8;
  171             my $endTime     = parsedate($1);
  172 
  173             print("    Got $type backup $bkupNum at $endTime\n");
  174             next if ( !-d "$dir/$bkupNum" );
  175             $BkupFromLOG->{$bkupNum} = {
  176                 num           => $bkupNum,
  177                 type          => $type,
  178                 startTime     => $startTime,
  179                 endTime       => $endTime,
  180                 size          => $sizeTotal,
  181                 nFiles        => $nFilesTotal,
  182                 xferErrs      => $xferErrs,
  183                 xferBadFile   => $badFiles,
  184                 xferBadShare  => $badShare,
  185                 nFilesExist   => 0,
  186                 sizeExist     => 0,
  187                 sizeExistComp => 0,
  188                 tarErrs       => 0,
  189                 compress      => $Conf{CompressLevel},
  190                 noFill        => $type eq "incr" ? 1 : 0,
  191                 level         => $type eq "incr" ? 1 : 0,
  192                 mangle        => 1,
  193                 fillFromNum   => $fillFromNum,
  194             };
  195             $fillFromNum = $bkupNum if ( $type eq "full" );
  196         }
  197     }
  198 
  199     #
  200     # Now merge any info from $BkupFromInfo and $BkupFromLOG
  201     # that is missing from @Backups.
  202     #
  203     # First, anything in @Backups overrides the other data
  204     #
  205     #
  206     foreach ( my $i = 0 ; $i < @Backups ; $i++ ) {
  207         my $bkupNum = $Backups[$i]{num};
  208         delete($BkupFromLOG->{$bkupNum});
  209         delete($BkupFromInfo->{$bkupNum});
  210         delete($BkupNums->{$bkupNum});
  211     }
  212 
  213     #
  214     # Now merge in data from the LOG and backupInfo files.
  215     # backupInfo files override LOG files.
  216     #
  217     my $changes;
  218 
  219     foreach my $bkupNum ( keys(%$BkupFromLOG) ) {
  220         next if ( defined($BkupFromInfo->{$bkupNum}) );
  221         print("    Adding info for backup $bkupNum from LOG file\n");
  222         push(@Backups, $BkupFromLOG->{$bkupNum});
  223         delete($BkupNums->{$bkupNum});
  224         $changes++;
  225     }
  226     foreach my $bkupNum ( keys(%$BkupFromInfo) ) {
  227         print("    Adding info for backup $bkupNum from backupInfo file\n");
  228         push(@Backups, $BkupFromInfo->{$bkupNum});
  229         delete($BkupNums->{$bkupNum});
  230         $changes++;
  231     }
  232     foreach my $bkupNum ( keys(%$BkupNums) ) {
  233         print("    *** No info for backup number $bkupNum\n");
  234     }
  235 
  236     if ( $changes ) {
  237         @Backups = sort({ $a->{num} <=> $b->{num} } @Backups);
  238 
  239         # print Dumper \@Backups;
  240         $bpc->BackupInfoWrite($host, @Backups);
  241     } else {
  242         print("    No changes for host $host\n");
  243     }
  244 }