e4e4b3cd17f9a65421311b6c881d1d854b1ce719
[BackupPC.git] / lib / BackupPC / Xfer / BackupPCd.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Xfer::BackupPCd package
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a BackupPC::Xfer::BackupPCd class for managing
8 #   the backuppcd-based transport of backup data from the client.
9 #
10 # AUTHOR
11 #   Craig Barratt  <cbarratt@users.sourceforge.net>
12 #
13 # COPYRIGHT
14 #   Copyright (C) 2006  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.0alpha, released 23 Jan 2006.
33 #
34 # See http://backuppc.sourceforge.net.
35 #
36 #========================================================================
37
38 package BackupPC::Xfer::BackupPCd;
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         badFiles  => [],
54
55         #
56         # Various stats
57         #
58         byteCnt         => 0,
59         fileCnt         => 0,
60         xferErrCnt      => 0,
61         xferBadShareCnt => 0,
62         xferBadFileCnt  => 0,
63         xferOK          => 0,
64
65         #
66         # User's args
67         #
68         %$args,
69     }, $class;
70
71     return $t;
72 }
73
74 sub args
75 {
76     my($t, $args) = @_;
77
78     foreach my $arg ( keys(%$args) ) {
79         $t->{$arg} = $args->{$arg};
80     }
81 }
82
83 sub useTar
84 {
85     return 0;
86 }
87
88 sub start
89 {
90     my($t) = @_;
91     my $bpc = $t->{bpc};
92     my $conf = $t->{conf};
93     my(@fileList, $bpcdCmd, $bpcdArgs, $logMsg, $incrDate,
94         $incrFlag, $restoreDir);
95
96     #
97     # We add a slash to the share name we pass to bpcd
98     #
99     ($t->{shareNameSlash} = "$t->{shareName}/") =~ s{//+$}{/};
100
101     if ( $t->{type} eq "restore" ) {
102         $bpcdCmd = $conf->{BackupPCdRestoreCmd};
103         $restoreDir = "$t->{shareName}/$t->{pathHdrDest}";
104         $restoreDir    =~ s{//+}{/}g;
105         $logMsg = "restore started below directory $t->{shareName}"
106                 . " to host $t->{host}";
107     } else {
108         if ( $t->{type} eq "full" ) {
109             if ( $t->{partialNum} ) {
110                 $logMsg = "full backup started for directory $t->{shareName};"
111                         . " updating partial $t->{partialNum}";
112             } else {
113                 $logMsg = "full backup started for directory $t->{shareName}";
114             }
115             $incrFlag = 0;
116         } else {
117             #
118             # TODO: fix this message - just refer to the backup, not time?
119             #
120             $incrDate = $bpc->timeStamp($t->{lastFull} - 3600, 1);
121             $logMsg = "incr backup started back to $incrDate for directory"
122                     . " $t->{shareName}";
123             $incrFlag = 1;
124         }
125         $bpcdCmd = $conf->{BackupPCdCmd};
126     }
127
128     #
129     # Merge variables into $bpcdCmd
130     #
131     my $args = {
132         host           => $t->{host},
133         hostIP         => $t->{hostIP},
134         client         => $t->{client},
135         shareName      => $t->{shareName},
136         shareNameSlash => $t->{shareNameSlash},
137         restoreDir     => $restoreDir,
138         bpcdPath       => $conf->{BackupPCdPath},
139         sshPath        => $conf->{SshPath},
140         topDir         => $bpc->TopDir(),
141         poolDir        => $t->{compress} ? $bpc->{CPoolDir} : $bpc->{PoolDir},
142         poolCompress   => $t->{compress} + 0,
143         incrFlag       => $incrFlag,
144     };
145     $bpcdCmd = $bpc->cmdVarSubstitute($bpcdCmd, $args);
146
147     $t->{bpcdCmd} = $bpcdCmd;
148
149     delete($t->{_errStr});
150
151     return $logMsg;
152 }
153
154 sub run
155 {
156     my($t) = @_;
157     my $bpc = $t->{bpc};
158     my $conf = $t->{conf};
159     my($remoteSend, $remoteDir, $remoteDirDaemon);
160     my $error;
161     my $stats;
162
163     # NO: alarm($conf->{ClientTimeout});
164
165     #
166     # Run backupcd command
167     #
168     my $str = "Running: "
169             . $t->{bpc}->execCmd2ShellCmd(@{$t->{bpcdCmd}})
170             . "\n";
171     $t->{XferLOG}->write(\$str);
172
173     #
174     #
175     #
176     
177     $bpc->cmdSystemOrEvalLong($t->{bpcdCmd},
178         sub {
179             # write stdout to the XferLOG
180             my($str) = @_;
181             $t->{XferLOG}->write(\$str);
182         }, 
183         0,                  # also catch stderr
184         $t->{pidHandler} 
185     );
186
187     #
188     # TODO: generate sensible stats by parsing the output of
189     # backuppcd.  Get error and fail status.
190     #
191     if ( !defined($error) && defined($stats) ) {
192         $t->{xferOK} = 1;
193     } else {
194         $t->{xferOK} = 0;
195     }
196     $t->{xferErrCnt} = $stats->{errorCnt};
197     $t->{byteCnt}    = $stats->{TotalFileSize};
198     $t->{fileCnt}    = $stats->{TotalFileCnt};
199     my $str = "Done: $t->{fileCnt} files, $t->{byteCnt} bytes\n";
200     $t->{XferLOG}->write(\$str);
201
202     $t->{hostError} = $error if ( defined($error) );
203
204     if ( $t->{type} eq "restore" ) {
205         return (
206             $t->{fileCnt},
207             $t->{byteCnt},
208             0,
209             0
210         );
211     } else {
212         return (
213             0,
214             $stats->{ExistFileCnt},
215             $stats->{ExistFileSize},
216             $stats->{ExistFileCompSize},
217             $stats->{TotalFileCnt},
218             $stats->{TotalFileSize}
219         );
220     }
221 }
222
223 sub abort
224 {
225     my($t, $reason) = @_;
226
227     # TODO
228     return 1;
229 }
230
231 sub errStr
232 {
233     my($t) = @_;
234
235     return $t->{_errStr};
236 }
237
238 sub xferPid
239 {
240     my($t) = @_;
241
242     return ();
243 }
244
245 #
246 # Returns a hash ref giving various status information about
247 # the transfer.
248 #
249 sub getStats
250 {
251     my($t) = @_;
252
253     return { map { $_ => $t->{$_} }
254             qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
255                xferOK hostAbort hostError lastOutputLine)
256     };
257 }
258
259 sub getBadFiles
260 {
261     my($t) = @_;
262
263     return @{$t->{badFiles}};
264 }
265
266 1;