1de40ff101508ae0636c28ec52fddc50264f82c5
[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 2.1.0beta0, released 20 Mar 2004.
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
96         #
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" ) {
102                 $conf->{$param} = {
103                         $conf->{TarShareName}[0] => $conf->{$param}
104                 };
105             } elsif ( ref($conf->{$param}) eq "HASH" ) {
106                 # do nothing
107             } else {
108                 $conf->{$param} = {
109                         $conf->{TarShareName}[0] => [ $conf->{$param} ]
110                 };
111             }
112         }
113         if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
114             foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
115             {
116                 $file = ".$file" if ( $file =~ /^\// );
117                 push(@fileList, "--exclude=$file");
118             }
119         }
120         if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
121             foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
122                 $file = ".$file" if ( $file =~ /^\// );
123                 push(@fileList, $file);
124             }
125         } else {
126             push(@fileList, ".");
127         }
128         if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) {
129             $tarClientCmd = $conf->{TarClientCmd};
130         } else {
131             $tarClientCmd = [split(/ +/, $conf->{TarClientCmd})];
132         }
133         my $args;
134         if ( $t->{type} eq "full" ) {
135             $args = $conf->{TarFullArgs};
136             $logMsg = "full backup started for directory $t->{shareName}";
137         } else {
138             $incrDate = $bpc->timeStamp($t->{lastFull} - 3600, 1);
139             $args = $conf->{TarIncrArgs};
140             $logMsg = "incr backup started back to $incrDate for directory"
141                     . " $t->{shareName}";
142         }
143         push(@$tarClientCmd, split(/ +/, $args));
144     }
145     #
146     # Merge variables into @tarClientCmd
147     #
148     my $args = {
149         host      => $t->{host},
150         hostIP    => $t->{hostIP},
151         client    => $t->{client},
152         incrDate  => $incrDate,
153         shareName => $t->{shareName},
154         fileList  => \@fileList,
155         tarPath   => $conf->{TarClientPath},
156         sshPath   => $conf->{SshPath},
157     };
158     $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args);
159     if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
160         $t->{_errStr} = "Can't fork to run tar";
161         return;
162     }
163     $t->{pipeTar} = *TAR;
164     if ( !$t->{xferPid} ) {
165         #
166         # This is the tar child.
167         #
168         setpgrp 0,0;
169         if ( $t->{type} eq "restore" ) {
170             #
171             # For restores, close the write end of the pipe,
172             # clone STDIN to RH
173             #
174             close($t->{pipeWH});
175             close(STDERR);
176             open(STDERR, ">&STDOUT");
177             close(STDIN);
178             open(STDIN, "<&$t->{pipeRH}");
179         } else {
180             #
181             # For backups, close the read end of the pipe,
182             # clone STDOUT to WH, and STDERR to STDOUT
183             #
184             close($t->{pipeRH});
185             close(STDERR);
186             open(STDERR, ">&STDOUT");
187             open(STDOUT, ">&$t->{pipeWH}");
188         }
189         #
190         # Run the tar command
191         #
192         alarm(0);
193         $bpc->cmdExecOrEval($tarClientCmd, $args);
194         # should not be reached, but just in case...
195         $t->{_errStr} = "Can't exec @$tarClientCmd";
196         return;
197     }
198     my $str = "Running: " . $bpc->execCmd2ShellCmd(@$tarClientCmd) . "\n";
199     $t->{XferLOG}->write(\"Running: @$tarClientCmd\n");
200     alarm($conf->{ClientTimeout});
201     $t->{_errStr} = undef;
202     return $logMsg;
203 }
204
205 sub readOutput
206 {
207     my($t, $FDreadRef, $rout) = @_;
208     my $conf = $t->{conf};
209
210     if ( vec($rout, fileno($t->{pipeTar}), 1) ) {
211         my $mesg;
212         if ( sysread($t->{pipeTar}, $mesg, 8192) <= 0 ) {
213             vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 0;
214             if ( !close($t->{pipeTar}) ) {
215                 $t->{tarOut} .= "Tar exited with error $? ($!) status\n";
216                 $t->{xferOK} = 0 if ( !$t->{tarBadExitOk} );
217             }
218         } else {
219             $t->{tarOut} .= $mesg;
220         }
221     }
222     while ( $t->{tarOut} =~ /(.*?)[\n\r]+(.*)/s ) {
223         $_ = $1;
224         $t->{tarOut} = $2;
225         #
226         # refresh our inactivity alarm
227         #
228         alarm($conf->{ClientTimeout}) if ( !$t->{abort} );
229         $t->{lastOutputLine} = $_ if ( !/^$/ );
230         if ( /^Total bytes written: / ) {
231             $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
232             $t->{xferOK} = 1;
233         } elsif ( /^\./ ) {
234             $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 2 );
235             $t->{fileCnt}++;
236         } else {
237             $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
238             $t->{xferErrCnt}++;
239             #
240             # If tar encounters a minor error, it will exit with a non-zero
241             # status.  We still consider that ok.  Remember if tar prints
242             # this message indicating a non-fatal error.
243             #
244             $t->{tarBadExitOk} = 1
245                     if ( $t->{xferOK} && /Error exit delayed from previous / );
246             #
247             # Also remember files that had read errors
248             #
249             if ( /: \.\/(.*): Read error at byte / ) {
250                 my $badFile = $1;
251                 push(@{$t->{badFiles}}, {
252                         share => $t->{shareName},
253                         file  => $badFile
254                     });
255             }
256
257         }
258     }
259     return 1;
260 }
261
262 sub abort
263 {
264     my($t, $reason) = @_;
265     my @xferPid = $t->xferPid;
266
267     $t->{abort} = 1;
268     $t->{abortReason} = $reason;
269     if ( @xferPid ) {
270         kill($t->{bpc}->sigName2num("INT"), @xferPid);
271     }
272 }
273
274 sub setSelectMask
275 {
276     my($t, $FDreadRef) = @_;
277
278     vec($$FDreadRef, fileno($t->{pipeTar}), 1) = 1;
279 }
280
281 sub errStr
282 {
283     my($t) = @_;
284
285     return $t->{_errStr};
286 }
287
288 sub xferPid
289 {
290     my($t) = @_;
291
292     return ($t->{xferPid});
293 }
294
295 sub logMsg
296 {
297     my($t, $msg) = @_;
298
299     push(@{$t->{_logMsg}}, $msg);
300 }
301
302 sub logMsgGet
303 {
304     my($t) = @_;
305
306     return shift(@{$t->{_logMsg}});
307 }
308
309 #
310 # Returns a hash ref giving various status information about
311 # the transfer.
312 #
313 sub getStats
314 {
315     my($t) = @_;
316
317     return { map { $_ => $t->{$_} }
318             qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
319                xferOK hostAbort hostError lastOutputLine)
320     };
321 }
322
323 sub getBadFiles
324 {
325     my($t) = @_;
326
327     return @{$t->{badFiles}};
328 }
329
330 1;