This file is indexed.

/usr/share/backuppc/bin/BackupPC_archive is in backuppc 3.3.0-1ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_archive: Archive files for an archive client.
#
# DESCRIPTION
#
#   Usage: BackupPC_archive <user> <archiveclient> <reqFileName>
#
# AUTHOR
#   Josh Marshall
#
# COPYRIGHT
#   Copyright (C) 2001-2013  Craig Barratt
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#========================================================================
#
# Version 3.3.0, released 14 Apr 2013.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================

use strict;
no  utf8;
use lib "/usr/share/backuppc/lib";
use BackupPC::Lib;
use BackupPC::FileZIO;
use BackupPC::Xfer::Archive;

use vars qw( %ArchiveReq );

###########################################################################
# Initialize
###########################################################################

die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
my $TopDir = $bpc->TopDir();
my $BinDir = $bpc->BinDir();
my %Conf   = $bpc->Conf();
my $NeedPostCmd;

my($user, $host, $client, $reqFileName, %stat);

$bpc->ChildInit();

if ( @ARGV != 3 ) {
    print("usage: $0 <user> <archiveclient> <reqFileName>\n");
    exit(1);
}
$user = $1 if ( $ARGV[0] =~ /(.+)/ );
$client = $1 if ( $ARGV[1] =~ /(.+)/ );
if ( $ARGV[2] !~ /^([\w\.\s-]+)$/ ) {
    print("$0: bad reqFileName (arg #3): $ARGV[2]\n");
    exit(1);
}
$reqFileName = $1;

my $startTime = time();

my $Dir     = "$TopDir/pc/$client";
my @xferPid = ();

#
# Catch various signals
#
$SIG{INT}  = \&catch_signal;
$SIG{ALRM} = \&catch_signal;
$SIG{TERM} = \&catch_signal;
$SIG{PIPE} = \&catch_signal;
$SIG{STOP} = \&catch_signal;
$SIG{TSTP} = \&catch_signal;
$SIG{TTIN} = \&catch_signal;
my $Pid = $$;

mkpath($Dir, 0, 0777) if ( !-d $Dir );
if ( !-f "$Dir/LOCK" ) {
    open(LOCK, ">", "$Dir/LOCK") && close(LOCK);
}

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $logPath = sprintf("$Dir/LOG.%02d%04d", $mon + 1, $year + 1900);

if ( !-f $logPath ) {
    #
    # Compress and prune old log files
    #
    my $lastLog = $Conf{MaxOldPerPCLogFiles} - 1;
    foreach my $file ( $bpc->sortedPCLogFiles($client) ) {
        if ( $lastLog <= 0 ) {
            unlink($file);
            next;
        }
        $lastLog--;
        next if ( $file =~ /\.z$/ || !$Conf{CompressLevel} );
        BackupPC::FileZIO->compressCopy($file,
                                        "$file.z",
                                        undef,
                                        $Conf{CompressLevel}, 1);
    }
}
open(LOG, ">>", $logPath);
select(LOG); $| = 1; select(STDOUT);

#
# Read the request file
#

if ( !(my $ret = do "$Dir/$reqFileName") ) {
    my $err;
    if ( $@ ) {
        $err = "couldn't parse $Dir/$reqFileName: $@";
    } elsif ( !defined($ret) ) {
	$err = "couldn't do $Dir/$reqFileName: $!";
    } else {
	$err = "couldn't run $Dir/$reqFileName";
    }
    $stat{hostError} = $err;
    exit(ArchiveCleanup($client));
}

#
# Re-read config file, so we can include the PC-specific config
#
if ( defined(my $error = $bpc->ConfigRead($client)) ) {
    $stat{hostError} = "Can't read PC's config file: $error";
    exit(ArchiveCleanup($client));
}
%Conf = $bpc->Conf();

#
# Make sure we eventually timeout if there is no activity from
# the data transport program.
#
alarm($Conf{ClientTimeout});

#
# See if the host name is aliased
#
if ( $Conf{ClientNameAlias} ne "" ) {
    $host = $Conf{ClientNameAlias};
} else {
    $host = $client;
}

