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-2007 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.2.0beta0, released 17 Jan 2009.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::Xfer::Rsync;
42 use BackupPC::Xfer::RsyncFileIO;
43 use Encode qw/from_to encode/;
44 use base qw(BackupPC::Xfer::Protocol);
46 use vars qw( $RsyncLibOK $RsyncLibErr );
49 eval "use File::RsyncP;";
52 # Rsync module doesn't exist.
55 $RsyncLibErr = "File::RsyncP module doesn't exist";
58 # Note: also update configure.pl when this version number is changed!
60 if ( $File::RsyncP::VERSION < 0.68 ) {
62 $RsyncLibErr = "File::RsyncP module version"
63 . " ($File::RsyncP::VERSION) too old: need >= 0.68";
72 my($class, $bpc, $args) = @_;
74 return if ( !$RsyncLibOK );
75 my $t = BackupPC::Xfer::Protocol->new($bpc, $args);
76 return bless($t, $class);
83 my $conf = $t->{conf};
84 my(@fileList, $rsyncClientCmd, $rsyncArgs, $logMsg,
85 $incrDate, $argList, $fioArgs);
88 # We add a slash to the share name we pass to rsync
90 ($t->{shareNameSlash} = "$t->{shareName}/") =~ s{//+$}{/};
92 if ( $t->{type} eq "restore" ) {
93 $rsyncClientCmd = $conf->{RsyncClientRestoreCmd};
94 $rsyncArgs = $conf->{RsyncRestoreArgs};
95 my $remoteDir = "$t->{shareName}/$t->{pathHdrDest}";
96 $remoteDir =~ s{//+}{/}g;
97 from_to($remoteDir, "utf8", $conf->{ClientCharset})
98 if ( $conf->{ClientCharset} ne "" );
99 $argList = ['--server', @$rsyncArgs, '.', $remoteDir];
101 client => $t->{bkupSrcHost},
102 share => $t->{bkupSrcShare},
103 viewNum => $t->{bkupSrcNum},
104 fileList => $t->{fileList},
106 $logMsg = "restore started below directory $t->{shareName}"
107 . " to host $t->{host}";
110 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
111 # into a hash of arrays of files, and $conf->{RsyncShareName}
114 $bpc->backupFileConfFix($conf, "RsyncShareName");
116 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
117 my(@inc, @exc, %incDone, %excDone);
118 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
120 # If the user wants to just include /home/craig, then
121 # we need to do create include/exclude pairs at
123 # --include /home --exclude /*
124 # --include /home/craig --exclude /home/*
126 # It's more complex if the user wants to include multiple
127 # deep paths. For example, if they want /home/craig and
128 # /var/log, then we need this mouthfull:
129 # --include /home --include /var --exclude /*
130 # --include /home/craig --exclude /home/*
131 # --include /var/log --exclude /var/*
133 # To make this easier we do all the includes first and all
134 # of the excludes at the end (hopefully they commute).
139 if ( $file eq "/" ) {
141 # This is a special case: if the user specifies
142 # "/" then just include it and don't exclude "/*".
144 push(@inc, $file) if ( !$incDone{$file} );
148 while ( $file =~ m{^/([^/]*)(.*)} ) {
151 if ( $file eq "/" ) {
153 # preserve a tailing slash
158 push(@exc, "$f/*") if ( !$excDone{"$f/*"} );
159 $excDone{"$f/*"} = 1;
161 push(@inc, $f) if ( !$incDone{$f} );
165 foreach my $file ( @inc ) {
166 $file = encode($conf->{ClientCharset}, $file)
167 if ( $conf->{ClientCharset} ne "" );
168 push(@fileList, "--include=$file");
170 foreach my $file ( @exc ) {
171 $file = encode($conf->{ClientCharset}, $file)
172 if ( $conf->{ClientCharset} ne "" );
173 push(@fileList, "--exclude=$file");
176 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
177 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
180 # just append additional exclude lists onto the end
182 $file = encode($conf->{ClientCharset}, $file)
183 if ( $conf->{ClientCharset} ne "" );
184 push(@fileList, "--exclude=$file");
187 if ( $t->{type} eq "full" ) {
188 if ( $t->{partialNum} ) {
189 $logMsg = "full backup started for directory $t->{shareName};"
190 . " updating partial #$t->{partialNum}";
192 $logMsg = "full backup started for directory $t->{shareName}";
193 if ( $t->{incrBaseBkupNum} ne "" ) {
194 $logMsg .= " (baseline backup #$t->{incrBaseBkupNum})";
198 $incrDate = $bpc->timeStamp($t->{incrBaseTime}, 1);
199 $logMsg = "incr backup started back to $incrDate"
200 . " (backup #$t->{incrBaseBkupNum}) for directory"
201 . " $t->{shareName}";
205 # A full dump is implemented with --ignore-times: this causes all
206 # files to be checksummed, even if the attributes are the same.
207 # That way all the file contents are checked, but you get all
208 # the efficiencies of rsync: only files deltas need to be
209 # transferred, even though it is a full dump.
211 $rsyncArgs = $conf->{RsyncArgs};
212 $rsyncArgs = [@$rsyncArgs, @fileList] if ( @fileList );
213 $rsyncArgs = [@$rsyncArgs, "--ignore-times"]
214 if ( $t->{type} eq "full" );
215 $rsyncClientCmd = $conf->{RsyncClientCmd};
216 my $shareNameSlash = $t->{shareNameSlash};
217 from_to($shareNameSlash, "utf8", $conf->{ClientCharset})
218 if ( $conf->{ClientCharset} ne "" );
219 $argList = ['--server', '--sender', @$rsyncArgs,
220 '.', $shareNameSlash];
222 $argList = File::RsyncP->excludeStrip($argList);
225 client => $t->{client},
226 share => $t->{shareName},
227 viewNum => $t->{incrBaseBkupNum},
228 partialNum => $t->{partialNum},
233 # Merge variables into $rsyncClientCmd
237 hostIP => $t->{hostIP},
238 client => $t->{client},
239 shareName => $t->{shareName},
240 shareNameSlash => $t->{shareNameSlash},
241 rsyncPath => $conf->{RsyncClientPath},
242 sshPath => $conf->{SshPath},
245 from_to($args->{shareName}, "utf8", $conf->{ClientCharset})
246 if ( $conf->{ClientCharset} ne "" );
247 from_to($args->{shareNameSlash}, "utf8", $conf->{ClientCharset})
248 if ( $conf->{ClientCharset} ne "" );
249 $rsyncClientCmd = $bpc->cmdVarSubstitute($rsyncClientCmd, $args);
252 # Create the Rsync object, and tell it to use our own File::RsyncP::FileIO
253 # module, which handles all the special BackupPC file storage
254 # (compression, mangling, hardlinks, special files, attributes etc).
256 $t->{rsyncClientCmd} = $rsyncClientCmd;
257 $t->{rs} = File::RsyncP->new({
258 logLevel => $t->{logLevel} || $conf->{RsyncLogLevel},
261 $bpc->cmdExecOrEval($rsyncClientCmd, $args);
263 rsyncCmdType => "full",
264 rsyncArgs => $rsyncArgs,
265 timeout => $conf->{ClientTimeout},
266 doPartial => defined($t->{partialNum}) ? 1 : undef,
271 $t->{XferLOG}->write(\$str);
272 if ( $str =~ /^Remote\[1\]: read errors mapping "(.*)"/ ) {
274 # Files with read errors (eg: region locked files
275 # on WinXX) are filled with 0 by rsync. Remember
276 # them and delete them later.
279 $badFile =~ s/^\/+//;
280 push(@{$t->{badFiles}}, {
281 share => $t->{shareName},
287 $t->{pidHandler}(@_);
289 completionPercent => sub {
290 $t->{completionPercent}(@_);
292 clientCharset => $conf->{ClientCharset},
293 fio => BackupPC::Xfer::RsyncFileIO->new({
297 backups => $t->{backups},
298 logLevel => $t->{logLevel}
299 || $conf->{RsyncLogLevel},
303 $t->{XferLOG}->write(\$str);
305 cacheCheckProb => $conf->{RsyncCsumCacheVerifyProb},
306 clientCharset => $conf->{ClientCharset},
311 delete($t->{_errStr});
320 my $conf = $t->{conf};
321 my($remoteSend, $remoteDir, $remoteDirDaemon);
323 alarm($conf->{ClientTimeout});
324 if ( $t->{type} eq "restore" ) {
326 ($remoteDir = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
327 ($remoteDirDaemon = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
328 $remoteDirDaemon = $t->{shareNameSlash}
329 if ( $t->{pathHdrDest} eq ""
330 || $t->{pathHdrDest} eq "/" );
333 $remoteDir = $t->{shareNameSlash};
334 $remoteDirDaemon = ".";
336 from_to($remoteDir, "utf8", $conf->{ClientCharset})
337 if ( $conf->{ClientCharset} ne "" );
338 from_to($remoteDirDaemon, "utf8", $conf->{ClientCharset})
339 if ( $conf->{ClientCharset} ne "" );
341 if ( $t->{XferMethod} eq "rsync" ) {
345 my $str = "Running: "
346 . $t->{bpc}->execCmd2ShellCmd(@{$t->{rsyncClientCmd}})
348 from_to($str, $conf->{ClientCharset}, "utf8")
349 if ( $conf->{ClientCharset} ne "" );
350 $t->{XferLOG}->write(\$str);
351 $rs->remoteStart($remoteSend, $remoteDir);
354 # Connect to the rsync server
356 if ( defined(my $err = $rs->serverConnect($t->{hostIP},
357 $conf->{RsyncdClientPort})) ) {
358 $t->{hostError} = $err;
359 my $str = "Error connecting to rsync daemon at $t->{hostIP}"
360 . ":$conf->{RsyncdClientPort}: $err\n";
361 $t->{XferLOG}->write(\$str);
365 # Pass module name, and follow it with a slash if it already
366 # contains a slash; otherwise just keep the plain module name.
368 my $module = $t->{shareName};
369 $module = $t->{shareNameSlash} if ( $module =~ /\// );
370 from_to($module, "utf8", $conf->{ClientCharset})
371 if ( $conf->{ClientCharset} ne "" );
372 if ( defined(my $err = $rs->serverService($module,
373 $conf->{RsyncdUserName},
374 $conf->{RsyncdPasswd},
375 $conf->{RsyncdAuthRequired})) ) {
376 my $str = "Error connecting to module $module at $t->{hostIP}"
377 . ":$conf->{RsyncdClientPort}: $err\n";
378 $t->{XferLOG}->write(\$str);
379 $t->{hostError} = $err;
384 # This is a hack. To avoid wide chars we encode the arguments
385 # to utf8 byte streams, then to the client's local charset.
386 # The second conversion should really go in File::RsyncP, since
387 # it shouldn't be applied to in-line include/exclude arguments.
389 for ( my $i = 0 ; $i < @{$rs->{rsyncArgs}} ; $i++ ) {
390 $rs->{rsyncArgs}[$i] = encode('utf8', $rs->{rsyncArgs}[$i]);
391 from_to($rs->{rsyncArgs}[$i], 'utf8', $conf->{ClientCharset})
392 if ( $conf->{ClientCharset} ne "" );
395 my $str = "RsyncArgsBefore: " . join(" ", @{$rs->{rsyncArgs}}) . "\n";
396 $t->{XferLOG}->write(\$str);
398 $rs->serverStart($remoteSend, $remoteDirDaemon);
400 my $str = "RsyncArgsAfter: " . join(" ", @{$rs->{rsyncArgs}}) . "\n";
401 $t->{XferLOG}->write(\$str);
403 my $shareNameSlash = $t->{shareNameSlash};
404 from_to($shareNameSlash, "utf8", $conf->{ClientCharset})
405 if ( $conf->{ClientCharset} ne "" );
406 my $error = $rs->go($shareNameSlash);
410 # TODO: generate sensible stats
412 # $rs->{stats}{totalWritten}
413 # $rs->{stats}{totalSize}
415 my $stats = $rs->statsFinal;
416 if ( !defined($error) && defined($stats) ) {
421 $t->{xferErrCnt} = $stats->{remoteErrCnt}
422 + $stats->{childStats}{errorCnt}
423 + $stats->{parentStats}{errorCnt};
424 $t->{byteCnt} = $stats->{childStats}{TotalFileSize}
425 + $stats->{parentStats}{TotalFileSize};
426 $t->{fileCnt} = $stats->{childStats}{TotalFileCnt}
427 + $stats->{parentStats}{TotalFileCnt};
428 my $str = "Done: $t->{fileCnt} files, $t->{byteCnt} bytes\n";
429 $t->{XferLOG}->write(\$str);
431 # TODO: get error count, and call fio to get stats...
433 $t->{hostError} = $error if ( defined($error) );
435 if ( $t->{type} eq "restore" ) {
445 $stats->{childStats}{ExistFileCnt}
446 + $stats->{parentStats}{ExistFileCnt},
447 $stats->{childStats}{ExistFileSize}
448 + $stats->{parentStats}{ExistFileSize},
449 $stats->{childStats}{ExistFileCompSize}
450 + $stats->{parentStats}{ExistFileCompSize},
451 $stats->{childStats}{TotalFileCnt}
452 + $stats->{parentStats}{TotalFileCnt},
453 $stats->{childStats}{TotalFileSize}
454 + $stats->{parentStats}{TotalFileSize},
461 my($t, $reason) = @_;
472 return $RsyncLibErr if ( !defined($t) || ref($t) ne "HASH" );
473 return $t->{_errStr};