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 2.0.0beta2, released 13 Apr 2003.
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 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
125 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
126 push(@fileList, $file);
128 } elsif ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
129 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
131 push(@fileList, $file);
134 # Allow simple wildcards in exclude list by specifying "r" option.
138 if ( $t->{type} eq "full" ) {
139 $smbClientCmd = $conf->{SmbClientFullCmd};
140 $logMsg = "full backup started for share $t->{shareName}";
142 $timeStampFile = "$t->{outDir}/timeStamp.level0";
143 open(LEV0, ">", $timeStampFile) && close(LEV0);
144 utime($t->{lastFull} - 3600, $t->{lastFull} - 3600, $timeStampFile);
145 $smbClientCmd = $conf->{SmbClientIncrCmd};
146 $logMsg = "incr backup started back to "
147 . $bpc->timeStamp($t->{lastFull} - 3600, 0)
148 . "for share $t->{shareName}";
152 smbClientPath => $conf->{SmbClientPath},
154 hostIP => $t->{hostIP},
155 client => $t->{client},
156 shareName => $t->{shareName},
157 userName => $conf->{SmbShareUserName},
158 fileList => \@fileList,
159 I_option => $I_option,
160 X_option => $X_option,
161 timeStampFile => $timeStampFile,
163 $smbClientCmd = $bpc->cmdVarSubstitute($smbClientCmd, $args);
165 if ( !defined($t->{xferPid} = open(SMB, "-|")) ) {
166 $t->{_errStr} = "Can't fork to run smbclient";
169 $t->{pipeSMB} = *SMB;
170 if ( !$t->{xferPid} ) {
172 # This is the smbclient child.
175 if ( $t->{type} eq "restore" ) {
177 # For restores close the write end of the pipe,
178 # clone STDIN from RH, and STDERR to STDOUT
182 open(STDERR, ">&STDOUT");
184 open(STDIN, "<&$t->{pipeRH}");
187 # For backups close the read end of the pipe,
188 # clone STDOUT to WH, STDERR to STDOUT
192 open(STDERR, ">&STDOUT");
193 open(STDOUT, ">&$t->{pipeWH}");
198 $bpc->cmdExecOrEval($smbClientCmd, $args);
199 # should not be reached, but just in case...
200 $t->{_errStr} = "Can't exec $conf->{SmbClientPath}";
203 my $str = "Running: " . $bpc->execCmd2ShellCmd(@$smbClientCmd) . "\n";
204 $t->{XferLOG}->write(\$str);
205 alarm($conf->{ClientTimeout});
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->{ClientTimeout});
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}};