#
# Setup file extension for compression and open ArchiveLOG output file
#
if ( $Conf{CompressLevel} && !BackupPC::FileZIO->compOk ) {
    $stat{hostError} = "Compress::Zlib not found";
    exit(ArchiveCleanup($client));
}
my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
my $ArchiveLOG = BackupPC::FileZIO->open("$Dir/ArchiveLOG$fileExt", 1,
                                     $Conf{CompressLevel});
my($logMsg, $xfer);

$stat{xferOK} = 1;
$stat{hostAbort} = undef;
$stat{hostError} = $stat{lastOutputLine} = undef;
local(*RH, *WH);

#
# Run an optional pre-archive command
#
UserCommandRun("ArchivePreUserCmd");
if ( $? && $Conf{UserCmdCheckStatus} ) {
    $stat{hostError} = "ArchivePreUserCmd returned error status $?";
    exit(ArchiveCleanup($client));
}
$NeedPostCmd = 1;

$xfer = BackupPC::Xfer::Archive->new($bpc);

#
# Run the transport program
#

my $xferArgs = {
    client       => $client,
    host         => $host,
    user         => $ArchiveReq{user},
    type         => "archive",
    XferLOG      => $ArchiveLOG,
    XferMethod   => $Conf{XferMethod},
    pathHdrSrc   => $ArchiveReq{pathHdrSrc},
    pathHdrDest  => $ArchiveReq{pathHdrDest},
    HostList     => \@{$ArchiveReq{HostList}},
    BackupList   => \@{$ArchiveReq{BackupList}},
    archiveloc   => $ArchiveReq{archiveloc},
    parfile      => $ArchiveReq{parfile},
    compression  => $ArchiveReq{compression},
    compext      => $ArchiveReq{compext},
    splitsize    => $ArchiveReq{splitsize},
    pidHandler   => \&pidHandler,
};

$xfer->args($xferArgs);

if ( !defined($logMsg = $xfer->start()) ) {
    UserCommandRun("ArchivePostUserCmd") if ( $NeedPostCmd );
    $stat{hostError} = "xfer start failed: ", $xfer->errStr;
    exit(ArchiveCleanup($client));
}

print(LOG $bpc->timeStamp, "Starting archive\n");
print("started_archive\n");
$xfer->run();
$stat{xferOK} = 0 if ( defined($stat{hostError} = $xfer->errStr) );
alarm(0);

exit(ArchiveCleanup($client));

###########################################################################
# Subroutines
###########################################################################

sub catch_signal
{
    my $signame = shift;

    #
    # Children quit quietly on ALRM
    #
    exit(1) if ( $Pid != $$ && $signame eq "ALRM" );

    #
    # Ignore signals in children
    #
    return if ( $Pid != $$ );

    #
    # Note: needs to be tested for each kind of XferMethod
    #
    print(LOG $bpc->timeStamp, "cleaning up after signal $signame\n");
    $SIG{$signame} = 'IGNORE';
    $ArchiveLOG->write(\"exiting after signal $signame\n");
    $stat{xferOK} = 0;
    if ( $signame eq "INT" ) {
	$stat{hostError} = "aborted by user (signal=$signame)";
    } else {
	$stat{hostError} = "aborted by signal=$signame";
    }
    exit(ArchiveCleanup($client));
}

