X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=lib%2FBackupPC%2FXfer%2FTar.pm;h=f6f0be1deb3f07d90284213c75b0c5971cda7812;hp=7092b89b57662c2717d7a9c7acc0dd095f165d0a;hb=fda25dc88a63ccac1c80efa2e4994bf0725ca9b7;hpb=e9453b7611be63303572ae443d5fb56b73364678 diff --git a/lib/BackupPC/Xfer/Tar.pm b/lib/BackupPC/Xfer/Tar.pm index 7092b89..f6f0be1 100644 --- a/lib/BackupPC/Xfer/Tar.pm +++ b/lib/BackupPC/Xfer/Tar.pm @@ -11,7 +11,7 @@ # Craig Barratt # # COPYRIGHT -# Copyright (C) 2001 Craig Barratt +# Copyright (C) 2001-2007 Craig Barratt # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -29,7 +29,7 @@ # #======================================================================== # -# Version 1.6.0_CVS, released 10 Dec 2002. +# Version 3.1.0, released 25 Nov 2007. # # See http://backuppc.sourceforge.net. # @@ -38,6 +38,7 @@ package BackupPC::Xfer::Tar; use strict; +use Encode qw/from_to encode/; sub new { @@ -78,11 +79,11 @@ sub start my($t) = @_; my $bpc = $t->{bpc}; my $conf = $t->{conf}; - my(@fileList, @tarClientCmd, $logMsg, $incrDate); + my(@fileList, $tarClientCmd, $logMsg, $incrDate); local(*TAR); if ( $t->{type} eq "restore" ) { - push(@tarClientCmd, split(/ +/, $conf->{TarClientRestoreCmd})); + $tarClientCmd = $conf->{TarClientRestoreCmd}; $logMsg = "restore started below directory $t->{shareName}"; # # restores are considered to work unless we see they fail @@ -92,85 +93,65 @@ sub start } else { # # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude} - # into a hash of arrays of files + # into a hash of arrays of files, and $conf->{TarShareName} + # to an array # - $conf->{TarShareName} = [ $conf->{TarShareName} ] - unless ref($conf->{TarShareName}) eq "ARRAY"; - foreach my $param qw(BackupFilesOnly BackupFilesExclude) { - next if ( !defined($conf->{$param}) ); - if ( ref($conf->{$param}) eq "ARRAY" ) { - $conf->{$param} = { - $conf->{TarShareName}[0] => $conf->{$param} - }; - } elsif ( ref($conf->{$param}) eq "HASH" ) { - # do nothing - } else { - $conf->{$param} = { - $conf->{TarShareName}[0] => [ $conf->{$param} ] - }; - } - } + $bpc->backupFileConfFix($conf, "TarShareName"); + if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) { foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} ) { - $file = ".$file" if ( $file =~ /^\// ); + $file = "./$2" if ( $file =~ m{^(\./+|/+)(.*)}s ); + $file = encode($conf->{ClientCharset}, $file) + if ( $conf->{ClientCharset} ne "" ); push(@fileList, "--exclude=$file"); } } if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) { foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) { - $file = ".$file" if ( $file =~ /^\// ); + $file = $2 if ( $file =~ m{^(\./+|/+)(.*)}s ); + $file = "./$file"; + $file = encode($conf->{ClientCharset}, $file) + if ( $conf->{ClientCharset} ne "" ); push(@fileList, $file); } } else { push(@fileList, "."); } + if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) { + $tarClientCmd = $conf->{TarClientCmd}; + } else { + $tarClientCmd = [split(/ +/, $conf->{TarClientCmd})]; + } + my $args; if ( $t->{type} eq "full" ) { - push(@tarClientCmd, - split(/ +/, $conf->{TarClientCmd}), - split(/ +/, $conf->{TarFullArgs}) - ); + $args = $conf->{TarFullArgs}; $logMsg = "full backup started for directory $t->{shareName}"; } else { - $incrDate = $bpc->timeStampISO($t->{lastFull} - 3600, 1); - push(@tarClientCmd, - split(/ +/, $conf->{TarClientCmd}), - split(/ +/, $conf->{TarIncrArgs}) - ); - $logMsg = "incr backup started back to $incrDate for directory" + $incrDate = $bpc->timeStamp($t->{incrBaseTime} - 3600, 1); + $args = $conf->{TarIncrArgs}; + $logMsg = "incr backup started back to $incrDate" + . " (backup #$t->{incrBaseBkupNum}) for directory" . " $t->{shareName}"; } + push(@$tarClientCmd, split(/ +/, $args)); } # # Merge variables into @tarClientCmd # - my $vars = { + my $args = { host => $t->{host}, hostIP => $t->{hostIP}, + client => $t->{client}, incrDate => $incrDate, shareName => $t->{shareName}, + fileList => \@fileList, tarPath => $conf->{TarClientPath}, sshPath => $conf->{SshPath}, }; - my @cmd = @tarClientCmd; - @tarClientCmd = (); - foreach my $arg ( @cmd ) { - next if ( $arg =~ /^\s*$/ ); - if ( $arg =~ /^\$fileList(\+?)/ ) { - my $esc = $1 eq "+"; - foreach $arg ( @fileList ) { - $arg = $bpc->shellEscape($arg) if ( $esc ); - push(@tarClientCmd, $arg); - } - } else { - $arg =~ s{\$(\w+)(\+?)}{ - defined($vars->{$1}) - ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1}) - : "\$$1" - }eg; - push(@tarClientCmd, $arg); - } - } + from_to($args->{shareName}, "utf8", $conf->{ClientCharset}) + if ( $conf->{ClientCharset} ne "" ); + $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args); if ( !defined($t->{xferPid} = open(TAR, "-|")) ) { $t->{_errStr} = "Can't fork to run tar"; return; @@ -204,13 +185,17 @@ sub start # # Run the tar command # - exec(@tarClientCmd); + alarm(0); + $bpc->cmdExecOrEval($tarClientCmd, $args); # should not be reached, but just in case... - $t->{_errStr} = "Can't exec @tarClientCmd"; + $t->{_errStr} = "Can't exec @$tarClientCmd"; return; } - $t->{XferLOG}->write(\"Running: @tarClientCmd\n"); - alarm($conf->{SmbClientTimeout}); + my $str = "Running: " . $bpc->execCmd2ShellCmd(@$tarClientCmd) . "\n"; + from_to($str, $conf->{ClientCharset}, "utf8") + if ( $conf->{ClientCharset} ne "" ); + $t->{XferLOG}->write(\"Running: @$tarClientCmd\n"); + alarm($conf->{ClientTimeout}); $t->{_errStr} = undef; return $logMsg; } @@ -224,7 +209,12 @@ sub readOutput my $mesg; if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) { vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0; - if ( !close($t->{pipeTar}) ) { + if ( !close($t->{pipeTar}) && $? != 256 ) { + # + # Tar 1.16 uses exit status 1 (256) when some files + # changed during archive creation. We allow this + # as a benign error and consider the archive ok + # $t->{tarOut} .= "Tar exited with error $? ($!) status\n"; $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} ); } @@ -232,21 +222,31 @@ sub readOutput $t->{tarOut} .= $mesg; } } + my $logFileThres = $t->{type} eq "restore" ? 1 : 2; while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) { $_ = $1; $t->{tarOut} = $2; - $t->{XferLOG}->write(\"$_\n"); + from_to($_, $conf->{ClientCharset}, "utf8") + if ( $conf->{ClientCharset} ne "" ); # # refresh our inactivity alarm # - alarm($conf->{SmbClientTimeout}); + alarm($conf->{ClientTimeout}) if ( !$t->{abort} ); $t->{lastOutputLine} = $_ if ( !/^$/ ); - if ( /^Total bytes written: / ) { + if ( /^Total bytes (written|read): / ) { + $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 ); $t->{xferOK} = 1; } elsif ( /^\./ ) { + $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= $logFileThres ); $t->{fileCnt}++; } else { - $t->{xferErrCnt}++; + # + # Ignore annoying log message on incremental for tar 1.15.x + # + if ( !/: file is unchanged; not dumped$/ && !/: socket ignored$/ ) { + $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 ); + $t->{xferErrCnt}++; + } # # If tar encounters a minor error, it will exit with a non-zero # status. We still consider that ok. Remember if tar prints @@ -254,11 +254,34 @@ sub readOutput # $t->{tarBadExitOk} = 1 if ( $t->{xferOK} && /Error exit delayed from previous / ); + # + # Also remember files that had read errors + # + if ( /: \.\/(.*): Read error at byte / ) { + my $badFile = $1; + push(@{$t->{badFiles}}, { + share => $t->{shareName}, + file => $badFile + }); + } + } } return 1; } +sub abort +{ + my($t, $reason) = @_; + my @xferPid = $t->xferPid; + + $t->{abort} = 1; + $t->{abortReason} = $reason; + if ( @xferPid ) { + kill($t->{bpc}->sigName2num("INT"), @xferPid); + } +} + sub setSelectMask { my($t, $FDreadRef) = @_; @@ -277,7 +300,7 @@ sub xferPid { my($t) = @_; - return $t->{xferPid}; + return ($t->{xferPid}); } sub logMsg