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-2007 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.1.0, released 25 Nov 2007.
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 );
105 $file = encode($conf->{ClientCharset}, $file)
106 if ( $conf->{ClientCharset} ne "" );
107 push(@fileList, "--exclude=$file");
110 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
111 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
112 $file = $2 if ( $file =~ m{^(\./+|/+)(.*)}s );
114 $file = encode($conf->{ClientCharset}, $file)
115 if ( $conf->{ClientCharset} ne "" );
116 push(@fileList, $file);
119 push(@fileList, ".");
121 if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) {
122 $tarClientCmd = $conf->{TarClientCmd};
124 $tarClientCmd = [split(/ +/, $conf->{TarClientCmd})];
127 if ( $t->{type} eq "full" ) {
128 $args = $conf->{TarFullArgs};
129 $logMsg = "full backup started for directory $t->{shareName}";
131 $incrDate = $bpc->timeStamp($t->{incrBaseTime} - 3600, 1);
132 $args = $conf->{TarIncrArgs};
133 $logMsg = "incr backup started back to $incrDate"
134 . " (backup #$t->{incrBaseBkupNum}) for directory"
135 . " $t->{shareName}";
137 push(@$tarClientCmd, split(/ +/, $args));
140 # Merge variables into @tarClientCmd
144 hostIP => $t->{hostIP},
145 client => $t->{client},
146 incrDate => $incrDate,
147 shareName => $t->{shareName},
148 fileList => \@fileList,
149 tarPath => $conf->{TarClientPath},
150 sshPath => $conf->{SshPath},
152 from_to($args->{shareName}, "utf8", $conf->{ClientCharset})
153 if ( $conf->{ClientCharset} ne "" );
154 $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args);
155 if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
156 $t->{_errStr} = "Can't fork to run tar";
159 $t->{pipeTar} = *TAR;
160 if ( !$t->{xferPid} ) {
162 # This is the tar child.
165 if ( $t->{type} eq "restore" ) {
167 # For restores, close the write end of the pipe,
172 open(STDERR, ">&STDOUT");
174 open(STDIN, "<&$t->{pipeRH}");
177 # For backups, close the read end of the pipe,
178 # clone STDOUT to WH, and STDERR to STDOUT
182 open(STDERR, ">&STDOUT");
183 open(STDOUT, ">&$t->{pipeWH}");
186 # Run the tar command
189 $bpc->cmdExecOrEval($tarClientCmd, $args);
190 # should not be reached, but just in case...
191 $t->{_errStr} = "Can't exec @$tarClientCmd";
194 my $str = "Running: " . $bpc->execCmd2ShellCmd(@$tarClientCmd) . "\n";
195 from_to($str, $conf->{ClientCharset}, "utf8")
196 if ( $conf->{ClientCharset} ne "" );
197 $t->{XferLOG}->write(\"Running: @$tarClientCmd\n");
198 alarm($conf->{ClientTimeout});
199 $t->{_errStr} = undef;
205 my($t, $FDreadRef, $rout) = @_;
206 my $conf = $t->{conf};
208 if ( vec($rout, fileno($t->{pipeTar}), 1) ) {
210 if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) {
211 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0;
212 if ( !close($t->{pipeTar}) && $? != 256 ) {
214 # Tar 1.16 uses exit status 1 (256) when some files
215 # changed during archive creation. We allow this
216 # as a benign error and consider the archive ok
218 $t->{tarOut} .= "Tar exited with error $? ($!) status\n";
219 $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} );
222 $t->{tarOut} .= $mesg;
225 my $logFileThres = $t->{type} eq "restore" ? 1 : 2;
226 while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) {
229 from_to($_, $conf->{ClientCharset}, "utf8")
230 if ( $conf->{ClientCharset} ne "" );
232 # refresh our inactivity alarm
234 alarm($conf->{ClientTimeout}) if ( !$t->{abort} );
235 $t->{lastOutputLine} = $_ if ( !/^$/ );
236 if ( /^Total bytes (written|read): / ) {
237 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
240 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= $logFileThres );
244 # Ignore annoying log message on incremental for tar 1.15.x
246 if ( !/: file is unchanged; not dumped$/ && !/: socket ignored$/ ) {
247 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
251 # If tar encounters a minor error, it will exit with a non-zero
252 # status. We still consider that ok. Remember if tar prints
253 # this message indicating a non-fatal error.
255 $t->{tarBadExitOk} = 1
256 if ( $t->{xferOK} && /Error exit delayed from previous / );
258 # Also remember files that had read errors
260 if ( /: \.\/(.*): Read error at byte / ) {
262 push(@{$t->{badFiles}}, {
263 share => $t->{shareName},
275 my($t, $reason) = @_;
276 my @xferPid = $t->xferPid;
279 $t->{abortReason} = $reason;
281 kill($t->{bpc}->sigName2num("INT"), @xferPid);
287 my($t, $FDreadRef) = @_;
289 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1;
296 return $t->{_errStr};
303 return ($t->{xferPid});
310 push(@{$t->{_logMsg}}, $msg);
317 return shift(@{$t->{_logMsg}});
321 # Returns a hash ref giving various status information about
328 return { map { $_ => $t->{$_} }
329 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
330 xferOK hostAbort hostError lastOutputLine)
338 return @{$t->{badFiles}};