#
# Cleanup and update the archive status
#
sub ArchiveCleanup
{
    my($client) = @_;

    $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );

    if ( !$stat{xferOK} ) {
	#
	# Kill off the tranfer program, first nicely then forcefully.
	# We use negative PIDs to make sure all processes in each
	# group get the signal.
	#
	if ( @xferPid ) {
	    foreach my $pid ( @xferPid ) {
		kill($bpc->sigName2num("INT"), -$pid);
	    }
	    sleep(1);
	    foreach my $pid ( @xferPid ) {
		kill($bpc->sigName2num("KILL"), -$pid);
	    }
	}
    }

    my $lastNum  = -1;
    my @Archives;

    @Archives = $bpc->ArchiveInfoRead($client);
    for ( my $i = 0 ; $i < @Archives ; $i++ ) {
	$lastNum = $Archives[$i]{num} if ( $lastNum < $Archives[$i]{num} );
    }
    $lastNum++;

    #
    # Run an optional post-archive command
    #
    if ( $NeedPostCmd ) {
        UserCommandRun("ArchivePostUserCmd");
        if ( $? && $Conf{UserCmdCheckStatus} ) {
            $stat{hostError} = "RestorePreUserCmd returned error status $?";
            $stat{xferOK} = 0;
        }
    }

    rename("$Dir/ArchiveLOG$fileExt", "$Dir/ArchiveLOG.$lastNum$fileExt");
    rename("$Dir/$reqFileName", "$Dir/ArchiveInfo.$lastNum");
    my $endTime = time();

    #
    # If the archive failed, clean up
    #
    if ( !$stat{xferOK} ) {
	$stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
	$stat{hostAbort} = 1;
	$ArchiveLOG->write(\"Archive failed: $stat{hostError}")
					    if ( defined($ArchiveLOG) );
    }

    $ArchiveLOG->close() if ( defined($ArchiveLOG) );

    #
    # Add the new archive information to the archive file
    #
    @Archives = $bpc->ArchiveInfoRead($client);
    my $i = @Archives;
    $Archives[$i]{num}           = $lastNum;
    $Archives[$i]{startTime}     = $startTime;
    $Archives[$i]{endTime}       = $endTime;
    $Archives[$i]{result}        = $stat{xferOK} ? "ok" : "failed";
    $Archives[$i]{errorMsg}      = $stat{hostError};

    while ( @Archives > $Conf{ArchiveInfoKeepCnt} ) {
	my $num = $Archives[0]{num};
	unlink("$Dir/ArchiveLOG.$num.z");
	unlink("$Dir/ArchiveLOG.$num");
	unlink("$Dir/ArchiveInfo.$num");
	shift(@Archives);
    }
    $bpc->ArchiveInfoWrite($client, @Archives);

    if ( !$stat{xferOK} ) {
	print(LOG $bpc->timeStamp, "Archive failed ($stat{hostError})\n");
	print("archive failed: $stat{hostError}\n");
	return 1;
    } else {
        print(LOG $bpc->timeStamp, "Archive Complete\n");
	print("archive complete\n");
	return;
    }
}

#
# The Xfer method might tell us from time to time about processes
# it forks.  We tell BackupPC about this (for status displays) and
# keep track of the pids in case we cancel the backup
#
sub pidHandler
{
    @xferPid = @_;
    @xferPid = grep(/./, @xferPid);
    return if ( !@xferPid );
    my @pids = @xferPid;
    my $str = join(",", @pids);
    $ArchiveLOG->write(\"Xfer PIDs are now $str\n") if ( defined($ArchiveLOG) );
    print("xferPids $str\n");
}

#
# Run an optional pre- or post-dump command
#
sub UserCommandRun
{
    my($cmdType) = @_;

    return if ( !defined($Conf{$cmdType}) );
    my $vars = {
        xfer         => $xfer,
        client       => $client,
        host         => $host,
        user         => $user,
        share        => $ArchiveReq{shareDest},
        XferMethod   => $Conf{XferMethod},
        HostList     => \@{$ArchiveReq{HostList}},
        BackupList   => \@{$ArchiveReq{BackupList}},
        archiveloc   => $ArchiveReq{archiveloc},
        parfile      => $ArchiveReq{parfile},
        compression  => $ArchiveReq{compression},
        compext      => $ArchiveReq{compext},
        splitsize    => $ArchiveReq{splitsize},
        sshPath      => $Conf{SshPath},
        LOG          => *LOG,
        XferLOG      => $ArchiveLOG,
        stat         => \%stat,
        xferOK       => $stat{xferOK} || 0,
	type	     => "archive",
	cmdType	     => $cmdType,
    };
    my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars);
    $ArchiveLOG->write(\"Executing $cmdType: @$cmd\n");
    #
    # Run the user's command, dumping the stdout/stderr into the
    # Xfer log file.  Also supply the optional $vars and %Conf in
    # case the command is really perl code instead of a shell
    # command.
    #
    $bpc->cmdSystemOrEval($cmd,
	    sub {
		$ArchiveLOG->write(\$_[0]);
	    },
	    $vars, \%Conf);
}