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.0beta3, released 3 Dec 2006.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::Xfer::Tar;
41 use Encode qw/from_to encode/;
45 my($class, $bpc, $args) = @_;
50 conf => { $bpc->Conf },
67 foreach my $arg ( keys(%$args) ) {
68 $t->{$arg} = $args->{$arg};
81 my $conf = $t->{conf};
82 my(@fileList, $tarClientCmd, $logMsg, $incrDate);
85 if ( $t->{type} eq "restore" ) {
86 $tarClientCmd = $conf->{TarClientRestoreCmd};
87 $logMsg = "restore started below directory $t->{shareName}";
89 # restores are considered to work unless we see they fail
90 # (opposite to backups...)
95 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
96 # into a hash of arrays of files, and $conf->{TarShareName}
99 $bpc->backupFileConfFix($conf, "TarShareName");
101 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
102 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
104 $file = $2 if ( $file =~ m{^(\./+|/+)(.*)}s );
106 $file = encode($conf->{ClientCharset}, $file)
107 if ( $conf->{ClientCharset} ne "" );
108 push(@fileList, "--exclude=$file");
111 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
112 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
113 $file = $2 if ( $file =~ m{^(\./+|/+)(.*)}s );
115 $file = encode($conf->{ClientCharset}, $file)
116 if ( $conf->{ClientCharset} ne "" );
117 push(@fileList, $file);
120 push(@fileList, ".");
122 if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) {
123 $tarClientCmd = $conf->{TarClientCmd};
125 $tarClientCmd = [split(/ +/, $conf->{TarClientCmd})];
128 if ( $t->{type} eq "full" ) {
129 $args = $conf->{TarFullArgs};
130 $logMsg = "full backup started for directory $t->{shareName}";
132 $incrDate = $bpc->timeStamp($t->{incrBaseTime} - 3600, 1);
133 $args = $conf->{TarIncrArgs};
134 $logMsg = "incr backup started back to $incrDate"
135 . " (backup #$t->{incrBaseBkupNum}) for directory"
136 . " $t->{shareName}";
138 push(@$tarClientCmd, split(/ +/, $args));
141 # Merge variables into @tarClientCmd
145 hostIP => $t->{hostIP},
146 client => $t->{client},
147 incrDate => $incrDate,
148 shareName => $t->{shareName},
149 fileList => \@fileList,
150 tarPath => $conf->{TarClientPath},
151 sshPath => $conf->{SshPath},
153 from_to($args->{shareName}, "utf8", $conf->{ClientCharset})
154 if ( $conf->{ClientCharset} ne "" );
155 $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args);
156 if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
157 $t->{_errStr} = "Can't fork to run tar";
160 $t->{pipeTar} = *TAR;
161 if ( !$t->{xferPid} ) {
163 # This is the tar child.
166 if ( $t->{type} eq "restore" ) {
168 # For restores, close the write end of the pipe,
173 open(STDERR, ">&STDOUT");
175 open(STDIN, "<&$t->{pipeRH}");
178 # For backups, close the read end of the pipe,
179 # clone STDOUT to WH, and STDERR to STDOUT
183 open(STDERR, ">&STDOUT");
184 open(STDOUT, ">&$t->{pipeWH}");
187 # Run the tar command
190 $bpc->cmdExecOrEval($tarClientCmd, $args);
191 # should not be reached, but just in case...
192 $t->{_errStr} = "Can't exec @$tarClientCmd";
195 my $str = "Running: " . $bpc->execCmd2ShellCmd(@$tarClientCmd) . "\n";
196 from_to($str, $conf->{ClientCharset}, "utf8")
197 if ( $conf->{ClientCharset} ne "" );
198 $t->{XferLOG}->write(\"Running: @$tarClientCmd\n");
199 alarm($conf->{ClientTimeout});
200 $t->{_errStr} = undef;
206 my($t, $FDreadRef, $rout) = @_;
207 my $conf = $t->{conf};
209 if ( vec($rout, fileno($t->{pipeTar}), 1) ) {
211 if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) {
212 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0;
213 if ( !close($t->{pipeTar}) && $? != 256 ) {
215 # Tar 1.16 uses exit status 1 (256) when some files
216 # changed during archive creation. We allow this
217 # as a benign error and consider the archive ok
219 $t->{tarOut} .= "Tar exited with error $? ($!) status\n";
220 $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} );
223 $t->{tarOut} .= $mesg;
226 my $logFileThres = $t->{type} eq "restore" ? 1 : 2;
227 while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) {
230 from_to($_, $conf->{ClientCharset}, "utf8")
231 if ( $conf->{ClientCharset} ne "" );
233 # refresh our inactivity alarm
235 alarm($conf->{ClientTimeout}) if ( !$t->{abort} );
236 $t->{lastOutputLine} = $_ if ( !/^$/ );
237 if ( /^Total bytes (written|read): / ) {
238 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
241 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= $logFileThres );
245 # Ignore annoying log message on incremental for tar 1.15.x
247 if ( !/: file is unchanged; not dumped$/ ) {
248 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
252 # If tar encounters a minor error, it will exit with a non-zero
253 # status. We still consider that ok. Remember if tar prints
254 # this message indicating a non-fatal error.
256 $t->{tarBadExitOk} = 1
257 if ( $t->{xferOK} && /Error exit delayed from previous / );
259 # Also remember files that had read errors
261 if ( /: \.\/(.*): Read error at byte / ) {
263 push(@{$t->{badFiles}}, {
264 share => $t->{shareName},
276 my($t, $reason) = @_;
277 my @xferPid = $t->xferPid;
280 $t->{abortReason} = $reason;
282 kill($t->{bpc}->sigName2num("INT"), @xferPid);
288 my($t, $FDreadRef) = @_;
290 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1;
297 return $t->{_errStr};
304 return ($t->{xferPid});
311 push(@{$t->{_logMsg}}, $msg);
318 return shift(@{$t->{_logMsg}});
322 # Returns a hash ref giving various status information about
329 return { map { $_ => $t->{$_} }
330 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
331 xferOK hostAbort hostError lastOutputLine)
339 return @{$t->{badFiles}};