1 #============================================================= -*-perl-*-
3 # BackupPC::Xfer::Rsync package
7 # This library defines a BackupPC::Xfer::Rsync class for managing
8 # the rsync-based transport of backup data from the client.
11 # Craig Barratt <cbarratt@users.sourceforge.net>
14 # Copyright (C) 2002-2003 Craig Barratt
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #========================================================================
32 # Version 3.0.0beta0, released 11 Jul 2006.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::Xfer::Rsync;
42 use BackupPC::Xfer::RsyncFileIO;
44 use vars qw( $RsyncLibOK $RsyncLibErr );
47 eval "use File::RsyncP;";
50 # Rsync module doesn't exist.
53 $RsyncLibErr = "File::RsyncP module doesn't exist";
56 # Note: also update configure.pl when this version number is changed!
58 if ( $File::RsyncP::VERSION < 0.62 ) {
60 $RsyncLibErr = "File::RsyncP module version"
61 . " ($File::RsyncP::VERSION) too old: need 0.62";
70 my($class, $bpc, $args) = @_;
72 return if ( !$RsyncLibOK );
76 conf => { $bpc->Conf },
105 foreach my $arg ( keys(%$args) ) {
106 $t->{$arg} = $args->{$arg};
119 my $conf = $t->{conf};
120 my(@fileList, $rsyncClientCmd, $rsyncArgs, $logMsg,
121 $incrDate, $argList, $fioArgs);
124 # We add a slash to the share name we pass to rsync
126 ($t->{shareNameSlash} = "$t->{shareName}/") =~ s{//+$}{/};
128 if ( $t->{type} eq "restore" ) {
129 $rsyncClientCmd = $conf->{RsyncClientRestoreCmd};
130 $rsyncArgs = $conf->{RsyncRestoreArgs};
131 my $remoteDir = "$t->{shareName}/$t->{pathHdrDest}";
132 $remoteDir =~ s{//+}{/}g;
133 $argList = ['--server', @$rsyncArgs, '.', $remoteDir];
135 client => $t->{bkupSrcHost},
136 share => $t->{bkupSrcShare},
137 viewNum => $t->{bkupSrcNum},
138 fileList => $t->{fileList},
140 $logMsg = "restore started below directory $t->{shareName}"
141 . " to host $t->{host}";
144 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
145 # into a hash of arrays of files, and $conf->{RsyncShareName}
148 $bpc->backupFileConfFix($conf, "RsyncShareName");
150 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
151 my(@inc, @exc, %incDone, %excDone);
152 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
154 # If the user wants to just include /home/craig, then
155 # we need to do create include/exclude pairs at
157 # --include /home --exclude /*
158 # --include /home/craig --exclude /home/*
160 # It's more complex if the user wants to include multiple
161 # deep paths. For example, if they want /home/craig and
162 # /var/log, then we need this mouthfull:
163 # --include /home --include /var --exclude /*
164 # --include /home/craig --exclude /home/*
165 # --include /var/log --exclude /var/*
167 # To make this easier we do all the includes first and all
168 # of the excludes at the end (hopefully they commute).
173 if ( $file eq "/" ) {
175 # This is a special case: if the user specifies
176 # "/" then just include it and don't exclude "/*".
178 push(@inc, $file) if ( !$incDone{$file} );
182 while ( $file =~ m{^/([^/]*)(.*)} ) {
185 if ( $file eq "/" ) {
187 # preserve a tailing slash
192 push(@exc, "$f/*") if ( !$excDone{"$f/*"} );
193 $excDone{"$f/*"} = 1;
195 push(@inc, $f) if ( !$incDone{$f} );
199 foreach my $file ( @inc ) {
200 push(@fileList, "--include=$file");
202 foreach my $file ( @exc ) {
203 push(@fileList, "--exclude=$file");
206 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
207 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
210 # just append additional exclude lists onto the end
212 push(@fileList, "--exclude=$file");
215 if ( $t->{type} eq "full" ) {
216 if ( $t->{partialNum} ) {
217 $logMsg = "full backup started for directory $t->{shareName};"
218 . " updating partial #$t->{partialNum}";
220 $logMsg = "full backup started for directory $t->{shareName}";
221 if ( $t->{incrBaseBkupNum} ne "" ) {
222 $logMsg .= " (baseline backup #$t->{incrBaseBkupNum})";
226 $incrDate = $bpc->timeStamp($t->{incrBaseTime}, 1);
227 $logMsg = "incr backup started back to $incrDate"
228 . " (backup #$t->{incrBaseBkupNum}) for directory"
229 . " $t->{shareName}";
233 # A full dump is implemented with --ignore-times: this causes all
234 # files to be checksummed, even if the attributes are the same.
235 # That way all the file contents are checked, but you get all
236 # the efficiencies of rsync: only files deltas need to be
237 # transferred, even though it is a full dump.
239 $rsyncArgs = $conf->{RsyncArgs};
240 $rsyncArgs = [@$rsyncArgs, @fileList] if ( @fileList );
241 $rsyncArgs = [@$rsyncArgs, "--ignore-times"]
242 if ( $t->{type} eq "full" );
243 $rsyncClientCmd = $conf->{RsyncClientCmd};
244 $argList = ['--server', '--sender', @$rsyncArgs,
245 '.', $t->{shareNameSlash}];
247 $argList = File::RsyncP->excludeStrip($argList);
250 client => $t->{client},
251 share => $t->{shareName},
252 viewNum => $t->{incrBaseBkupNum},
253 partialNum => $t->{partialNum},
258 # Merge variables into $rsyncClientCmd
262 hostIP => $t->{hostIP},
263 client => $t->{client},
264 shareName => $t->{shareName},
265 shareNameSlash => $t->{shareNameSlash},
266 rsyncPath => $conf->{RsyncClientPath},
267 sshPath => $conf->{SshPath},
270 $rsyncClientCmd = $bpc->cmdVarSubstitute($rsyncClientCmd, $args);
273 # Create the Rsync object, and tell it to use our own File::RsyncP::FileIO
274 # module, which handles all the special BackupPC file storage
275 # (compression, mangling, hardlinks, special files, attributes etc).
277 $t->{rsyncClientCmd} = $rsyncClientCmd;
278 $t->{rs} = File::RsyncP->new({
279 logLevel => $t->{logLevel} || $conf->{RsyncLogLevel},
282 $bpc->cmdExecOrEval($rsyncClientCmd, $args);
284 rsyncCmdType => "full",
285 rsyncArgs => $rsyncArgs,
286 timeout => $conf->{ClientTimeout},
287 doPartial => defined($t->{partialNum}) ? 1 : undef,
292 $t->{XferLOG}->write(\$str);
293 if ( $str =~ /^Remote\[1\]: read errors mapping "(.*)"/ ) {
295 # Files with read errors (eg: region locked files
296 # on WinXX) are filled with 0 by rsync. Remember
297 # them and delete them later.
300 $badFile =~ s/^\/+//;
301 push(@{$t->{badFiles}}, {
302 share => $t->{shareName},
308 $t->{pidHandler}(@_);
310 clientCharset => $conf->{ClientCharset},
311 fio => BackupPC::Xfer::RsyncFileIO->new({
315 backups => $t->{backups},
316 logLevel => $t->{logLevel}
317 || $conf->{RsyncLogLevel},
321 $t->{XferLOG}->write(\$str);
323 cacheCheckProb => $conf->{RsyncCsumCacheVerifyProb},
324 clientCharset => $conf->{ClientCharset},
329 delete($t->{_errStr});
338 my $conf = $t->{conf};
339 my($remoteSend, $remoteDir, $remoteDirDaemon);
341 alarm($conf->{ClientTimeout});
342 if ( $t->{type} eq "restore" ) {
344 ($remoteDir = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
345 ($remoteDirDaemon = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
346 $remoteDirDaemon = $t->{shareNameSlash}
347 if ( $t->{pathHdrDest} eq ""
348 || $t->{pathHdrDest} eq "/" );
351 $remoteDir = $t->{shareNameSlash};
352 $remoteDirDaemon = ".";
354 if ( $t->{XferMethod} eq "rsync" ) {
358 my $str = "Running: "
359 . $t->{bpc}->execCmd2ShellCmd(@{$t->{rsyncClientCmd}})
361 $t->{XferLOG}->write(\$str);
362 $rs->remoteStart($remoteSend, $remoteDir);
365 # Connect to the rsync server
367 if ( defined(my $err = $rs->serverConnect($t->{hostIP},
368 $conf->{RsyncdClientPort})) ) {
369 $t->{hostError} = $err;
370 my $str = "Error connecting to rsync daemon at $t->{hostIP}"
371 . ":$conf->{RsyncdClientPort}: $err\n";
372 $t->{XferLOG}->write(\$str);
376 # Pass module name, and follow it with a slash if it already
377 # contains a slash; otherwise just keep the plain module name.
379 my $module = $t->{shareName};
380 $module = $t->{shareNameSlash} if ( $module =~ /\// );
381 if ( defined(my $err = $rs->serverService($module,
382 $conf->{RsyncdUserName},
383 $conf->{RsyncdPasswd},
384 $conf->{RsyncdAuthRequired})) ) {
385 my $str = "Error connecting to module $module at $t->{hostIP}"
386 . ":$conf->{RsyncdClientPort}: $err\n";
387 $t->{XferLOG}->write(\$str);
388 $t->{hostError} = $err;
391 $rs->serverStart($remoteSend, $remoteDirDaemon);
393 my $error = $rs->go($t->{shareNameSlash});
397 # TODO: generate sensible stats
399 # $rs->{stats}{totalWritten}
400 # $rs->{stats}{totalSize}
402 my $stats = $rs->statsFinal;
403 if ( !defined($error) && defined($stats) ) {
408 $t->{xferErrCnt} = $stats->{remoteErrCnt}
409 + $stats->{childStats}{errorCnt}
410 + $stats->{parentStats}{errorCnt};
411 $t->{byteCnt} = $stats->{childStats}{TotalFileSize}
412 + $stats->{parentStats}{TotalFileSize};
413 $t->{fileCnt} = $stats->{childStats}{TotalFileCnt}
414 + $stats->{parentStats}{TotalFileCnt};
415 my $str = "Done: $t->{fileCnt} files, $t->{byteCnt} bytes\n";
416 $t->{XferLOG}->write(\$str);
418 # TODO: get error count, and call fio to get stats...
420 $t->{hostError} = $error if ( defined($error) );
422 if ( $t->{type} eq "restore" ) {
432 $stats->{childStats}{ExistFileCnt}
433 + $stats->{parentStats}{ExistFileCnt},
434 $stats->{childStats}{ExistFileSize}
435 + $stats->{parentStats}{ExistFileSize},
436 $stats->{childStats}{ExistFileCompSize}
437 + $stats->{parentStats}{ExistFileCompSize},
438 $stats->{childStats}{TotalFileCnt}
439 + $stats->{parentStats}{TotalFileCnt},
440 $stats->{childStats}{TotalFileSize}
441 + $stats->{parentStats}{TotalFileSize},
448 my($t, $reason) = @_;
457 my($t, $FDreadRef) = @_;
464 return $RsyncLibErr if ( !defined($t) || ref($t) ne "HASH" );
465 return $t->{_errStr};
479 push(@{$t->{_logMsg}}, $msg);
486 return shift(@{$t->{_logMsg}});
490 # Returns a hash ref giving various status information about
497 return { map { $_ => $t->{$_} }
498 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
499 xferOK hostAbort hostError lastOutputLine)
507 return @{$t->{badFiles}};