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 2.1.0_CVS, released 8 Feb 2004.
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";
55 if ( $File::RsyncP::VERSION < 0.44 ) {
57 $RsyncLibErr = "File::RsyncP module version too old: need 0.44";
66 my($class, $bpc, $args) = @_;
68 return if ( !$RsyncLibOK );
72 conf => { $bpc->Conf },
101 foreach my $arg ( keys(%$args) ) {
102 $t->{$arg} = $args->{$arg};
115 my $conf = $t->{conf};
116 my(@fileList, $rsyncClientCmd, $rsyncArgs, $logMsg,
117 $incrDate, $argList, $fioArgs);
120 # We add a slash to the share name we pass to rsync
122 ($t->{shareNameSlash} = "$t->{shareName}/") =~ s{//+$}{/};
124 if ( $t->{type} eq "restore" ) {
125 $rsyncClientCmd = $conf->{RsyncClientRestoreCmd};
126 $rsyncArgs = $conf->{RsyncRestoreArgs};
127 my $remoteDir = "$t->{shareName}/$t->{pathHdrDest}";
128 $remoteDir =~ s{//+}{/}g;
129 $argList = ['--server', @$rsyncArgs, '.', $remoteDir];
131 client => $t->{bkupSrcHost},
132 share => $t->{bkupSrcShare},
133 viewNum => $t->{bkupSrcNum},
134 fileList => $t->{fileList},
136 $logMsg = "restore started below directory $t->{shareName}"
137 . " to host $t->{host}";
140 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
141 # into a hash of arrays of files.
143 $conf->{RsyncShareName} = [ $conf->{RsyncShareName} ]
144 unless ref($conf->{RsyncShareName}) eq "ARRAY";
145 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
146 next if ( !defined($conf->{$param}) );
147 if ( ref($conf->{$param}) eq "ARRAY" ) {
149 $conf->{RsyncShareName}[0] => $conf->{$param}
151 } elsif ( ref($conf->{$param}) eq "HASH" ) {
155 $conf->{RsyncShareName}[0] => [ $conf->{$param} ]
159 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
160 my(@inc, @exc, %incDone, %excDone);
161 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
163 # If the user wants to just include /home/craig, then
164 # we need to do create include/exclude pairs at
166 # --include /home --exclude /*
167 # --include /home/craig --exclude /home/*
169 # It's more complex if the user wants to include multiple
170 # deep paths. For example, if they want /home/craig and
171 # /var/log, then we need this mouthfull:
172 # --include /home --include /var --exclude /*
173 # --include /home/craig --exclude /home/*
174 # --include /var/log --exclude /var/*
176 # To make this easier we do all the includes first and all
177 # of the excludes at the end (hopefully they commute).
182 if ( $file eq "/" ) {
184 # This is a special case: if the user specifies
185 # "/" then just include it and don't exclude "/*".
187 push(@inc, $file) if ( !$incDone{$file} );
191 while ( $file =~ m{^/([^/]*)(.*)} ) {
194 if ( $file eq "/" ) {
196 # preserve a tailing slash
201 push(@exc, "$f/*") if ( !$excDone{"$f/*"} );
202 $excDone{"$f/*"} = 1;
204 push(@inc, $f) if ( !$incDone{$f} );
208 foreach my $file ( @inc ) {
209 push(@fileList, "--include=$file");
211 foreach my $file ( @exc ) {
212 push(@fileList, "--exclude=$file");
215 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
216 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
219 # just append additional exclude lists onto the end
221 push(@fileList, "--exclude=$file");
224 if ( $t->{type} eq "full" ) {
225 $logMsg = "full backup started for directory $t->{shareName}";
227 $incrDate = $bpc->timeStamp($t->{lastFull} - 3600, 1);
228 $logMsg = "incr backup started back to $incrDate 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 client => $t->{client},
248 share => $t->{shareName},
249 viewNum => $t->{lastFullBkupNum},
250 partialNum => $t->{partialNum},
255 # Merge variables into $rsyncClientCmd
259 hostIP => $t->{hostIP},
260 client => $t->{client},
261 shareName => $t->{shareName},
262 shareNameSlash => $t->{shareNameSlash},
263 rsyncPath => $conf->{RsyncClientPath},
264 sshPath => $conf->{SshPath},
267 $rsyncClientCmd = $bpc->cmdVarSubstitute($rsyncClientCmd, $args);
270 # Create the Rsync object, and tell it to use our own File::RsyncP::FileIO
271 # module, which handles all the special BackupPC file storage
272 # (compression, mangling, hardlinks, special files, attributes etc).
274 $t->{rsyncClientCmd} = $rsyncClientCmd;
275 $t->{rs} = File::RsyncP->new({
276 logLevel => $conf->{RsyncLogLevel},
279 $bpc->cmdExecOrEval($rsyncClientCmd, $args);
281 rsyncCmdType => "full",
282 rsyncArgs => $rsyncArgs,
283 timeout => $conf->{ClientTimeout},
287 $t->{XferLOG}->write(\$str);
290 $t->{pidHandler}(@_);
292 fio => BackupPC::Xfer::RsyncFileIO->new({
296 backups => $t->{backups},
297 logLevel => $conf->{RsyncLogLevel},
301 $t->{XferLOG}->write(\$str);
307 delete($t->{_errStr});
316 my $conf = $t->{conf};
317 my($remoteSend, $remoteDir, $remoteDirDaemon);
319 alarm($conf->{ClientTimeout});
320 if ( $t->{type} eq "restore" ) {
322 ($remoteDir = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
323 ($remoteDirDaemon = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
324 $remoteDirDaemon = $t->{shareNameSlash}
325 if ( $t->{pathHdrDest} eq ""
326 || $t->{pathHdrDest} eq "/" );
329 $remoteDir = $t->{shareNameSlash};
330 $remoteDirDaemon = ".";
332 if ( $t->{XferMethod} eq "rsync" ) {
336 my $str = "Running: "
337 . $t->{bpc}->execCmd2ShellCmd(@{$t->{rsyncClientCmd}})
339 $t->{XferLOG}->write(\$str);
340 $rs->remoteStart($remoteSend, $remoteDir);
343 # Connect to the rsync server
345 if ( defined(my $err = $rs->serverConnect($t->{hostIP},
346 $conf->{RsyncdClientPort})) ) {
347 $t->{hostError} = $err;
348 my $str = "Error connecting to rsync daemon at $t->{hostIP}"
349 . ":$conf->{RsyncdClientPort}: $err\n";
350 $t->{XferLOG}->write(\$str);
354 # Pass module name, and follow it with a slash if it already
355 # contains a slash; otherwise just keep the plain module name.
357 my $module = $t->{shareName};
358 $module = $t->{shareNameSlash} if ( $module =~ /\// );
359 if ( defined(my $err = $rs->serverService($module,
360 $conf->{RsyncdUserName},
361 $conf->{RsyncdPasswd},
362 $conf->{RsyncdAuthRequired})) ) {
363 my $str = "Error connecting to module $module at $t->{hostIP}"
364 . ":$conf->{RsyncdClientPort}: $err\n";
365 $t->{XferLOG}->write(\$str);
366 $t->{hostError} = $err;
369 $rs->serverStart($remoteSend, $remoteDirDaemon);
371 my $error = $rs->go($t->{shareNameSlash});
375 # TODO: generate sensible stats
377 # $rs->{stats}{totalWritten}
378 # $rs->{stats}{totalSize}
380 my $stats = $rs->statsFinal;
381 if ( !defined($error) && defined($stats) ) {
386 $t->{xferErrCnt} = $stats->{remoteErrCnt}
387 + $stats->{childStats}{errorCnt}
388 + $stats->{parentStats}{errorCnt};
389 $t->{byteCnt} = $stats->{childStats}{TotalFileSize}
390 + $stats->{parentStats}{TotalFileSize};
391 $t->{fileCnt} = $stats->{childStats}{TotalFileCnt}
392 + $stats->{parentStats}{TotalFileCnt};
393 my $str = "Done: $t->{fileCnt} files, $t->{byteCnt} bytes\n";
394 $t->{XferLOG}->write(\$str);
396 # TODO: get error count, and call fio to get stats...
398 $t->{hostError} = $error if ( defined($error) );
400 if ( $t->{type} eq "restore" ) {
410 $stats->{childStats}{ExistFileCnt}
411 + $stats->{parentStats}{ExistFileCnt},
412 $stats->{childStats}{ExistFileSize}
413 + $stats->{parentStats}{ExistFileSize},
414 $stats->{childStats}{ExistFileCompSize}
415 + $stats->{parentStats}{ExistFileCompSize},
416 $stats->{childStats}{TotalFileCnt}
417 + $stats->{parentStats}{TotalFileCnt},
418 $stats->{childStats}{TotalFileSize}
419 + $stats->{parentStats}{TotalFileSize},
426 my($t, $FDreadRef) = @_;
433 return $RsyncLibErr if ( !defined($t) || ref($t) ne "HASH" );
434 return $t->{_errStr};
448 push(@{$t->{_logMsg}}, $msg);
455 return shift(@{$t->{_logMsg}});
459 # Returns a hash ref giving various status information about
466 return { map { $_ => $t->{$_} }
467 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
468 xferOK hostAbort hostError lastOutputLine)
476 return @{$t->{badFiles}};