2 #============================================================= -*-perl-*-
4 # BackupPC_archive: Archive files for an archive client.
8 # Usage: BackupPC_archive <user> <archiveclient> <reqFileName>
14 # Copyright (C) 2001-2004 Craig Barratt
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.
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.
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
30 #========================================================================
32 # Version 3.0.0alpha, released 23 Jan 2006.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
40 use lib "/usr/local/BackupPC/lib";
42 use BackupPC::FileZIO;
43 use BackupPC::Xfer::Archive;
45 use vars qw( %ArchiveReq );
47 ###########################################################################
49 ###########################################################################
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();
57 my($user, $host, $client, $reqFileName, %stat);
62 print("usage: $0 <user> <archiveclient> <reqFileName>\n");
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");
73 my $startTime = time();
75 my $Dir = "$TopDir/pc/$client";
79 # Catch various signals
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;
90 mkpath($Dir, 0, 0777) if ( !-d $Dir );
91 if ( !-f "$Dir/LOCK" ) {
92 open(LOCK, ">", "$Dir/LOCK") && close(LOCK);
94 open(LOG, ">>", "$Dir/LOG");
95 select(LOG); $| = 1; select(STDOUT);
99 # Read the request file
102 if ( !(my $ret = do "$Dir/$reqFileName") ) {
105 $err = "couldn't parse $Dir/$reqFileName: $@";
106 } elsif ( !defined($ret) ) {
107 $err = "couldn't do $Dir/$reqFileName: $!";
109 $err = "couldn't run $Dir/$reqFileName";
111 $stat{hostError} = $err;
112 exit(ArchiveCleanup($client));
116 # Re-read config file, so we can include the PC-specific config
118 if ( defined(my $error = $bpc->ConfigRead($client)) ) {
119 $stat{hostError} = "Can't read PC's config file: $error";
120 exit(ArchiveCleanup($client));
122 %Conf = $bpc->Conf();
125 # Make sure we eventually timeout if there is no activity from
126 # the data transport program.
128 alarm($Conf{ClientTimeout});
131 # See if the host name is aliased
133 if ( $Conf{ClientNameAlias} ne "" ) {
134 $host = $Conf{ClientNameAlias};
140 # Setup file extension for compression and open ArchiveLOG output file
142 if ( $Conf{CompressLevel} && !BackupPC::FileZIO->compOk ) {
143 $stat{hostError} = "Compress::Zlib not found";
144 exit(ArchiveCleanup($client));
146 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
147 my $ArchiveLOG = BackupPC::FileZIO->open("$Dir/ArchiveLOG$fileExt", 1,
148 $Conf{CompressLevel});
152 $stat{hostAbort} = undef;
153 $stat{hostError} = $stat{lastOutputLine} = undef;
157 # Run an optional pre-archive command
159 UserCommandRun("ArchivePreUserCmd");
162 $xfer = BackupPC::Xfer::Archive->new($bpc);
165 # Run the transport program
171 user => $ArchiveReq{user},
173 XferLOG => $ArchiveLOG,
174 XferMethod => $Conf{XferMethod},
175 pathHdrSrc => $ArchiveReq{pathHdrSrc},
176 pathHdrDest => $ArchiveReq{pathHdrDest},
177 HostList => \@{$ArchiveReq{HostList}},
178 BackupList => \@{$ArchiveReq{BackupList}},
179 archiveloc => $ArchiveReq{archiveloc},
180 parfile => $ArchiveReq{parfile},
181 compression => $ArchiveReq{compression},
182 compext => $ArchiveReq{compext},
183 splitsize => $ArchiveReq{splitsize},
184 pidHandler => \&pidHandler,
187 $xfer->args($xferArgs);
189 if ( !defined($logMsg = $xfer->start()) ) {
190 UserCommandRun("ArchivePostUserCmd") if ( $NeedPostCmd );
191 $stat{hostError} = "xfer start failed: ", $xfer->errStr;
192 exit(ArchiveCleanup($client));
195 print(LOG $bpc->timeStamp, "Starting archive\n");
196 print("started_archive\n");
198 $stat{xferOK} = 0 if ( defined($stat{hostError} = $xfer->errStr) );
201 exit(ArchiveCleanup($client));
203 ###########################################################################
205 ###########################################################################
212 # Children quit quietly on ALRM
214 exit(1) if ( $Pid != $$ && $signame eq "ALRM" );
217 # Ignore signals in children
219 return if ( $Pid != $$ );
222 # Note: needs to be tested for each kind of XferMethod
224 print(LOG $bpc->timeStamp, "cleaning up after signal $signame\n");
225 $SIG{$signame} = 'IGNORE';
226 $ArchiveLOG->write(\"exiting after signal $signame\n");
228 if ( $signame eq "INT" ) {
229 $stat{hostError} = "aborted by user (signal=$signame)";
231 $stat{hostError} = "aborted by signal=$signame";
233 exit(ArchiveCleanup($client));
237 # Cleanup and update the archive status
243 $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );
245 if ( !$stat{xferOK} ) {
247 # Kill off the tranfer program, first nicely then forcefully.
248 # We use negative PIDs to make sure all processes in each
249 # group get the signal.
252 foreach my $pid ( @xferPid ) {
253 kill($bpc->sigName2num("INT"), -$pid);
256 foreach my $pid ( @xferPid ) {
257 kill($bpc->sigName2num("KILL"), -$pid);
265 @Archives = $bpc->ArchiveInfoRead($client);
266 for ( my $i = 0 ; $i < @Archives ; $i++ ) {
267 $lastNum = $Archives[$i]{num} if ( $lastNum < $Archives[$i]{num} );
272 # Run an optional post-archive command
274 UserCommandRun("ArchivePostUserCmd") if ( $NeedPostCmd );
276 rename("$Dir/ArchiveLOG$fileExt", "$Dir/ArchiveLOG.$lastNum$fileExt");
277 rename("$Dir/$reqFileName", "$Dir/ArchiveInfo.$lastNum");
278 my $endTime = time();
281 # If the archive failed, clean up
283 if ( !$stat{xferOK} ) {
284 $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
285 $stat{hostAbort} = 1;
286 $ArchiveLOG->write(\"Archive failed: $stat{hostError}")
287 if ( defined($ArchiveLOG) );
290 $ArchiveLOG->close() if ( defined($ArchiveLOG) );
293 # Add the new archive information to the archive file
295 @Archives = $bpc->ArchiveInfoRead($client);
297 $Archives[$i]{num} = $lastNum;
298 $Archives[$i]{startTime} = $startTime;
299 $Archives[$i]{endTime} = $endTime;
300 $Archives[$i]{result} = $stat{xferOK} ? "ok" : "failed";
301 $Archives[$i]{errorMsg} = $stat{hostError};
303 while ( @Archives > $Conf{ArchiveInfoKeepCnt} ) {
304 my $num = $Archives[0]{num};
305 unlink("$Dir/ArchiveLOG.$num.z");
306 unlink("$Dir/ArchiveLOG.$num");
307 unlink("$Dir/ArchiveInfo.$num");
310 $bpc->ArchiveInfoWrite($client, @Archives);
312 if ( !$stat{xferOK} ) {
313 print(LOG $bpc->timeStamp, "Archive failed ($stat{hostError})\n");
314 print("archive failed: $stat{hostError}\n");
317 print(LOG $bpc->timeStamp, "Archive Complete\n");
318 print("archive complete\n");
324 # The Xfer method might tell us from time to time about processes
325 # it forks. We tell BackupPC about this (for status displays) and
326 # keep track of the pids in case we cancel the backup
331 @xferPid = grep(/./, @xferPid);
332 return if ( !@xferPid );
334 my $str = join(",", @pids);
335 $ArchiveLOG->write(\"Xfer PIDs are now $str\n") if ( defined($ArchiveLOG) );
336 print("xferPids $str\n");
340 # Run an optional pre- or post-dump command
346 return if ( !defined($Conf{$cmdType}) );
352 share => $ArchiveReq{shareDest},
353 XferMethod => $Conf{XferMethod},
354 HostList => \@{$ArchiveReq{HostList}},
355 BackupList => \@{$ArchiveReq{BackupList}},
356 archiveloc => $ArchiveReq{archiveloc},
357 parfile => $ArchiveReq{parfile},
358 compression => $ArchiveReq{compression},
359 compext => $ArchiveReq{compext},
360 splitsize => $ArchiveReq{splitsize},
361 sshPath => $Conf{SshPath},
363 XferLOG => $ArchiveLOG,
365 xferOK => $stat{xferOK} || 0,
369 my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars);
370 $ArchiveLOG->write(\"Executing $cmdType: @$cmd\n");
372 # Run the user's command, dumping the stdout/stderr into the
373 # Xfer log file. Also supply the optional $vars and %Conf in
374 # case the command is really perl code instead of a shell
377 $bpc->cmdSystemOrEval($cmd,
379 $ArchiveLOG->write(\$_[0]);