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 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 1.6.0_CVS, released 10 Dec 2002.
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 push(@tarClientCmd, split(/ +/, $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
97 $conf->{TarShareName} = [ $conf->{TarShareName} ]
98 unless ref($conf->{TarShareName}) eq "ARRAY";
99 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
100 next if ( !defined($conf->{$param}) );
101 if ( ref($conf->{$param}) eq "ARRAY" ) {
103 $conf->{TarShareName}[0] => $conf->{$param}
105 } elsif ( ref($conf->{$param}) eq "HASH" ) {
109 $conf->{TarShareName}[0] => [ $conf->{$param} ]
113 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
114 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
116 $file = ".$file" if ( $file =~ /^\// );
117 push(@fileList, "--exclude=$file");
120 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
121 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
122 $file = ".$file" if ( $file =~ /^\// );
123 push(@fileList, $file);
126 push(@fileList, ".");
128 if ( $t->{type} eq "full" ) {
130 split(/ +/, $conf->{TarClientCmd}),
131 split(/ +/, $conf->{TarFullArgs})
133 $logMsg = "full backup started for directory $t->{shareName}";
135 $incrDate = $bpc->timeStampISO($t->{lastFull} - 3600, 1);
137 split(/ +/, $conf->{TarClientCmd}),
138 split(/ +/, $conf->{TarIncrArgs})
140 $logMsg = "incr backup started back to $incrDate for directory"
141 . " $t->{shareName}";
145 # Merge variables into @tarClientCmd
149 hostIP => $t->{hostIP},
150 incrDate => $incrDate,
151 shareName => $t->{shareName},
152 tarPath => $conf->{TarClientPath},
153 sshPath => $conf->{SshPath},
155 my @cmd = @tarClientCmd;
157 foreach my $arg ( @cmd ) {
158 next if ( $arg =~ /^\s*$/ );
159 if ( $arg =~ /^\$fileList(\+?)/ ) {
161 foreach $arg ( @fileList ) {
162 $arg = $bpc->shellEscape($arg) if ( $esc );
163 push(@tarClientCmd, $arg);
166 $arg =~ s{\$(\w+)(\+?)}{
168 ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
171 push(@tarClientCmd, $arg);
174 if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
175 $t->{_errStr} = "Can't fork to run tar";
178 $t->{pipeTar} = *TAR;
179 if ( !$t->{xferPid} ) {
181 # This is the tar child.
184 if ( $t->{type} eq "restore" ) {
186 # For restores, close the write end of the pipe,
191 open(STDERR, ">&STDOUT");
193 open(STDIN, "<&$t->{pipeRH}");
196 # For backups, close the read end of the pipe,
197 # clone STDOUT to WH, and STDERR to STDOUT
201 open(STDERR, ">&STDOUT");
202 open(STDOUT, ">&$t->{pipeWH}");
205 # Run the tar command
208 # should not be reached, but just in case...
209 $t->{_errStr} = "Can't exec @tarClientCmd";
212 $t->{XferLOG}->write(\"Running: @tarClientCmd\n");
213 alarm($conf->{SmbClientTimeout});
214 $t->{_errStr} = undef;
220 my($t, $FDreadRef, $rout) = @_;
221 my $conf = $t->{conf};
223 if ( vec($rout, fileno($t->{pipeTar}), 1) ) {
225 if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) {
226 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0;
227 if ( !close($t->{pipeTar}) ) {
228 $t->{tarOut} .= "Tar exited with error $? ($!) status\n";
229 $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} );
232 $t->{tarOut} .= $mesg;
235 while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) {
238 $t->{XferLOG}->write(\"$_\n");
240 # refresh our inactivity alarm
242 alarm($conf->{SmbClientTimeout});
243 $t->{lastOutputLine} = $_ if ( !/^$/ );
244 if ( /^Total bytes written: / ) {
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 / );
264 my($t, $FDreadRef) = @_;
266 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1;
273 return $t->{_errStr};
280 return $t->{xferPid};
287 push(@{$t->{_logMsg}}, $msg);
294 return shift(@{$t->{_logMsg}});
298 # Returns a hash ref giving various status information about
305 return { map { $_ => $t->{$_} }
306 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
307 xferOK hostAbort hostError lastOutputLine)
315 return @{$t->{badFiles}};