Tar.pm (BackupPC-4.3.2) | : | Tar.pm (BackupPC-4.4.0) | ||
---|---|---|---|---|
skipping to change at line 31 | skipping to change at line 31 | |||
# This program is distributed in the hope that it will be useful, | # This program is distributed in the hope that it will be useful, | |||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
# GNU General Public License for more details. | # GNU General Public License for more details. | |||
# | # | |||
# You should have received a copy of the GNU General Public License | # You should have received a copy of the GNU General Public License | |||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
# | # | |||
#======================================================================== | #======================================================================== | |||
# | # | |||
# Version 4.3.2, released 17 Feb 2020. | # Version 4.4.0, released 20 Jun 2020. | |||
# | # | |||
# See http://backuppc.sourceforge.net. | # See http://backuppc.sourceforge.net. | |||
# | # | |||
#======================================================================== | #======================================================================== | |||
package BackupPC::Xfer::Tar; | package BackupPC::Xfer::Tar; | |||
use strict; | use strict; | |||
use Encode qw/from_to encode/; | use Encode qw/from_to encode/; | |||
use base qw(BackupPC::Xfer::Protocol); | use base qw(BackupPC::Xfer::Protocol); | |||
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); | use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); | |||
use Errno qw(EWOULDBLOCK); | use Errno qw(EWOULDBLOCK); | |||
sub useTar | sub useTar | |||
{ | { | |||
return 1; | return 1; | |||
} | } | |||
sub start | sub start | |||
{ | { | |||
my($t) = @_; | my($t) = @_; | |||
my $bpc = $t->{bpc}; | my $bpc = $t->{bpc}; | |||
my $conf = $t->{conf}; | my $conf = $t->{conf}; | |||
my(@fileList, $tarClientCmd, $logMsg, $incrDate); | my(@fileList, $tarClientCmd, $logMsg, $incrDate); | |||
local(*TAR); | local(*TAR); | |||
my $shareNamePath = $t->shareName2Path($t->{shareName}); | my $shareNamePath = $t->shareName2Path($t->{shareName}); | |||
if ( $t->{type} eq "restore" ) { | if ( $t->{type} eq "restore" ) { | |||
$tarClientCmd = $conf->{TarClientRestoreCmd}; | $tarClientCmd = $conf->{TarClientRestoreCmd}; | |||
$logMsg = "restore started below directory $t->{shareName}"; | $logMsg = "restore started below directory $t->{shareName}"; | |||
# | # | |||
# restores are considered to work unless we see they fail | # restores are considered to work unless we see they fail | |||
# (opposite to backups...) | # (opposite to backups...) | |||
# | # | |||
$t->{xferOK} = 1; | $t->{xferOK} = 1; | |||
} else { | } else { | |||
# | # | |||
# Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude} | # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude} | |||
# into a hash of arrays of files, and $conf->{TarShareName} | # into a hash of arrays of files, and $conf->{TarShareName} | |||
# to an array | # to an array | |||
# | # | |||
$bpc->backupFileConfFix($conf, "TarShareName"); | $bpc->backupFileConfFix($conf, "TarShareName"); | |||
if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) { | if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) { | |||
foreach my $file2 ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} ) { | foreach my $file2 ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} ) { | |||
my $file = $file2; | my $file = $file2; | |||
$file = "./$2" if ( $file =~ m{^(\./+|/+)(.*)}s ); | $file = "./$2" if ( $file =~ m{^(\./+|/+)(.*)}s ); | |||
$file = encode($conf->{ClientCharset}, $file) | $file = encode($conf->{ClientCharset}, $file) | |||
if ( $conf->{ClientCharset} ne "" ); | if ( $conf->{ClientCharset} ne "" ); | |||
push(@fileList, "--exclude=$file"); | push(@fileList, "--exclude=$file"); | |||
} | } | |||
} | } | |||
if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) { | if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) { | |||
foreach my $file2 ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) { | foreach my $file2 ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) { | |||
my $file = $file2; | my $file = $file2; | |||
$file = $2 if ( $file =~ m{^(\./+|/+)(.*)}s ); | $file = $2 if ( $file =~ m{^(\./+|/+)(.*)}s ); | |||
$file = "./$file"; | $file = "./$file"; | |||
$file = encode($conf->{ClientCharset}, $file) | $file = encode($conf->{ClientCharset}, $file) | |||
if ( $conf->{ClientCharset} ne "" ); | if ( $conf->{ClientCharset} ne "" ); | |||
push(@fileList, $file); | push(@fileList, $file); | |||
} | } | |||
} else { | } else { | |||
push(@fileList, "."); | push(@fileList, "."); | |||
} | ||||
if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) { | ||||
$tarClientCmd = $conf->{TarClientCmd}; | ||||
} else { | ||||
$tarClientCmd = [split(/ +/, $conf->{TarClientCmd})]; | ||||
} | } | |||
if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) { | my $args; | |||
$tarClientCmd = $conf->{TarClientCmd}; | ||||
} else { | ||||
$tarClientCmd = [split(/ +/, $conf->{TarClientCmd})]; | ||||
} | ||||
my $args; | ||||
if ( $t->{type} eq "full" ) { | if ( $t->{type} eq "full" ) { | |||
$args = $conf->{TarFullArgs}; | $args = $conf->{TarFullArgs}; | |||
$logMsg = "full backup started for directory $t->{shareName}"; | $logMsg = "full backup started for directory $t->{shareName}"; | |||
} else { | } else { | |||
$incrDate = $bpc->timeStamp($t->{incrBaseTime} - 3600, 1); | $incrDate = $bpc->timeStamp($t->{incrBaseTime} - 3600, 1); | |||
$args = $conf->{TarIncrArgs}; | $args = $conf->{TarIncrArgs}; | |||
$logMsg = "incr backup started back to $incrDate" | $logMsg = | |||
. " (backup #$t->{incrBaseBkupNum}) for directory" | "incr backup started back to $incrDate" | |||
. " $t->{shareName}"; | . " (backup #$t->{incrBaseBkupNum}) for directory" | |||
. " $t->{shareName}"; | ||||
} | } | |||
push(@$tarClientCmd, split(/ +/, $args)); | push(@$tarClientCmd, split(/ +/, $args)); | |||
} | } | |||
$logMsg .= " (client path $shareNamePath)" if ( $t->{shareName} ne $shareNam ePath ); | $logMsg .= " (client path $shareNamePath)" if ( $t->{shareName} ne $shareNam ePath ); | |||
# | # | |||
# Merge variables into @tarClientCmd | # Merge variables into @tarClientCmd | |||
# | # | |||
my $args = { | my $args = { | |||
host => $t->{host}, | host => $t->{host}, | |||
hostIP => $t->{hostIP}, | hostIP => $t->{hostIP}, | |||
client => $t->{client}, | client => $t->{client}, | |||
incrDate => $incrDate, | incrDate => $incrDate, | |||
shareNameOrig => $t->{shareName}, | shareNameOrig => $t->{shareName}, | |||
shareName => $shareNamePath, | shareName => $shareNamePath, | |||
fileList => \@fileList, | fileList => \@fileList, | |||
tarPath => $conf->{TarClientPath}, | tarPath => $conf->{TarClientPath}, | |||
sshPath => $conf->{SshPath}, | sshPath => $conf->{SshPath}, | |||
}; | }; | |||
from_to($args->{shareName}, "utf8", $conf->{ClientCharset}) | from_to($args->{shareName}, "utf8", $conf->{ClientCharset}) | |||
if ( $conf->{ClientCharset} ne "" ); | if ( $conf->{ClientCharset} ne "" ); | |||
$tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args); | $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args); | |||
if ( !defined($t->{xferPid} = open(TAR, "-|")) ) { | if ( !defined($t->{xferPid} = open(TAR, "-|")) ) { | |||
$t->{_errStr} = "Can't fork to run tar"; | $t->{_errStr} = "Can't fork to run tar"; | |||
return; | return; | |||
} | } | |||
$t->{pipeTar} = *TAR; | $t->{pipeTar} = *TAR; | |||
if ( !$t->{xferPid} ) { | if ( !$t->{xferPid} ) { | |||
# | # | |||
# This is the tar child. | # This is the tar child. | |||
# | # | |||
setpgrp 0,0; | setpgrp 0, 0; | |||
if ( $t->{type} eq "restore" ) { | if ( $t->{type} eq "restore" ) { | |||
# | # | |||
# For restores, close the write end of the pipe, | # For restores, close the write end of the pipe, | |||
# clone STDIN to RH | # clone STDIN to RH | |||
# | # | |||
close($t->{pipeWH}); | close($t->{pipeWH}); | |||
close(STDERR); | close(STDERR); | |||
open(STDERR, ">&STDOUT"); | open(STDERR, ">&STDOUT"); | |||
close(STDIN); | close(STDIN); | |||
open(STDIN, "<&$t->{pipeRH}"); | open(STDIN, "<&$t->{pipeRH}"); | |||
skipping to change at line 165 | skipping to change at line 166 | |||
# clone STDOUT to WH, and STDERR to STDOUT | # clone STDOUT to WH, and STDERR to STDOUT | |||
# | # | |||
close($t->{pipeRH}); | close($t->{pipeRH}); | |||
close(STDERR); | close(STDERR); | |||
open(STDERR, ">&STDOUT"); | open(STDERR, ">&STDOUT"); | |||
open(STDOUT, ">&$t->{pipeWH}"); | open(STDOUT, ">&$t->{pipeWH}"); | |||
} | } | |||
# | # | |||
# Run the tar command | # Run the tar command | |||
# | # | |||
alarm(0); | alarm(0); | |||
$bpc->cmdExecOrEval($tarClientCmd, $args); | $bpc->cmdExecOrEval($tarClientCmd, $args); | |||
# should not be reached, but just in case... | # should not be reached, but just in case... | |||
$t->{_errStr} = "Can't exec @$tarClientCmd"; | $t->{_errStr} = "Can't exec @$tarClientCmd"; | |||
return; | return; | |||
} | } | |||
my $str = $bpc->execCmd2ShellCmd(@$tarClientCmd); | my $str = $bpc->execCmd2ShellCmd(@$tarClientCmd); | |||
from_to($str, $conf->{ClientCharset}, "utf8") | from_to($str, $conf->{ClientCharset}, "utf8") | |||
if ( $conf->{ClientCharset} ne "" ); | if ( $conf->{ClientCharset} ne "" ); | |||
$t->{XferLOG}->write(\"Running: $str\n"); | $t->{XferLOG}->write(\"Running: $str\n"); | |||
alarm($conf->{ClientTimeout}); | alarm($conf->{ClientTimeout}); | |||
$t->{_errStr} = undef; | $t->{_errStr} = undef; | |||
# | # | |||
# make pipeTar non-blocking; BackupPC_dump uses select() to see if there | # make pipeTar non-blocking; BackupPC_dump uses select() to see if there | |||
# is something to read. | # is something to read. | |||
# | # | |||
if ( !fcntl($t->{pipeTar}, F_SETFL, fcntl($t->{pipeTar}, F_GETFL, 0) | O_NON BLOCK) ) { | if ( !fcntl($t->{pipeTar}, F_SETFL, fcntl($t->{pipeTar}, F_GETFL, 0) | O_NON BLOCK) ) { | |||
$t->{_errStr} = "can't set pipeTar to non-blocking"; | $t->{_errStr} = "can't set pipeTar to non-blocking"; | |||
} | } | |||
skipping to change at line 195 | skipping to change at line 197 | |||
} | } | |||
sub readOutput | sub readOutput | |||
{ | { | |||
my($t, $FDreadRef, $rout) = @_; | my($t, $FDreadRef, $rout) = @_; | |||
my $conf = $t->{conf}; | my $conf = $t->{conf}; | |||
if ( vec($rout, fileno($t->{pipeTar}), 1) ) { | if ( vec($rout, fileno($t->{pipeTar}), 1) ) { | |||
my $mesg; | my $mesg; | |||
$! = 0; | $! = 0; | |||
if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) { | if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) { | |||
if ( $! == EWOULDBLOCK ) { | if ( $! == EWOULDBLOCK ) { | |||
$t->{XferLOG}->write(\"readOutput: no bytes read (EWOULDBLOCK); continuing\n"); | $t->{XferLOG}->write(\"readOutput: no bytes read (EWOULDBLOCK); continuing\n"); | |||
} elsif ( eof($t->{pipeTar}) ) { | } elsif ( eof($t->{pipeTar}) ) { | |||
$t->{XferLOG}->write(\"readOutput: sysread returns 0 and got EOF \n"); | $t->{XferLOG}->write(\"readOutput: sysread returns 0 and got EOF \n"); | |||
vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0; | vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0; | |||
if ( !close($t->{pipeTar}) && $? != 256 ) { | if ( !close($t->{pipeTar}) && $? != 256 ) { | |||
# | # | |||
# Tar 1.16 uses exit status 1 (256) when some files | # Tar 1.16 uses exit status 1 (256) when some files | |||
# changed during archive creation. We allow this | # changed during archive creation. We allow this | |||
skipping to change at line 221 | skipping to change at line 223 | |||
} | } | |||
} else { | } else { | |||
$t->{tarOut} .= $mesg; | $t->{tarOut} .= $mesg; | |||
} | } | |||
} | } | |||
my $logFileThres = $t->{type} eq "restore" ? 1 : 2; | my $logFileThres = $t->{type} eq "restore" ? 1 : 2; | |||
while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) { | while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) { | |||
$_ = $1; | $_ = $1; | |||
$t->{tarOut} = $2; | $t->{tarOut} = $2; | |||
from_to($_, $conf->{ClientCharset}, "utf8") | from_to($_, $conf->{ClientCharset}, "utf8") | |||
if ( $conf->{ClientCharset} ne "" ); | if ( $conf->{ClientCharset} ne "" ); | |||
# | # | |||
# refresh our inactivity alarm | # refresh our inactivity alarm | |||
# | # | |||
alarm($conf->{ClientTimeout}) if ( !$t->{abort} ); | alarm($conf->{ClientTimeout}) if ( !$t->{abort} ); | |||
$t->{lastOutputLine} = $_ if ( !/^$/ ); | $t->{lastOutputLine} = $_ if ( !/^$/ ); | |||
if ( /^Total bytes (written|read): / ) { | if ( /^Total bytes (written|read): / ) { | |||
$t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 ); | $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 ); | |||
$t->{xferOK} = 1; | $t->{xferOK} = 1; | |||
} elsif ( /^(a )?\./ ) { | } elsif ( /^(a )?\./ ) { | |||
$t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= $logFileThres ) ; | $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= $logFileThres ) ; | |||
$t->{fileCnt}++; | $t->{fileCnt}++; | |||
} else { | } else { | |||
# | # | |||
# Ignore annoying log message on incremental for tar 1.15.x | # Ignore annoying log message on incremental for tar 1.15.x | |||
# | # | |||
if ( !/: file is unchanged; not dumped$/ && !/: socket ignored$/ ) { | if ( !/: file is unchanged; not dumped$/ && !/: socket ignored$/ ) { | |||
$t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 ); | $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 ); | |||
$t->{xferErrCnt}++; | $t->{xferErrCnt}++; | |||
} | } | |||
# | # | |||
# If tar encounters a minor error, it will exit with a non-zero | # If tar encounters a minor error, it will exit with a non-zero | |||
# status. We still consider that ok. Remember if tar prints | # status. We still consider that ok. Remember if tar prints | |||
# this message indicating a non-fatal error. | # this message indicating a non-fatal error. | |||
# | # | |||
$t->{tarBadExitOk} = 1 | $t->{tarBadExitOk} = 1 | |||
if ( $t->{xferOK} && /Error exit delayed from previous / ); | if ( $t->{xferOK} && /Error exit delayed from previous / ); | |||
# | # | |||
# Also remember files that had read errors | # Also remember files that had read errors | |||
# | # | |||
if ( /: \.\/(.*): Read error at byte / ) { | if ( /: \.\/(.*): Read error at byte / ) { | |||
my $badFile = $1; | my $badFile = $1; | |||
push(@{$t->{badFiles}}, { | push( | |||
@{$t->{badFiles}}, | ||||
{ | ||||
share => $t->{shareName}, | share => $t->{shareName}, | |||
file => $badFile | file => $badFile | |||
}); | } | |||
); | ||||
} | } | |||
} | } | |||
} | } | |||
return 1; | return 1; | |||
} | } | |||
sub setSelectMask | sub setSelectMask | |||
{ | { | |||
my($t, $FDreadRef) = @_; | my($t, $FDreadRef) = @_; | |||
vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1; | vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1; | |||
} | } | |||
End of changes. 23 change blocks. | ||||
50 lines changed or deleted | 55 lines changed or added |