File: BackupPC_fixupBackupSummary

package info (click to toggle)
backuppc 4.4.0-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,752 kB
  • sloc: perl: 37,523; sh: 607; javascript: 176; makefile: 38; ansic: 6
file content (244 lines) | stat: -rwxr-xr-x 7,648 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_fixupBackupSummary: recreate backups file in case
# it was lost.
#
# DESCRIPTION
#
#   Usage: BackupPC_fixupBackupSummary [clients...]
#
# AUTHOR
#   Craig Barratt  <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
#   Copyright (C) 2005-2020  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
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#========================================================================
#
# Version 4.4.0, released 20 Jun 2020.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================

use strict;
no utf8;

use lib "__INSTALLDIR__/lib";
use Getopt::Std;
use Data::Dumper;
use Time::ParseDate;

use BackupPC::Lib;
use BackupPC::XS;

die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );

my $TopDir = $bpc->TopDir();
my $BinDir = $bpc->BinDir();
my %Conf   = $bpc->Conf();
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));
} else {
    @hostList = @ARGV;
}

foreach my $host ( @hostList ) {
    my(@Backups, $BkupFromLOG, $BkupFromInfo, $BkupNums, @LogFiles);

    $BkupFromInfo = {};
    $BkupFromLOG  = {};
    if ( !defined($Hosts->{$host}) ) {
        print("$host doesn't exist in BackupPC's host file... skipping\n");
        next;
    }

    my $dir = "$TopDir/pc/$host";
    print("Doing host $host\n");

    if ( !opendir(DIR, $dir) ) {
        print("$host: Can't open $dir... skipping $host\n");
        next;
    }

    #
    # Read the backups file
    #
    @Backups = $bpc->BackupInfoRead($host);

    #
    # Look through the LOG files to get information about
    # completed backups.  The data from the LOG file is
    # incomplete, but enough to get some useful info.
    #
    # Also, try to pick up the new-style of information
    # that is kept in each backup tree.  This info is
    # complete.  This data is only saved after version
    # 2.1.2.
    #
    my @files = readdir(DIR);
    closedir(DIR);
    foreach my $file ( @files ) {
        if ( $opts{l} && $file =~ /^LOG(.\d+\.z)?/ ) {
            push(@LogFiles, $file);
        } elsif ( $file =~ /^(\d+)$/ ) {
            my $bkupNum = $1;
            $BkupNums->{$bkupNum} = 1;

            next if ( !-f "$dir/$bkupNum/backupInfo" );

            #
            # Read backup info
            #
            %backupInfo = ();
            print("    Reading $dir/$bkupNum/backupInfo\n");
            if ( !(my $ret = do "$dir/$bkupNum/backupInfo") ) {
                print("    couldn't parse $dir/$bkupNum/backupInfo: $@\n") if $@;
                print("    couldn't do $dir/$bkupNum/backupInfo: $!\n")
                  unless defined $ret;
                print("    couldn't run $dir/$bkupNum/backupInfo\n");
                next;
            }
            if ( !keys(%backupInfo) || !defined($backupInfo{num}) ) {
                print("    $dir/$bkupNum/backupInfo is empty\n");
                next;
            }
            %{$BkupFromInfo->{$backupInfo{num}}} = %backupInfo;
        }
    }

    #
    # Read through LOG files from oldest to newest
    #
    @LogFiles = sort({ -M "$dir/$a" <=> -M "$dir/$b" } @LogFiles);
    my $startTime;
    my $fillFromNum;
    foreach my $file ( @LogFiles ) {
        my $f = BackupPC::XS::FileZIO::open("$dir/$file", 0, $file =~ /\.z/);

        if ( !defined($f) ) {
            print("$host: unable to open file $dir/$file\n");
            next;
        }
        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 / ) {
                $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 $type        = $2;
            my $bkupNum     = $3;
            my $nFilesTotal = $4;
            my $sizeTotal   = $5;
            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/$bkupNum" );
            $BkupFromLOG->{$bkupNum} = {
                num           => $bkupNum,
                type          => $type,
                startTime     => $startTime,
                endTime       => $endTime,
                size          => $sizeTotal,
                nFiles        => $nFilesTotal,
                xferErrs      => $xferErrs,
                xferBadFile   => $badFiles,
                xferBadShare  => $badShare,
                nFilesExist   => 0,
                sizeExist     => 0,
                sizeExistComp => 0,
                tarErrs       => 0,
                compress      => $Conf{CompressLevel},
                noFill        => $type eq "incr" ? 1 : 0,
                level         => $type eq "incr" ? 1 : 0,
                mangle        => 1,
                fillFromNum   => $fillFromNum,
            };
            $fillFromNum = $bkupNum if ( $type eq "full" );
        }
    }

    #
    # Now merge any info from $BkupFromInfo and $BkupFromLOG
    # that is missing from @Backups.
    #
    # First, anything in @Backups overrides the other data
    #
    #
    foreach ( my $i = 0 ; $i < @Backups ; $i++ ) {
        my $bkupNum = $Backups[$i]{num};
        delete($BkupFromLOG->{$bkupNum});
        delete($BkupFromInfo->{$bkupNum});
        delete($BkupNums->{$bkupNum});
    }

    #
    # Now merge in data from the LOG and backupInfo files.
    # backupInfo files override LOG files.
    #
    my $changes;

    foreach my $bkupNum ( keys(%$BkupFromLOG) ) {
        next if ( defined($BkupFromInfo->{$bkupNum}) );
        print("    Adding info for backup $bkupNum from LOG file\n");
        push(@Backups, $BkupFromLOG->{$bkupNum});
        delete($BkupNums->{$bkupNum});
        $changes++;
    }
    foreach my $bkupNum ( keys(%$BkupFromInfo) ) {
        print("    Adding info for backup $bkupNum from backupInfo file\n");
        push(@Backups, $BkupFromInfo->{$bkupNum});
        delete($BkupNums->{$bkupNum});
        $changes++;
    }
    foreach my $bkupNum ( keys(%$BkupNums) ) {
        print("    *** No info for backup number $bkupNum\n");
    }

    if ( $changes ) {
        @Backups = sort({ $a->{num} <=> $b->{num} } @Backups);

        # print Dumper \@Backups;
        $bpc->BackupInfoWrite($host, @Backups);
    } else {
        print("    No changes for host $host\n");
    }
}