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 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 1.6.0_CVS, released 10 Dec 2002.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::Xfer::Rsync;
42 use BackupPC::Xfer::RsyncFileIO;
44 use vars qw( $RsyncLibOK );
47 eval "use File::RsyncP;";
50 # Rsync module doesn't exist.
60 my($class, $bpc, $args) = @_;
62 return if ( !$RsyncLibOK );
66 conf => { $bpc->Conf },
81 foreach my $arg ( keys(%$args) ) {
82 $t->{$arg} = $args->{$arg};
95 my $conf = $t->{conf};
96 my(@fileList, @rsyncClientCmd, $logMsg, $incrDate);
98 if ( $t->{type} eq "restore" ) {
100 #push(@rsyncClientCmd, split(/ +/, $c o n f->{RsyncClientRestoreCmd}));
101 $logMsg = "restore not supported for $t->{shareName}";
103 # restores are considered to work unless we see they fail
104 # (opposite to backups...)
109 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
110 # into a hash of arrays of files. NOT IMPLEMENTED YET.
112 $conf->{RsyncShareName} = [ $conf->{RsyncShareName} ]
113 unless ref($conf->{RsyncShareName}) eq "ARRAY";
114 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
115 next if ( !defined($conf->{$param}) );
116 if ( ref($conf->{$param}) eq "ARRAY" ) {
118 $conf->{RsyncShareName}[0] => $conf->{$param}
120 } elsif ( ref($conf->{$param}) eq "HASH" ) {
124 $conf->{RsyncShareName}[0] => [ $conf->{$param} ]
128 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
129 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
131 push(@fileList, "--exclude=$file");
134 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
135 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
136 push(@fileList, $file);
139 push(@fileList, ".");
141 push(@rsyncClientCmd, split(/ +/, $conf->{RsyncClientCmd}));
142 if ( $t->{type} eq "full" ) {
143 $logMsg = "full backup started for directory $t->{shareName}";
145 $incrDate = $bpc->timeStampISO($t->{lastFull} - 3600, 1);
146 $logMsg = "incr backup started back to $incrDate for directory"
147 . " $t->{shareName}";
152 # Merge variables into @rsyncClientCmd
156 hostIP => $t->{hostIP},
157 shareName => $t->{shareName},
158 rsyncPath => $conf->{RsyncClientPath},
159 sshPath => $conf->{SshPath},
161 my @cmd = @rsyncClientCmd;
162 @rsyncClientCmd = ();
163 foreach my $arg ( @cmd ) {
164 next if ( $arg =~ /^\s*$/ );
165 if ( $arg =~ /^\$fileList(\+?)/ ) {
167 foreach $arg ( @fileList ) {
168 $arg = $bpc->shellEscape($arg) if ( $esc );
169 push(@rsyncClientCmd, $arg);
171 } elsif ( $arg =~ /^\$argList(\+?)/ ) {
173 foreach $arg ( (@{$conf->{RsyncArgs}},
174 @{$conf->{RsyncClientArgs}}) ) {
175 $arg = $bpc->shellEscape($arg) if ( $esc );
176 push(@rsyncClientCmd, $arg);
179 $arg =~ s{\$(\w+)(\+?)}{
181 ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
184 push(@rsyncClientCmd, $arg);
189 # A full dump is implemented with --ignore-times: this causes all
190 # files to be checksummed, even if the attributes are the same.
191 # That way all the file contents are checked, but you get all
192 # the efficiencies of rsync: only files deltas need to be
193 # transferred, even though it is a full dump.
195 my $rsyncArgs = $conf->{RsyncArgs};
196 $rsyncArgs = [@$rsyncArgs, "--ignore-times"] if ( $t->{type} eq "full" );
199 # Create the Rsync object, and tell it to use our own File::RsyncP::FileIO
200 # module, which handles all the special BackupPC file storage
201 # (compression, mangling, hardlinks, special files, attributes etc).
203 $t->{rs} = File::RsyncP->new({
204 logLevel => $conf->{RsyncLogLevel},
205 rsyncCmd => \@rsyncClientCmd,
206 rsyncArgs => $rsyncArgs,
210 $t->{XferLOG}->write(\$str);
212 fio => BackupPC::Xfer::RsyncFileIO->new({
217 backups => $t->{backups},
218 logLevel => $conf->{RsyncLogLevel},
222 # TODO: alarm($conf->{SmbClientTimeout});
223 delete($t->{_errStr});
232 my $conf = $t->{conf};
234 if ( $t->{XferMethod} eq "rsync" ) {
238 $rs->remoteStart(1, $t->{shareName});
241 # Connect to the rsync server
243 if ( defined(my $err = $rs->serverConnect($t->{hostIP},
244 $conf->{RsyncdClientPort})) ) {
245 $t->{hostError} = $err;
248 if ( defined(my $err = $rs->serverService($t->{shareName},
249 "craig", "xyz123", 0)) ) {
250 $t->{hostError} = $err;
253 $rs->serverStart(1, ".");
255 my $error = $rs->go($t->{shareName});
259 # TODO: generate sensible stats
261 # $rs->{stats}{totalWritten}
262 # $rs->{stats}{totalSize}
264 # qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
265 # xferOK hostAbort hostError lastOutputLine)
267 my $stats = $rs->statsFinal;
268 if ( !defined($error) && defined($stats) ) {
273 $t->{byteCnt} = $stats->{childStats}{TotalFileSize}
274 + $stats->{parentStats}{TotalFileSize};
275 $t->{fileCnt} = $stats->{childStats}{TotalFileCnt}
276 + $stats->{parentStats}{TotalFileCnt};
278 # TODO: get error count, and call fio to get stats...
280 $t->{hostError} = $error if ( defined($error) );
284 $stats->{childStats}{ExistFileCnt}
285 + $stats->{parentStats}{ExistFileCnt},
286 $stats->{childStats}{ExistFileSize}
287 + $stats->{parentStats}{ExistFileSize},
288 $stats->{childStats}{ExistFileCompSize}
289 + $stats->{parentStats}{ExistFileCompSize},
290 $stats->{childStats}{TotalFileCnt}
291 + $stats->{parentStats}{TotalFileCnt},
292 $stats->{childStats}{TotalFileSize}
293 + $stats->{parentStats}{TotalFileSize},
297 # alarm($conf->{SmbClientTimeout});
301 my($t, $FDreadRef) = @_;
308 return $t->{_errStr};
322 push(@{$t->{_logMsg}}, $msg);
329 return shift(@{$t->{_logMsg}});
333 # Returns a hash ref giving various status information about
340 return { map { $_ => $t->{$_} }
341 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
342 xferOK hostAbort hostError lastOutputLine)
350 return @{$t->{badFiles}};