1 #============================================================= -*-perl-*-
3 # BackupPC::Xfer::Tar package
7 # This library defines a BackupPC::Xfer::Tar class for managing
8 # the tar-based 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 3.0.0alpha, released 23 Jan 2006.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::Xfer::Tar;
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(@fileList, $tarClientCmd, $logMsg, $incrDate);
84 if ( $t->{type} eq "restore" ) {
85 $tarClientCmd = $conf->{TarClientRestoreCmd};
86 $logMsg = "restore started below directory $t->{shareName}";
88 # restores are considered to work unless we see they fail
89 # (opposite to backups...)
94 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
95 # into a hash of arrays of files, and $conf->{TarShareName}
98 $bpc->backupFileConfFix($conf, "TarShareName");
100 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
101 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
103 $file = ".$file" if ( $file =~ /^\// );
104 push(@fileList, "--exclude=$file");
107 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
108 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
109 $file = ".$file" if ( $file =~ /^\// );
110 push(@fileList, $file);
113 push(@fileList, ".");
115 if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) {
116 $tarClientCmd = $conf->{TarClientCmd};
118 $tarClientCmd = [split(/ +/, $conf->{TarClientCmd})];
121 if ( $t->{type} eq "full" ) {
122 $args = $conf->{TarFullArgs};
123 $logMsg = "full backup started for directory $t->{shareName}";
125 $incrDate = $bpc->timeStamp($t->{lastFull} - 3600, 1);
126 $args = $conf->{TarIncrArgs};
127 $logMsg = "incr backup started back to $incrDate for directory"
128 . " $t->{shareName}";
130 push(@$tarClientCmd, split(/ +/, $args));
133 # Merge variables into @tarClientCmd
137 hostIP => $t->{hostIP},
138 client => $t->{client},
139 incrDate => $incrDate,
140 shareName => $t->{shareName},
141 fileList => \@fileList,
142 tarPath => $conf->{TarClientPath},
143 sshPath => $conf->{SshPath},
145 $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args);
146 if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
147 $t->{_errStr} = "Can't fork to run tar";
150 $t->{pipeTar} = *TAR;
151 if ( !$t->{xferPid} ) {
153 # This is the tar child.
156 if ( $t->{type} eq "restore" ) {
158 # For restores, close the write end of the pipe,
163 open(STDERR, ">&STDOUT");
165 open(STDIN, "<&$t->{pipeRH}");
168 # For backups, close the read end of the pipe,
169 # clone STDOUT to WH, and STDERR to STDOUT
173 open(STDERR, ">&STDOUT");
174 open(STDOUT, ">&$t->{pipeWH}");
177 # Run the tar command
180 $bpc->cmdExecOrEval($tarClientCmd, $args);
181 # should not be reached, but just in case...
182 $t->{_errStr} = "Can't exec @$tarClientCmd";
185 my $str = "Running: " . $bpc->execCmd2ShellCmd(@$tarClientCmd) . "\n";
186 $t->{XferLOG}->write(\"Running: @$tarClientCmd\n");
187 alarm($conf->{ClientTimeout});
188 $t->{_errStr} = undef;
194 my($t, $FDreadRef, $rout) = @_;
195 my $conf = $t->{conf};
197 if ( vec($rout, fileno($t->{pipeTar}), 1) ) {
199 if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) {
200 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0;
201 if ( !close($t->{pipeTar}) ) {
202 $t->{tarOut} .= "Tar exited with error $? ($!) status\n";
203 $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} );
206 $t->{tarOut} .= $mesg;
209 while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) {
213 # refresh our inactivity alarm
215 alarm($conf->{ClientTimeout}) if ( !$t->{abort} );
216 $t->{lastOutputLine} = $_ if ( !/^$/ );
217 if ( /^Total bytes written: / ) {
218 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
221 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 2 );
225 # Ignore annoying log message on incremental for tar 1.15.x
227 if ( !/: file is unchanged; not dumped$/ ) {
228 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
232 # If tar encounters a minor error, it will exit with a non-zero
233 # status. We still consider that ok. Remember if tar prints
234 # this message indicating a non-fatal error.
236 $t->{tarBadExitOk} = 1
237 if ( $t->{xferOK} && /Error exit delayed from previous / );
239 # Also remember files that had read errors
241 if ( /: \.\/(.*): Read error at byte / ) {
243 push(@{$t->{badFiles}}, {
244 share => $t->{shareName},
256 my($t, $reason) = @_;
257 my @xferPid = $t->xferPid;
260 $t->{abortReason} = $reason;
262 kill($t->{bpc}->sigName2num("INT"), @xferPid);
268 my($t, $FDreadRef) = @_;
270 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1;
277 return $t->{_errStr};
284 return ($t->{xferPid});
291 push(@{$t->{_logMsg}}, $msg);
298 return shift(@{$t->{_logMsg}});
302 # Returns a hash ref giving various status information about
309 return { map { $_ => $t->{$_} }
310 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
311 xferOK hostAbort hostError lastOutputLine)
319 return @{$t->{badFiles}};