* Added multi-level incrementals. Still needs testing.
[BackupPC.git] / bin / BackupPC_archive
1 #!/bin/perl
2 #============================================================= -*-perl-*-
3 #
4 # BackupPC_archive: Archive files for an archive client.
5 #
6 # DESCRIPTION
7 #
8 #   Usage: BackupPC_archive <user> <archiveclient> <reqFileName>
9 #
10 # AUTHOR
11 #   Josh Marshall
12 #
13 # COPYRIGHT
14 #   Copyright (C) 2001-2004  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 use strict;
39 no  utf8;
40 use lib "/usr/local/BackupPC/lib";
41 use BackupPC::Lib;
42 use BackupPC::FileZIO;
43 use BackupPC::Xfer::Archive;
44
45 use vars qw( %ArchiveReq );
46
47 ###########################################################################
48 # Initialize
49 ###########################################################################
50
51 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
52 my $TopDir = $bpc->TopDir();
53 my $BinDir = $bpc->BinDir();
54 my %Conf   = $bpc->Conf();
55 my $NeedPostCmd;
56
57 my($user, $host, $client, $reqFileName, %stat);
58
59 $bpc->ChildInit();
60
61 if ( @ARGV != 3 ) {
62     print("usage: $0 <user> <archiveclient> <reqFileName>\n");
63     exit(1);
64 }
65 $user = $1 if ( $ARGV[0] =~ /(.+)/ );
66 $client = $1 if ( $ARGV[1] =~ /(.+)/ );
67 if ( $ARGV[2] !~ /^([\w.]+)$/ ) {
68     print("$0: bad reqFileName (arg #3): $ARGV[2]\n");
69     exit(1);
70 }
71 $reqFileName = $1;
72
73 my $startTime = time();
74
75 my $Dir     = "$TopDir/pc/$client";
76 my @xferPid = ();
77
78 #
79 # Catch various signals
80 #
81 $SIG{INT}  = \&catch_signal;
82 $SIG{ALRM} = \&catch_signal;
83 $SIG{TERM} = \&catch_signal;
84 $SIG{PIPE} = \&catch_signal;
85 $SIG{STOP} = \&catch_signal;
86 $SIG{TSTP} = \&catch_signal;
87 $SIG{TTIN} = \&catch_signal;
88 my $Pid = $$;
89
90 mkpath($Dir, 0, 0777) if ( !-d $Dir );
91 if ( !-f "$Dir/LOCK" ) {
92     open(LOCK, ">", "$Dir/LOCK") && close(LOCK);
93 }
94 open(LOG, ">>", "$Dir/LOG");
95 select(LOG); $| = 1; select(STDOUT);
96
97
98 #
99 # Read the request file
100 #
101
102 if ( !(my $ret = do "$Dir/$reqFileName") ) {
103     my $err;
104     if ( $@ ) {
105         $err = "couldn't parse $Dir/$reqFileName: $@";
106     } elsif ( !defined($ret) ) {
107         $err = "couldn't do $Dir/$reqFileName: $!";
108     } else {
109         $err = "couldn't run $Dir/$reqFileName";
110     }
111     $stat{hostError} = $err;
112     exit(ArchiveCleanup($client));
113 }
114
115 #
116 # Re-read config file, so we can include the PC-specific config
117 #
118 if ( defined(my $error = $bpc->ConfigRead($client)) ) {
119     $stat{hostError} = "Can't read PC's config file: $error";
120     exit(ArchiveCleanup($client));
121 }
122 %Conf = $bpc->Conf();
123
124 #
125 # Make sure we eventually timeout if there is no activity from
126 # the data transport program.
127 #
128 alarm($Conf{ClientTimeout});
129
130 #
131 # See if the host name is aliased
132 #
133 if ( $Conf{ClientNameAlias} ne "" ) {
134     $host = $Conf{ClientNameAlias};
135 } else {
136     $host = $client;
137 }
138
139 #
140 # Setup file extension for compression and open ArchiveLOG output file
141 #
142 if ( $Conf{CompressLevel} && !BackupPC::FileZIO->compOk ) {
143     $stat{hostError} = "Compress::Zlib not found";
144     exit(ArchiveCleanup($client));
145 }
146 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
147 my $ArchiveLOG = BackupPC::FileZIO->open("$Dir/ArchiveLOG$fileExt", 1,
148                                      $Conf{CompressLevel});
149 my($logMsg, $xfer);
150
151 $stat{xferOK} = 1;
152 $stat{hostAbort} = undef;
153 $stat{hostError} = $stat{lastOutputLine} = undef;
154 local(*RH, *WH);
155
156 #
157 # Run an optional pre-archive command
158 #
159 UserCommandRun("ArchivePreUserCmd");
160 if ( $? && $Conf{UserCmdCheckStatus} ) {
161     $stat{hostError} = "ArchivePreUserCmd returned error status $?";
162     exit(ArchiveCleanup($client));
163 }
164 $NeedPostCmd = 1;
165
166 $xfer = BackupPC::Xfer::Archive->new($bpc);
167
168 #
169 # Run the transport program
170 #
171
172 my $xferArgs = {
173     client       => $client,
174     host         => $host,
175     user         => $ArchiveReq{user},
176     type         => "archive",
177     XferLOG      => $ArchiveLOG,
178     XferMethod   => $Conf{XferMethod},
179     pathHdrSrc   => $ArchiveReq{pathHdrSrc},
180     pathHdrDest  => $ArchiveReq{pathHdrDest},
181     HostList     => \@{$ArchiveReq{HostList}},
182     BackupList   => \@{$ArchiveReq{BackupList}},
183     archiveloc   => $ArchiveReq{archiveloc},
184     parfile      => $ArchiveReq{parfile},
185     compression  => $ArchiveReq{compression},
186     compext      => $ArchiveReq{compext},
187     splitsize    => $ArchiveReq{splitsize},
188     pidHandler   => \&pidHandler,
189 };
190
191 $xfer->args($xferArgs);
192
193 if ( !defined($logMsg = $xfer->start()) ) {
194     UserCommandRun("ArchivePostUserCmd") if ( $NeedPostCmd );
195     $stat{hostError} = "xfer start failed: ", $xfer->errStr;
196     exit(ArchiveCleanup($client));
197 }
198
199 print(LOG $bpc->timeStamp, "Starting archive\n");
200 print("started_archive\n");
201 $xfer->run();
202 $stat{xferOK} = 0 if ( defined($stat{hostError} = $xfer->errStr) );
203 alarm(0);
204
205 exit(ArchiveCleanup($client));
206
207 ###########################################################################
208 # Subroutines
209 ###########################################################################
210
211 sub catch_signal
212 {
213     my $signame = shift;
214
215     #
216     # Children quit quietly on ALRM
217     #
218     exit(1) if ( $Pid != $$ && $signame eq "ALRM" );
219
220     #
221     # Ignore signals in children
222     #
223     return if ( $Pid != $$ );
224
225     #
226     # Note: needs to be tested for each kind of XferMethod
227     #
228     print(LOG $bpc->timeStamp, "cleaning up after signal $signame\n");
229     $SIG{$signame} = 'IGNORE';
230     $ArchiveLOG->write(\"exiting after signal $signame\n");
231     $stat{xferOK} = 0;
232     if ( $signame eq "INT" ) {
233         $stat{hostError} = "aborted by user (signal=$signame)";
234     } else {
235         $stat{hostError} = "aborted by signal=$signame";
236     }
237     exit(ArchiveCleanup($client));
238 }
239
240 #
241 # Cleanup and update the archive status
242 #
243 sub ArchiveCleanup
244 {
245     my($client) = @_;
246
247     $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );
248
249     if ( !$stat{xferOK} ) {
250         #
251         # Kill off the tranfer program, first nicely then forcefully.
252         # We use negative PIDs to make sure all processes in each
253         # group get the signal.
254         #
255         if ( @xferPid ) {
256             foreach my $pid ( @xferPid ) {
257                 kill($bpc->sigName2num("INT"), -$pid);
258             }
259             sleep(1);
260             foreach my $pid ( @xferPid ) {
261                 kill($bpc->sigName2num("KILL"), -$pid);
262             }
263         }
264     }
265
266     my $lastNum  = -1;
267     my @Archives;
268
269     @Archives = $bpc->ArchiveInfoRead($client);
270     for ( my $i = 0 ; $i < @Archives ; $i++ ) {
271         $lastNum = $Archives[$i]{num} if ( $lastNum < $Archives[$i]{num} );
272     }
273     $lastNum++;
274
275     #
276     # Run an optional post-archive command
277     #
278     if ( $NeedPostCmd ) {
279         UserCommandRun("ArchivePostUserCmd");
280         if ( $? && $Conf{UserCmdCheckStatus} ) {
281             $stat{hostError} = "RestorePreUserCmd returned error status $?";
282             $stat{xferOK} = 0;
283         }
284     }
285
286     rename("$Dir/ArchiveLOG$fileExt", "$Dir/ArchiveLOG.$lastNum$fileExt");
287     rename("$Dir/$reqFileName", "$Dir/ArchiveInfo.$lastNum");
288     my $endTime = time();
289
290     #
291     # If the archive failed, clean up
292     #
293     if ( !$stat{xferOK} ) {
294         $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
295         $stat{hostAbort} = 1;
296         $ArchiveLOG->write(\"Archive failed: $stat{hostError}")
297                                             if ( defined($ArchiveLOG) );
298     }
299
300     $ArchiveLOG->close() if ( defined($ArchiveLOG) );
301
302     #
303     # Add the new archive information to the archive file
304     #
305     @Archives = $bpc->ArchiveInfoRead($client);
306     my $i = @Archives;
307     $Archives[$i]{num}           = $lastNum;
308     $Archives[$i]{startTime}     = $startTime;
309     $Archives[$i]{endTime}       = $endTime;
310     $Archives[$i]{result}        = $stat{xferOK} ? "ok" : "failed";
311     $Archives[$i]{errorMsg}      = $stat{hostError};
312
313     while ( @Archives > $Conf{ArchiveInfoKeepCnt} ) {
314         my $num = $Archives[0]{num};
315         unlink("$Dir/ArchiveLOG.$num.z");
316         unlink("$Dir/ArchiveLOG.$num");
317         unlink("$Dir/ArchiveInfo.$num");
318         shift(@Archives);
319     }
320     $bpc->ArchiveInfoWrite($client, @Archives);
321
322     if ( !$stat{xferOK} ) {
323         print(LOG $bpc->timeStamp, "Archive failed ($stat{hostError})\n");
324         print("archive failed: $stat{hostError}\n");
325         return 1;
326     } else {
327         print(LOG $bpc->timeStamp, "Archive Complete\n");
328         print("archive complete\n");
329         return;
330     }
331 }
332
333 #
334 # The Xfer method might tell us from time to time about processes
335 # it forks.  We tell BackupPC about this (for status displays) and
336 # keep track of the pids in case we cancel the backup
337 #
338 sub pidHandler
339 {
340     @xferPid = @_;
341     @xferPid = grep(/./, @xferPid);
342     return if ( !@xferPid );
343     my @pids = @xferPid;
344     my $str = join(",", @pids);
345     $ArchiveLOG->write(\"Xfer PIDs are now $str\n") if ( defined($ArchiveLOG) );
346     print("xferPids $str\n");
347 }
348
349 #
350 # Run an optional pre- or post-dump command
351 #
352 sub UserCommandRun
353 {
354     my($cmdType) = @_;
355
356     return if ( !defined($Conf{$cmdType}) );
357     my $vars = {
358         xfer         => $xfer,
359         client       => $client,
360         host         => $host,
361         user         => $user,
362         share        => $ArchiveReq{shareDest},
363         XferMethod   => $Conf{XferMethod},
364         HostList     => \@{$ArchiveReq{HostList}},
365         BackupList   => \@{$ArchiveReq{BackupList}},
366         archiveloc   => $ArchiveReq{archiveloc},
367         parfile      => $ArchiveReq{parfile},
368         compression  => $ArchiveReq{compression},
369         compext      => $ArchiveReq{compext},
370         splitsize    => $ArchiveReq{splitsize},
371         sshPath      => $Conf{SshPath},
372         LOG          => *LOG,
373         XferLOG      => $ArchiveLOG,
374         stat         => \%stat,
375         xferOK       => $stat{xferOK} || 0,
376         type         => "archive",
377         cmdType      => $cmdType,
378     };
379     my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars);
380     $ArchiveLOG->write(\"Executing $cmdType: @$cmd\n");
381     #
382     # Run the user's command, dumping the stdout/stderr into the
383     # Xfer log file.  Also supply the optional $vars and %Conf in
384     # case the command is really perl code instead of a shell
385     # command.
386     #
387     $bpc->cmdSystemOrEval($cmd,
388             sub {
389                 $ArchiveLOG->write(\$_[0]);
390             },
391             $vars, \%Conf);
392 }