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.5.0, released 2 Aug 2002.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::Xfer::Tar;
44 my($class, $bpc, $args) = @_;
48 conf => { $bpc->Conf },
65 my $conf = $t->{conf};
66 my(@fileList, @tarClientCmd, $logMsg, $incrDate);
69 if ( $t->{type} eq "restore" ) {
70 push(@tarClientCmd, split(/ +/, $conf->{TarClientRestoreCmd}));
71 $logMsg = "restore started below directory $t->{shareName}";
73 # restores are considered to work unless we see they fail
74 # (opposite to backups...)
79 # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
80 # into a hash of arrays of files
82 $conf->{TarShareName} = [ $conf->{TarShareName} ]
83 unless ref($conf->{TarShareName}) eq "ARRAY";
84 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
85 next if ( !defined($conf->{$param}) );
86 if ( ref($conf->{$param}) eq "ARRAY" ) {
88 $conf->{TarShareName}[0] => $conf->{$param}
90 } elsif ( ref($conf->{$param}) eq "HASH" ) {
94 $conf->{TarShareName}[0] => [ $conf->{$param} ]
98 if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
99 foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
101 $file = ".$file" if ( $file =~ /^\// );
102 push(@fileList, "--exclude=$file");
105 if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
106 foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
107 $file = ".$file" if ( $file =~ /^\// );
108 push(@fileList, $file);
111 push(@fileList, ".");
113 if ( $t->{type} eq "full" ) {
115 split(/ +/, $conf->{TarClientCmd}),
116 split(/ +/, $conf->{TarFullArgs})
118 $logMsg = "full backup started for directory $t->{shareName}";
120 $incrDate = $bpc->timeStampISO($t->{lastFull} - 3600, 1);
122 split(/ +/, $conf->{TarClientCmd}),
123 split(/ +/, $conf->{TarIncrArgs})
125 $logMsg = "incr backup started back to $incrDate for directory"
126 . " $t->{shareName}";
130 # Merge variables into @tarClientCmd
134 hostIP => $t->{hostIP},
135 incrDate => $incrDate,
136 shareName => $t->{shareName},
137 tarPath => $conf->{TarClientPath},
138 sshPath => $conf->{SshPath},
140 my @cmd = @tarClientCmd;
142 foreach my $arg ( @cmd ) {
143 next if ( $arg =~ /^\s*$/ );
144 if ( $arg =~ /^\$fileList(\+?)/ ) {
146 foreach $arg ( @fileList ) {
147 $arg = $bpc->shellEscape($arg) if ( $esc );
148 push(@tarClientCmd, $arg);
151 $arg =~ s{\$(\w+)(\+?)}{
153 ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
156 push(@tarClientCmd, $arg);
159 if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
160 $t->{_errStr} = "Can't fork to run tar";
163 $t->{pipeTar} = *TAR;
164 if ( !$t->{xferPid} ) {
166 # This is the tar child.
169 if ( $t->{type} eq "restore" ) {
171 # For restores, close the write end of the pipe,
176 open(STDERR, ">&STDOUT");
178 open(STDIN, "<&$t->{pipeRH}");
181 # For backups, close the read end of the pipe,
182 # clone STDOUT to WH, and STDERR to STDOUT
186 open(STDERR, ">&STDOUT");
187 open(STDOUT, ">&$t->{pipeWH}");
190 # Run the tar command
193 # should not be reached, but just in case...
194 $t->{_errStr} = "Can't exec @tarClientCmd";
197 $t->{XferLOG}->write(\"Running: @tarClientCmd\n");
198 alarm($conf->{SmbClientTimeout});
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}) ) {
213 $t->{tarOut} .= "Tar exited with error $? ($!) status\n";
214 $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} );
217 $t->{tarOut} .= $mesg;
220 while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) {
223 $t->{XferLOG}->write(\"$_\n");
225 # refresh our inactivity alarm
227 alarm($conf->{SmbClientTimeout});
228 $t->{lastOutputLine} = $_ if ( !/^$/ );
229 if ( /^Total bytes written: / ) {
236 # If tar encounters a minor error, it will exit with a non-zero
237 # status. We still consider that ok. Remember if tar prints
238 # this message indicating a non-fatal error.
240 $t->{tarBadExitOk} = 1
241 if ( $t->{xferOK} && /Error exit delayed from previous / );
249 my($t, $FDreadRef) = @_;
251 vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1;
258 return $t->{_errStr};
265 return $t->{xferPid};
272 push(@{$t->{_logMsg}}, $msg);
279 return shift(@{$t->{_logMsg}});
283 # Returns a hash ref giving various status information about
290 return { map { $_ => $t->{$_} }
291 qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
292 xferOK hostAbort hostError lastOutputLine)
300 return @{$t->{badFiles}};