2 #============================================================= -*-perl-*-
4 # BackupPC_link: link new backup into pool
8 # BackupPC_link inspects every file in a new backup and
9 # checks if an existing file from any previous backup is
10 # identical. If so, the file is removed and replaced by
11 # a hardlink to the existing file. If the file is new,
12 # a hardlink to the file is made in the pool area, so that
13 # this file is available for checking against future backups.
15 # Then, for incremental backups, hardlinks are made in the
16 # backup directories to all files that were not extracted during
17 # the incremental backups. The means the incremental dump looks
18 # like a complete image of the PC.
21 # Craig Barratt <cbarratt@users.sourceforge.net>
24 # Copyright (C) 2001-2003 Craig Barratt
26 # This program is free software; you can redistribute it and/or modify
27 # it under the terms of the GNU General Public License as published by
28 # the Free Software Foundation; either version 2 of the License, or
29 # (at your option) any later version.
31 # This program is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 # GNU General Public License for more details.
36 # You should have received a copy of the GNU General Public License
37 # along with this program; if not, write to the Free Software
38 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #========================================================================
42 # Version 2.1.2, released 5 Sep 2005.
44 # See http://backuppc.sourceforge.net.
46 #========================================================================
50 use lib "__INSTALLDIR__/lib";
53 use BackupPC::PoolWrite;
59 ###########################################################################
61 ###########################################################################
63 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
64 my $TopDir = $bpc->TopDir();
65 my $BinDir = $bpc->BinDir();
66 my %Conf = $bpc->Conf();
71 print("usage: $0 <host>\n");
74 if ( $ARGV[0] !~ /^([\w\.\s-]+)$/ ) {
75 print("$0: bad host name '$ARGV[0]'\n");
79 my $Dir = "$TopDir/pc/$host";
80 my($CurrDumpDir, $Compress);
83 # Re-read config file, so we can include the PC-specific config
85 $bpc->ConfigRead($host);
88 ###########################################################################
89 # Process any backups that haven't been linked
90 ###########################################################################
91 my $md5 = Digest::MD5->new;
92 my($nFilesNew, $sizeNew, $sizeNewComp);
93 my($nFilesExist, $sizeExist, $sizeExistComp);
95 my @Backups = $bpc->BackupInfoRead($host);
96 $nFilesNew = $sizeNew = $sizeNewComp = 0;
97 $nFilesExist = $sizeExist = $sizeExistComp = 0;
99 for ( $num = 0 ; $num < @Backups ; $num++ ) {
100 last if ( $Backups[$num]{nFilesNew} eq ""
101 || -f "$Dir/NewFileList.$Backups[$num]{num}" );
103 last if ( $num >= @Backups );
105 # Process list of new files left by BackupPC_dump
107 $CurrDumpDir = "$Dir/$Backups[$num]{num}";
108 $Compress = $Backups[$num]{compress};
109 if ( open(NEW, "<", "$Dir/NewFileList.$Backups[$num]{num}") ) {
113 next if ( !/(\w+) (\d+) (.*)/ );
114 LinkNewFile($1, $2, "$CurrDumpDir/$3");
118 unlink("$Dir/NewFileList.$Backups[$num]{num}")
119 if ( -f "$Dir/NewFileList.$Backups[$num]{num}" );
122 # See if we should fill in this dump. We only need to fill
123 # in incremental dumps. We can only fill in the incremental
124 # dump if there is an existing filled in dump with the same
125 # type of compression (on or off). Eg, we can't fill in
126 # a compressed incremental if the most recent filled in dump
131 if ( $Backups[$num]{type} ne "incr" ) {
133 } elsif ( $Conf{IncrFill} ) {
135 for ( $i = $num - 1 ; $i >= 0 ; $i-- ) {
136 last if ( !$Backups[$i]{noFill}
137 && ($Backups[$i]{compress} ? 1 : 0)
138 == ($Compress ? 1 : 0) );
140 my $prevDump = "$Dir/$Backups[$i]{num}";
141 if ( $i >= 0 && -d $prevDump ) {
142 find({wanted => \&FillIncr, no_chdir => 1}, $prevDump);
144 $fillFromNum = $Backups[$i]{num};
148 # Update the backup info file in $TopDir/pc/$host/backups
150 @Backups = $bpc->BackupInfoRead($host);
151 $Backups[$num]{nFilesExist} += $nFilesExist;
152 $Backups[$num]{sizeExist} += $sizeExist;
153 $Backups[$num]{sizeExistComp} += $sizeExistComp;
154 $Backups[$num]{nFilesNew} += $nFilesNew;
155 $Backups[$num]{sizeNew} += $sizeNew;
156 $Backups[$num]{sizeNewComp} += $sizeNewComp;
157 $Backups[$num]{noFill} = $noFill;
158 $Backups[$num]{fillFromNum} = $fillFromNum;
159 $bpc->BackupInfoWrite($host, @Backups);
162 ###########################################################################
164 ###########################################################################
167 # Fill in an incremental dump by making hardlinks to the previous
172 my($name) = $File::Find::name;
175 $name = $1 if ( $name =~ /(.*)/ );
176 return if ( $name !~ m{\Q$Dir\E/(\d+)/(.*)} );
177 $newName = "$CurrDumpDir/$2";
178 if ( -d $name && -d $newName ) {
180 # Merge the file attributes.
182 my $newAttr = BackupPC::Attrib->new({ compress => $Compress });
183 my $attr = BackupPC::Attrib->new({ compress => $Compress });
184 $newAttr->read($newName) if ( -f $newAttr->fileName($newName) );
185 $attr->read($name) if ( -f $attr->fileName($name) );
186 $newAttr->merge($attr);
188 # Now write it out, adding a link to the pool if necessary
190 my $data = $newAttr->writeData;
191 my $origSize = length($data);
192 my $fileName = $newAttr->fileName($newName);
193 my $poolWrite = BackupPC::PoolWrite->new($bpc, $fileName,
194 length($data), $Compress);
195 $poolWrite->write(\$data);
196 my($exists, $digest, $outSize, $errs) = $poolWrite->close;
198 print("log ", join("", @$errs));
202 $sizeExist += $origSize;
203 $sizeExistComp += $outSize;
204 } elsif ( $outSize > 0 ) {
206 $sizeNew += $origSize;
207 $sizeNewComp += -s $outSize;
208 LinkNewFile($digest, $origSize, $fileName);
210 } elsif ( -f $name && !-f $newName ) {
212 # Exists in the older filled backup, and not in the new, so link it
214 link($name, $newName);
219 # Add a link in the pool to a new file
223 my($d, $size, $fileName) = @_;
224 my $res = $bpc->MakeFileLink($fileName, $d, 1, $Compress);
228 $sizeExistComp += -s $fileName;
229 } elsif ( $res == 2 ) {
232 $sizeNewComp += -s $fileName;
233 } elsif ( $res != 0 && $res != -1 ) {
234 print("log BackupPC_link got error $res when calling"
235 . " MakeFileLink($fileName, $d, 1)\n");