* lots of minor changes to prepare for 3.0.0beta0 release
[BackupPC.git] / lib / BackupPC / Xfer / Tar.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Xfer::Tar package
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a BackupPC::Xfer::Tar class for managing
8 #   the tar-based transport of backup data from the client.
9 #
10 # AUTHOR
11 #   Craig Barratt  <cbarratt@users.sourceforge.net>
12 #
13 # COPYRIGHT
14 #   Copyright (C) 2001-2003  Craig Barratt
15 #
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.
20 #
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.
25 #
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
29 #
30 #========================================================================
31 #
32 # Version 3.0.0beta0, released 11 Jul 2006.
33 #
34 # See http://backuppc.sourceforge.net.
35 #
36 #========================================================================
37
38 package BackupPC::Xfer::Tar;
39
40 use strict;
41
42 sub new
43 {
44     my($class, $bpc, $args) = @_;
45
46     $args ||= {};
47     my $t = bless {
48         bpc       => $bpc,
49         conf      => { $bpc->Conf },
50         host      => "",
51         hostIP    => "",
52         shareName => "",
53         pipeRH    => undef,
54         pipeWH    => undef,
55         badFiles  => [],
56         %$args,
57     }, $class;
58
59     return $t;
60 }
61
62 sub args
63 {
64     my($t, $args) = @_;
65
66     foreach my $arg ( keys(%$args) ) {
67         $t->{$arg} = $args->{$arg};
68     }
69 }
70
71 sub useTar
72 {
73     return 1;
74 }
75
76 sub start
77 {
78     my($t) = @_;
79     my $bpc = $t->{bpc};
80     my $conf = $t->{conf};
81     my(@fileList, $tarClientCmd, $logMsg, $incrDate);
82     local(*TAR);
83
84     if ( $t->{type} eq "restore" ) {
85         $tarClientCmd = $conf->{TarClientRestoreCmd};
86         $logMsg = "restore started below directory $t->{shareName}";
87         #
88         # restores are considered to work unless we see they fail
89         # (opposite to backups...)
90         #
91         $t->{xferOK} = 1;
92     } else {
93         #
94         # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
95         # into a hash of arrays of files, and $conf->{TarShareName}
96         # to an array
97         #
98         $bpc->backupFileConfFix($conf, "TarShareName");
99
100         if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
101             foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
102             {
103                 $file = ".$file" if ( $file =~ /^\// );
104                 push(@fileList, "--exclude=$file");
105             }
106         }
107         if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
108             foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
109                 $file = ".$file" if ( $file =~ /^\// );
110                 push(@fileList, $file);
111             }
112         } else {
113             push(@fileList, ".");
114         }
115         if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) {
116             $tarClientCmd = $conf->{TarClientCmd};
117         } else {
118             $tarClientCmd = [split(/ +/, $conf->{TarClientCmd})];
119         }
120         my $args;
121         if ( $t->{type} eq "full" ) {
122             $args = $conf->{TarFullArgs};
123             $logMsg = "full backup started for directory $t->{shareName}";
124         } else {
125             $incrDate = $bpc->timeStamp($t->{incrBaseTime} - 3600, 1);
126             $args = $conf->{TarIncrArgs};
127             $logMsg = "incr backup started back to $incrDate"
128                     . " (backup #$t->{incrBaseBkupNum}) for directory"
129                     . " $t->{shareName}";
130         }
131         push(@$tarClientCmd, split(/ +/, $args));
132     }
133     #
134     # Merge variables into @tarClientCmd
135     #
136     my $args = {
137         host      => $t->{host},
138         hostIP    => $t->{hostIP},
139         client    => $t->{client},
140         incrDate  => $incrDate,
141         shareName => $t->{shareName},
142         fileList  => \@fileList,
143         tarPath   => $conf->{TarClientPath},
144         sshPath   => $conf->{SshPath},
145     };
146     $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args);
147     if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
148         $t->{_errStr} = "Can't fork to run tar";
149         return;
150     }
151     $t->{pipeTar} = *TAR;
152     if ( !$t->{xferPid} ) {
153         #
154         # This is the tar child.
155         #
156         setpgrp 0,0;
157         if ( $t->{type} eq "restore" ) {
158             #
159             # For restores, close the write end of the pipe,
160             # clone STDIN to RH
161             #
162             close($t->{pipeWH});
163             close(STDERR);
164             open(STDERR, ">&STDOUT");
165             close(STDIN);
166             open(STDIN, "<&$t->{pipeRH}");
167         } else {
168             #
169             # For backups, close the read end of the pipe,
170             # clone STDOUT to WH, and STDERR to STDOUT
171             #
172             close($t->{pipeRH});
173             close(STDERR);
174             open(STDERR, ">&STDOUT");
175             open(STDOUT, ">&$t->{pipeWH}");
176         }
177         #
178         # Run the tar command
179         #
180         alarm(0);
181         $bpc->cmdExecOrEval($tarClientCmd, $args);
182         # should not be reached, but just in case...
183         $t->{_errStr} = "Can't exec @$tarClientCmd";
184         return;
185     }
186     my $str = "Running: " . $bpc->execCmd2ShellCmd(@$tarClientCmd) . "\n";
187     $t->{XferLOG}->write(\"Running: @$tarClientCmd\n");
188     alarm($conf->{ClientTimeout});
189     $t->{_errStr} = undef;
190     return $logMsg;
191 }
192
193 sub readOutput
194 {
195     my($t, $FDreadRef, $rout) = @_;
196     my $conf = $t->{conf};
197
198     if ( vec($rout, fileno($t->{pipeTar}), 1) ) {
199         my $mesg;
200         if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) {
201             vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0;
202             if ( !close($t->{pipeTar}) ) {
203                 $t->{tarOut} .= "Tar exited with error $? ($!) status\n";
204                 $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} );
205             }
206         } else {
207             $t->{tarOut} .= $mesg;
208         }
209     }
210     while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) {
211         $_ = $1;
212         $t->{tarOut} = $2;
213         #
214         # refresh our inactivity alarm
215         #
216         alarm($conf->{ClientTimeout}) if ( !$t->{abort} );
217         $t->{lastOutputLine} = $_ if ( !/^$/ );
218         if ( /^Total bytes written: / ) {
219             $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
220             $t->{xferOK} = 1;
221         } elsif ( /^\./ ) {
222             $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 2 );
223             $t->{fileCnt}++;
224         } else {
225             #
226             # Ignore annoying log message on incremental for tar 1.15.x
227             #
228             if ( !/: file is unchanged; not dumped$/ ) {
229                 $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
230                 $t->{xferErrCnt}++;
231             }
232             #
233             # If tar encounters a minor error, it will exit with a non-zero
234             # status.  We still consider that ok.  Remember if tar prints
235             # this message indicating a non-fatal error.
236             #
237             $t->{tarBadExitOk} = 1
238                     if ( $t->{xferOK} && /Error exit delayed from previous / );
239             #
240             # Also remember files that had read errors
241             #
242             if ( /: \.\/(.*): Read error at byte / ) {
243                 my $badFile = $1;
244                 push(@{$t->{badFiles}}, {
245                         share => $t->{shareName},
246                         file  => $badFile
247                     });
248             }
249
250         }
251     }
252     return 1;
253 }
254
255 sub abort
256 {
257     my($t, $reason) = @_;
258     my @xferPid = $t->xferPid;
259
260     $t->{abort} = 1;
261     $t->{abortReason} = $reason;
262     if ( @xferPid ) {
263         kill($t->{bpc}->sigName2num("INT"), @xferPid);
264     }
265 }
266
267 sub setSelectMask
268 {
269     my($t, $FDreadRef) = @_;
270
271     vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1;
272 }
273
274 sub errStr
275 {
276     my($t) = @_;
277
278     return $t->{_errStr};
279 }
280
281 sub xferPid
282 {
283     my($t) = @_;
284
285     return ($t->{xferPid});
286 }
287
288 sub logMsg
289 {
290     my($t, $msg) = @_;
291
292     push(@{$t->{_logMsg}}, $msg);
293 }
294
295 sub logMsgGet
296 {
297     my($t) = @_;
298
299     return shift(@{$t->{_logMsg}});
300 }
301
302 #
303 # Returns a hash ref giving various status information about
304 # the transfer.
305 #
306 sub getStats
307 {
308     my($t) = @_;
309
310     return { map { $_ => $t->{$_} }
311             qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
312                xferOK hostAbort hostError lastOutputLine)
313     };
314 }
315
316 sub getBadFiles
317 {
318     my($t) = @_;
319
320     return @{$t->{badFiles}};
321 }
322
323 1;