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-2007 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 3.2.0beta0, released 5 April 2009.
44 # See http://backuppc.sourceforge.net.
46 #========================================================================
50 use lib "/usr/local/BackupPC/lib";
53 use BackupPC::PoolWrite;
54 use BackupPC::Storage;
60 ###########################################################################
62 ###########################################################################
64 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
65 my $TopDir = $bpc->TopDir();
66 my $BinDir = $bpc->BinDir();
67 my %Conf = $bpc->Conf();
72 print("usage: $0 <host>\n");
75 if ( $ARGV[0] !~ /^([\w\.\s-]+)$/ ) {
76 print("$0: bad host name '$ARGV[0]'\n");
80 my $Dir = "$TopDir/pc/$host";
81 my($CurrDumpDir, $Compress);
84 # Re-read config file, so we can include the PC-specific config
86 $bpc->ConfigRead($host);
89 ###########################################################################
90 # Process any backups that haven't been linked
91 ###########################################################################
92 my $md5 = Digest::MD5->new;
93 my($nFilesNew, $sizeNew, $sizeNewComp);
94 my($nFilesExist, $sizeExist, $sizeExistComp);
96 my @Backups = $bpc->BackupInfoRead($host);
97 $nFilesNew = $sizeNew = $sizeNewComp = 0;
98 $nFilesExist = $sizeExist = $sizeExistComp = 0;
100 for ( $num = 0 ; $num < @Backups ; $num++ ) {
101 last if ( $Backups[$num]{nFilesNew} eq ""
102 || -f "$Dir/NewFileList.$Backups[$num]{num}" );
104 last if ( $num >= @Backups );
106 # Process list of new files left by BackupPC_dump
108 $CurrDumpDir = "$Dir/$Backups[$num]{num}";
109 $Compress = $Backups[$num]{compress};
110 if ( open(NEW, "<", "$Dir/NewFileList.$Backups[$num]{num}") ) {
114 next if ( !/(\w+) (\d+) (.*)/ );
115 LinkNewFile($1, $2, "$CurrDumpDir/$3");
119 unlink("$Dir/NewFileList.$Backups[$num]{num}")
120 if ( -f "$Dir/NewFileList.$Backups[$num]{num}" );
123 # See if we should fill in this dump. We only need to fill
124 # in incremental dumps. We can only fill in the incremental
125 # dump if there is an existing filled in dump with the same
126 # type of compression (on or off). Eg, we can't fill in
127 # a compressed incremental if the most recent filled in dump
132 if ( $Backups[$num]{type} ne "incr" ) {
134 } elsif ( $Conf{IncrFill} ) {
136 for ( $i = $num - 1 ; $i >= 0 ; $i-- ) {
137 last if ( !$Backups[$i]{noFill}
138 && ($Backups[$i]{compress} ? 1 : 0)
139 == ($Compress ? 1 : 0) );
141 my $prevDump = "$Dir/$Backups[$i]{num}";
142 if ( $i >= 0 && -d $prevDump ) {
143 find({wanted => \&FillIncr, no_chdir => 1}, $prevDump);
145 $fillFromNum = $Backups[$i]{num};
149 # Update the backup info file in $TopDir/pc/$host/backups
151 @Backups = $bpc->BackupInfoRead($host);
152 $Backups[$num]{nFilesExist} += $nFilesExist;
153 $Backups[$num]{sizeExist} += $sizeExist;
154 $Backups[$num]{sizeExistComp} += $sizeExistComp;
155 $Backups[$num]{nFilesNew} += $nFilesNew;
156 $Backups[$num]{sizeNew} += $sizeNew;
157 $Backups[$num]{sizeNewComp} += $sizeNewComp;
158 $Backups[$num]{noFill} = $noFill;
159 $Backups[$num]{fillFromNum} = $fillFromNum;
161 # Save just this backup's info in case the main backups file
164 BackupPC::Storage->backupInfoWrite($Dir,
168 # Save the main backups file
170 $bpc->BackupInfoWrite($host, @Backups);
173 ###########################################################################
175 ###########################################################################
178 # Fill in an incremental dump by making hardlinks to the previous
183 my($name) = $File::Find::name;
186 $name = $1 if ( $name =~ /(.*)/ );
187 return if ( $name !~ m{\Q$Dir\E/(\d+)/(.*)} );
188 $newName = "$CurrDumpDir/$2";
189 if ( -d $name && -d $newName ) {
191 # Merge the file attributes.
193 my $newAttr = BackupPC::Attrib->new({ compress => $Compress });
194 my $attr = BackupPC::Attrib->new({ compress => $Compress });
195 $newAttr->read($newName) if ( -f $newAttr->fileName($newName) );
196 $attr->read($name) if ( -f $attr->fileName($name) );
197 $newAttr->merge($attr);
199 # Now write it out, adding a link to the pool if necessary
201 my $data = $newAttr->writeData;
202 my $origSize = length($data);
203 my $fileName = $newAttr->fileName($newName);
204 my $poolWrite = BackupPC::PoolWrite->new($bpc, $fileName,
205 length($data), $Compress);
206 $poolWrite->write(\$data);
207 my($exists, $digest, $outSize, $errs) = $poolWrite->close;
209 print("log ", join("", @$errs));
213 $sizeExist += $origSize;
214 $sizeExistComp += $outSize;
215 } elsif ( $outSize > 0 ) {
217 $sizeNew += $origSize;
218 $sizeNewComp += -s $outSize;
219 LinkNewFile($digest, $origSize, $fileName);
221 } elsif ( -f $name && !-f $newName ) {
223 # Exists in the older filled backup, and not in the new, so link it
225 my($exists, $digest, $origSize, $outSize, $errs)
226 = BackupPC::PoolWrite::LinkOrCopy(
229 $newName, $Compress);
232 $sizeExist += $origSize;
233 $sizeExistComp += $outSize;
234 } elsif ( $outSize > 0 ) {
236 $sizeNew += $origSize;
237 $sizeNewComp += -s $outSize;
238 LinkNewFile($digest, $origSize, $newName);
244 # Add a link in the pool to a new file
248 my($d, $size, $fileName) = @_;
249 my $res = $bpc->MakeFileLink($fileName, $d, 1, $Compress);
253 $sizeExistComp += -s $fileName;
254 } elsif ( $res == 2 ) {
257 $sizeNewComp += -s $fileName;
258 } elsif ( $res != 0 && $res != -1 ) {
259 print("log BackupPC_link got error $res when calling"
260 . " MakeFileLink($fileName, $d, 1)\n");