1 #============================================================= -*-perl-*-
3 # BackupPC::Xfer::Smb package
7 # This library defines a BackupPC::Xfer::Smb class for managing
8 # the SMB (smbclient) transport of backup data from the client.
11 # Craig Barratt <cbarratt@users.sourceforge.net>
14 # Copyright (C) 2001 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::Smb;
44 my($class, $bpc, $args) = @_;
49 conf => { $bpc->Conf },
66 foreach my $arg ( keys(%$args) ) {
67 $t->{$arg} = $args->{$arg};
80 my $conf = $t->{conf};
81 my $I_option = $t->{hostIP} eq $t->{host} ? "" : " -I $t->{hostIP}";
82 my($fileList, $optX, $smbClientCmd, $logMsg);
86 # First propagate the PASSWD setting
88 $ENV{PASSWD} = $ENV{BPC_SMB_PASSWD} if ( defined($ENV{BPC_SMB_PASSWD}) );
89 $ENV{PASSWD} = $conf->{SmbSharePasswd}
90 if ( defined($conf->{SmbSharePasswd}) );
91 if ( !defined($ENV{PASSWD}) ) {
92 $t->{_errStr} = "passwd not set for smbclient";
95 if ( !defined($conf->{SmbClientPath}) || !-x $conf->{SmbClientPath} ) {
96 $t->{_errStr} = '$Conf{SmbClientPath} is not a valid executable';
99 if ( $t->{type} eq "restore" ) {
101 "$conf->{SmbClientPath} '\\\\$t->{host}\\$t->{shareName}'"
102 . "$I_option -U '$conf->{SmbShareUserName}' -E -N -d 1"
103 . " $conf->{SmbClientArgs}"
104 . " -c 'tarmode full' -Tx -";
105 $logMsg = "restore started for share $t->{shareName}";
108 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
109 # into a hash of arrays of files
111 $conf->{SmbShareName} = [ $conf->{SmbShareName} ]
112 unless ref($conf->{SmbShareName}) eq "ARRAY";
113 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
114 next if ( !defined($conf->{$param}) );
115 if ( ref($conf->{$param}) eq "ARRAY" ) {
117 $conf->{SmbShareName}[0] => $conf->{$param}
119 } elsif ( ref($conf->{$param}) eq "HASH" ) {
123 $conf->{SmbShareName}[0] => [ $conf->{$param} ]
127 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
128 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
130 $fileList .= "'$file' ";
132 } elsif ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
133 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
136 $fileList .= "'$file' ";
139 # Allow simple wildcards in exclude list by specifying "r" option.
143 if ( $t->{type} eq "full" ) {
145 "$conf->{SmbClientPath} '\\\\$t->{host}\\$t->{shareName}'"
146 . "$I_option -U '$conf->{SmbShareUserName}' -E -N -d 1"
147 . " $conf->{SmbClientArgs}"
148 . " -c 'tarmode full'"
149 . " -Tc$optX - $fileList";
150 $logMsg = "full backup started for share $t->{shareName}";
152 my $timeStampFile = "$t->{outDir}/timeStamp.level0";
153 open(LEV0, ">$timeStampFile") && close(LEV0);
154 utime($t->{lastFull} - 3600, $t->{lastFull} - 3600, $timeStampFile);
156 "$conf->{SmbClientPath} '\\\\$t->{host}\\$t->{shareName}'"
157 . "$I_option -U '$conf->{SmbShareUserName}' -E -N -d 1"
158 . " $conf->{SmbClientArgs}"
159 . " -c 'tarmode full'"
160 . " -TcN$optX $timeStampFile - $fileList";
161 $logMsg = "incr backup started back to "
162 . $bpc->timeStamp($t->{lastFull} - 3600, 0)
163 . "for share $t->{shareName}";
166 if ( !defined($t->{xferPid} = open(SMB, "-|")) ) {
167 $t->{_errStr} = "Can't fork to run smbclient";
170 $t->{pipeSMB} = *SMB;
171 if ( !$t->{xferPid} ) {
173 # This is the smbclient child.
176 if ( $t->{type} eq "restore" ) {
178 # For restores close the write end of the pipe,
179 # clone STDIN from RH, and STDERR to STDOUT
183 open(STDERR, ">&STDOUT");
185 open(STDIN, "<&$t->{pipeRH}");
188 # For backups close the read end of the pipe,
189 # clone STDOUT to WH, STDERR to STDOUT
193 open(STDERR, ">&STDOUT");
194 open(STDOUT, ">&$t->{pipeWH}");
200 # should not be reached, but just in case...
201 $t->{_errStr} = "Can't exec $conf->{SmbClientPath}";
204 $t->{XferLOG}->write(\"Running: $smbClientCmd\n");
205 alarm($conf->{SmbClientTimeout});
206 $t->{_errStr} = undef;
212 my($t, $FDreadRef, $rout) = @_;
213 my $conf = $t->{conf};
215 if ( vec($rout, fileno($t->{pipeSMB}), 1) ) {
217 if ( sysread($t->{pipeSMB}, $mesg, 8192) <= 0 ) {
218 vec($$FDreadRef, fileno($t->{pipeSMB}), 1) = 0;
219 close($t->{pipeSMB});
221 $t->{smbOut} .= $mesg;
224 while ( $t->{smbOut} =~ /(.*?)[\n\r]+(.*)/s ) {
227 $t->{XferLOG}->write(\"$_\n");
229 # refresh our inactivity alarm
231 alarm($conf->{SmbClientTimeout});
232 $t->{lastOutputLine} = $_ if ( !/^$/ );
234 # This section is highly dependent on the version of smbclient.
235 # If you upgrade Samba, make sure that these regexp are still valid.
237 if ( /^\s*(-?\d+) \(\s*\d+\.\d kb\/s\) (.*)$/ ) {
238 my $sambaFileSize = $1;
240 (my $fileName = $pcFileName) =~ s/\\/\//g;
241 $sambaFileSize += 1024 * 1024 * 4096 if ( $sambaFileSize < 0 );
242 $fileName =~ s/^\/*//;
243 $t->{byteCnt} += $sambaFileSize;
245 } elsif ( /restore tar file (.*) of size (\d+) bytes/ ) {
248 } elsif ( /tar: dumped \d+ files/ ) {
250 } elsif ( /^tar: restored \d+ files/ ) {
252 } elsif ( /^read_socket_with_timeout: timeout read. /i ) {
254 } elsif ( /^code 0 listing /
255 || /^code 0 opening /
256 || /^abandoning restore/i
257 || /^Error: Looping in FIND_NEXT/i
259 || /^Call timed out: server did not respond/i
261 $t->{hostError} ||= $_;
263 || /^added interface/i
264 || /^tarmode is now/i
265 || /^Total bytes written/i
267 || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/i
268 || /^Getting files newer than/i
269 || /^\s+directory \\/i
270 || /^Output is \/dev\/null/i
271 || /^Timezone is/i ) {
272 # ignore these messages
275 $t->{xferBadShareCnt}++ if ( /^ERRDOS - ERRbadshare/ );
276 $t->{xferBadFileCnt}++ if ( /^ERRDOS - ERRbadfile/ );
277 if ( $t->{xferErrCnt} > 50000 ) {
279 "Too many smbtar errors ($t->{xferErrCnt})... giving up");
280 $t->{hostError} = "Too many smbtar errors ($t->{xferErrCnt})";
283 if ( /^Error reading file (.*)\. Got 0 bytes/ ) {
285 # This happens when a Windoze application has
286 # locked the file. This is a particular problem
287 # with MS-Outlook. smbclient has already written
288 # the tar header to stdout, so all it can do is to
289 # write a dummy file with the correct size, but all
290 # zeros. BackupPC_tarExtract stores these
291 # zero-content files efficiently as a sparse file,
292 # or if compression is on the file will be small
293 # anyhow. After the dump is done we simply delete
294 # the file (it is no use) and try to link it to same
295 # file in any recent backup.
298 $badFile =~ s{\\}{/}g;
300 push(@{$t->{badFiles}}, "$t->{shareName}/$badFile");
309 my($t, $FDreadRef) = @_;
311 vec($$FDreadRef, fileno($t->{pipeSMB}), 1) = 1;
318 return $t->{_errStr};
325 return $t->{xferPid};
332 push(@{$t->{_logMsg}}, $msg);
339 return shift(@{$t->{_logMsg}});
343 # Returns a hash ref giving various status information about
350 return { map { $_ => $t->{$_} }
351 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
352 xferOK hostAbort hostError lastOutputLine)
360 return @{$t->{badFiles}};