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-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.0beta1, released 9 Apr 2004.
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, $X_option, $smbClientCmd, $logMsg);
87 # First propagate the PASSWD setting
89 $ENV{PASSWD} = $ENV{BPC_SMB_PASSWD} if ( defined($ENV{BPC_SMB_PASSWD}) );
90 $ENV{PASSWD} = $conf->{SmbSharePasswd}
91 if ( defined($conf->{SmbSharePasswd}) );
92 if ( !defined($ENV{PASSWD}) ) {
93 $t->{_errStr} = "passwd not set for smbclient";
96 if ( !defined($conf->{SmbClientPath}) || !-x $conf->{SmbClientPath} ) {
97 $t->{_errStr} = '$Conf{SmbClientPath} is not a valid executable';
100 if ( $t->{type} eq "restore" ) {
101 $smbClientCmd = $conf->{SmbClientRestoreCmd};
102 $logMsg = "restore started for share $t->{shareName}";
105 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
106 # into a hash of arrays of files
108 $conf->{SmbShareName} = [ $conf->{SmbShareName} ]
109 unless ref($conf->{SmbShareName}) eq "ARRAY";
110 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
111 next if ( !defined($conf->{$param}) );
112 if ( ref($conf->{$param}) eq "ARRAY" ) {
114 $conf->{SmbShareName}[0] => $conf->{$param}
116 } elsif ( ref($conf->{$param}) eq "HASH" ) {
120 $conf->{SmbShareName}[0] => [ $conf->{$param} ]
124 $t->{fileIncludeHash} = {};
125 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
126 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
127 push(@fileList, $file);
128 $t->{fileIncludeHash}{$file} = 1;
130 } elsif ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
131 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
133 push(@fileList, $file);
136 # Allow simple wildcards in exclude list by specifying "r" option.
140 if ( $t->{type} eq "full" ) {
141 $smbClientCmd = $conf->{SmbClientFullCmd};
142 $logMsg = "full backup started for share $t->{shareName}";
144 $timeStampFile = "$t->{outDir}/timeStamp.level0";
145 open(LEV0, ">", $timeStampFile) && close(LEV0);
146 utime($t->{lastFull} - 3600, $t->{lastFull} - 3600, $timeStampFile);
147 $smbClientCmd = $conf->{SmbClientIncrCmd};
148 $logMsg = "incr backup started back to "
149 . $bpc->timeStamp($t->{lastFull} - 3600, 0)
150 . "for share $t->{shareName}";
154 smbClientPath => $conf->{SmbClientPath},
156 hostIP => $t->{hostIP},
157 client => $t->{client},
158 shareName => $t->{shareName},
159 userName => $conf->{SmbShareUserName},
160 fileList => \@fileList,
161 I_option => $I_option,
162 X_option => $X_option,
163 timeStampFile => $timeStampFile,
165 $smbClientCmd = $bpc->cmdVarSubstitute($smbClientCmd, $args);
167 if ( !defined($t->{xferPid} = open(SMB, "-|")) ) {
168 $t->{_errStr} = "Can't fork to run smbclient";
171 $t->{pipeSMB} = *SMB;
172 if ( !$t->{xferPid} ) {
174 # This is the smbclient child.
177 if ( $t->{type} eq "restore" ) {
179 # For restores close the write end of the pipe,
180 # clone STDIN from RH, and STDERR to STDOUT
184 open(STDERR, ">&STDOUT");
186 open(STDIN, "<&$t->{pipeRH}");
189 # For backups close the read end of the pipe,
190 # clone STDOUT to WH, STDERR to STDOUT
194 open(STDERR, ">&STDOUT");
195 open(STDOUT, ">&$t->{pipeWH}");
201 $bpc->cmdExecOrEval($smbClientCmd, $args);
202 # should not be reached, but just in case...
203 $t->{_errStr} = "Can't exec $conf->{SmbClientPath}";
206 my $str = "Running: " . $bpc->execCmd2ShellCmd(@$smbClientCmd) . "\n";
207 $t->{XferLOG}->write(\$str);
208 alarm($conf->{ClientTimeout});
209 $t->{_errStr} = undef;
215 my($t, $FDreadRef, $rout) = @_;
216 my $conf = $t->{conf};
218 if ( vec($rout, fileno($t->{pipeSMB}), 1) ) {
220 if ( sysread($t->{pipeSMB}, $mesg, 8192) <= 0 ) {
221 vec($$FDreadRef, fileno($t->{pipeSMB}), 1) = 0;
222 close($t->{pipeSMB});
224 $t->{smbOut} .= $mesg;
227 while ( $t->{smbOut} =~ /(.*?)[\n\r]+(.*)/s ) {
231 # ignore the log file time stamps from smbclient introduced
232 # in version 3.0.0 - don't even write them to the log file.
234 if ( m{^\[\d+/\d+/\d+ +\d+:\d+:\d+.*\] +(client/cli|lib/util_unistr).*\(\d+\)} ) {
235 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 5 );
239 # refresh our inactivity alarm
241 alarm($conf->{ClientTimeout}) if ( !$t->{abort} );
242 $t->{lastOutputLine} = $_ if ( !/^$/ );
244 # This section is highly dependent on the version of smbclient.
245 # If you upgrade Samba, make sure that these regexp are still valid.
247 if ( /^\s*(-?\d+) \(\s*\d+[.,]\d kb\/s\) (.*)$/ ) {
248 my $sambaFileSize = $1;
250 (my $fileName = $pcFileName) =~ s/\\/\//g;
251 $sambaFileSize += 1024 * 1024 * 4096 if ( $sambaFileSize < 0 );
252 $fileName =~ s/^\/*//;
253 $t->{byteCnt} += $sambaFileSize;
255 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 2 );
256 } elsif ( /restore tar file (.*) of size (\d+) bytes/ ) {
259 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
260 } elsif ( /^\s*tar: dumped \d+ files/ ) {
262 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
263 } elsif ( /^\s*tar: restored \d+ files/ ) {
265 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
266 } elsif ( /^\s*read_socket_with_timeout: timeout read. /i ) {
268 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
269 } elsif ( /^code 0 listing /
270 || /^\s*code 0 opening /
271 || /^\s*abandoning restore/i
272 || /^\s*Error: Looping in FIND_NEXT/i
273 || /^\s*SUCCESS - 0/i
274 || /^\s*Call timed out: server did not respond/i
275 || /^\s*tree connect failed: ERRDOS - ERRnoaccess \(Access denied\.\)/
276 || /^\s*tree connect failed: NT_STATUS_BAD_NETWORK_NAME/
278 if ( $t->{hostError} eq "" ) {
279 $t->{XferLOG}->write(\"This backup will fail because: $_\n");
280 $t->{hostError} = $_;
282 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
283 } elsif ( /^\s*NT_STATUS_ACCESS_DENIED listing (.*)/
284 || /^\s*ERRDOS - ERRnoaccess \(Access denied\.\) listing (.*)/ ) {
286 $badDir =~ s{\\}{/}g;
287 $badDir =~ s{/+}{/}g;
288 $badDir =~ s{/\*$}{};
289 if ( $t->{hostError} eq ""
290 && ($badDir eq "" || $t->{fileIncludeHash}{$badDir}) ) {
291 $t->{XferLOG}->write(\"This backup will fail because: $_\n");
292 $t->{hostError} ||= $_;
294 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
295 } elsif ( /^\s*directory \\/i ) {
296 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 2 );
298 || /^\s*added interface/i
299 || /^\s*tarmode is now/i
300 || /^\s*Total bytes written/i
302 || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/i
303 || /^\s*Getting files newer than/i
304 || /^\s*restore directory \\/i
305 || /^\s*Output is \/dev\/null/i
306 || /^\s*Timezone is/i
307 || /^\s*creating lame (up|low)case table/i
309 # ignore these messages
310 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
313 $t->{xferBadShareCnt}++ if ( /^ERRDOS - ERRbadshare/ );
314 $t->{xferBadFileCnt}++ if ( /^ERRDOS - ERRbadfile/ );
315 if ( $t->{xferErrCnt} > 50000 ) {
317 "Too many smbtar errors ($t->{xferErrCnt})... giving up");
318 $t->{hostError} = "Too many smbtar errors ($t->{xferErrCnt})";
321 if ( /^Error reading file (.*)\. Got 0 bytes/ ) {
323 # This happens when a Windoze application has
324 # locked the file. This is a particular problem
325 # with MS-Outlook. smbclient has already written
326 # the tar header to stdout, so all it can do is to
327 # write a dummy file with the correct size, but all
328 # zeros. BackupPC_tarExtract stores these
329 # zero-content files efficiently as a sparse file,
330 # or if compression is on the file will be small
331 # anyhow. After the dump is done we simply delete
332 # the file (it is no use) and try to link it to same
333 # file in any recent backup.
336 $badFile =~ s{\\}{/}g;
338 push(@{$t->{badFiles}}, {
339 share => $t->{shareName},
343 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
351 my($t, $reason) = @_;
354 $t->{abortReason} = $reason;
359 my($t, $FDreadRef) = @_;
361 vec($$FDreadRef, fileno($t->{pipeSMB}), 1) = 1;
368 return $t->{_errStr};
375 return ($t->{xferPid});
382 push(@{$t->{_logMsg}}, $msg);
389 return shift(@{$t->{_logMsg}});
393 # Returns a hash ref giving various status information about
400 return { map { $_ => $t->{$_} }
401 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
402 xferOK hostAbort hostError lastOutputLine)
410 return @{$t->{badFiles}};