my $hnlistFile = "/etc/backuppc/".basename($0).".hnlist";
my $velistFile = $ENV{HOME}."/log/".basename($0).".velist";
-# Uncomment one of the following
+# Write to the debug file only if it is already present
+my $dbgfn = "/tmp/BackupPC_ovz.debug";
my $dbgf;
-open $dbgf, ">>/tmp/BackupPC_ovz.debug" || die "Cannot open debug file";
-#open $dbgf, ">>/dev/null" || die "Cannot open debug file";
+if ( ! -f $dbgfn) {
+ $dbgfn = "/dev/null";
+}
+open $dbgf, ">>$dbgfn" || die "Cannot open debug file $dbgfn";
sub mydie($)
{
print(STDERR "Perl code fragment for exec shouldn't return!!\n");
exit(1);
} else {
- $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
+ $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
+ print $dbgf ": execing command $cmd\n";
alarm(0);
$cmd = [map { m/(.*)/ } @$cmd]; # untaint
#
$? = 0;
$cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
+ print $dbgf ": evaluating command $cmd\n";
eval($cmd);
} else {
+ print $dbgf ": running command $cmd\n";
system($cmd);
}
}
# Write the VEs on all HNs to a config file on the BackupPC server for
# later use.
my @HNS = ();
+
+ print $dbgf ": refreshConfig\n";
open my $cfg, "<$hnlistFile" || mydie "Cannot read $hnlistFile";
while (<$cfg>) {
chomp;
sub delSnapshot($)
{
my ($ve) = @_;
+ print $dbgf ": delSnapshot\n";
mydie "No VE record for delSnapshot" if (!defined($ve));
#print "delSnapshot: doing nothing for now\n";
my $dir = $ve->{'snaproot'};
if (defined($dir) && -d $dir) {
#cmdSystemOrEval("rm -rf $dir/etc/vzdump");
+ print $dbgf ": delSnapshot snapshot mounted at $dir\n";
cmdSystemOrEval("umount $dir");
cmdSystemOrEval("rmdir $dir");
}
my $dev = $ve->{'snapdev'};
- cmdSystemOrEval("lvremove -f $dev >/dev/null 2>&1") if (-b $dev);
+ #cmdSystemOrEval("lvremove -f $dev >/dev/null 2>&1") if (-b $dev);
+ if (-b $dev) {
+ print $dbgf ": removing LV $dev\n";
+ system("lvremove -f $dev >>$dbgfn 2>&1")
+ } else {
+ print $dbgf ": lvremove: $dev is not block special\n";
+ }
}
sub getDevice($)
sub checkRunningClient()
{
+ print $dbgf ": checkRunningClient, vzsnap=$vzsnap\n";
mydie "A backup or restore operation are already in progress"
if (Proc::PID::File->running({ dir => '/tmp', verify => 1 }));
# Clean up any prior backup's mount point and snapshot, if it exists.
# Note that the snapshot is small, so we don't really want it lying around!
+ # There are cases where the snapshot will not show as a snapshot, and in
+ # these cases the LV will not be removed by this function.
my $vg = undef;
open my $fh, "lvscan|" || mydie "Unable to exec lvscan";
while (my $line = <$fh>) {
+ #print $dbgf ": checkRunningClient lvscan line=$line\n";
if ($line =~ m|^\s+ACTIVE\s+Snapshot\s+\'/dev/([^/]+)/$vzsnap\'\s|) {
$vg = $1;
+ #print $dbgf ": checkRunningClient vg=$vg\n";
}
}
close($fh);
if (defined($vg)) {
my $dev = "/dev/mapper/$vg-$vzsnap";
- #print "Found vzsnap lv $dev\n";
+ print $dbgf ": found vzsnap lv $dev\n";
cmdSystemOrEval("umount /$vzsnap");
cmdSystemOrEval("rmdir /$vzsnap") if (-d "/$vzsnap");
- cmdSystemOrEval("lvremove -f $dev >/dev/null 2>&1") if (-b $dev);
+ #cmdSystemOrEval("lvremove -f $dev >/dev/null 2>&1") if (-b $dev);
+ system("lvremove -f $dev >>$dbgfn 2>&1") if (-b $dev);
+ }
+}
+
+sub runPing()
+{
+ # This command generates ping output by pinging the host specified in the
+ # ping command still within @ARGV. However, if the host listed therein is
+ # a VE, the HN must be pinged instead.
+
+ # Get the host
+ my $host = shift(@ARGV);
+ my $cmd = join(' ', @ARGV);
+
+ print $dbgf ": runPing host=$host, cmd=$cmd\n";
+ # Find $host in the list of VEs
+ loadVeList();
+ my $hostname = gethostbyaddr(gethostbyname($host), AF_INET);
+ mydie "Host $host not found" if (!defined($hostname));
+ my $ve = getVeEntry('hostname', $hostname);
+ if (defined($ve)) {
+ $hostname = $ve->{'HN'};
+ mydie "HN is undefined for host $host" if (!defined($hostname));
+ $cmd =~ s/$host/$hostname/g;
+ print $dbgf ": ping request for $host remapped to HN $hostname\n";
}
+
+ cmdExecOrEval($cmd);
}
sub runClient($)
{
my ($restore) = @_;
+ print $dbgf ": runClient restore=$restore\n";
checkRunningClient();
my $veid = shift(@ARGV);
# Remove snapshot, we're done
delSnapshot($ve);
+ print $dbgf ": runClient complete\n";
+
# Pass the return code back
#exit $ret; FIXME: currently, cmdSystemOrEval doesn't return a retcode.
exit 0;
sub runServer($)
{
my ($restore) = @_;
-
my $host = shift(@ARGV);
+
+ print $dbgf ": runServer restore=$restore, host=$host\n";
mydie "Hostname argument required" if (!defined($host));
mydie "No command to execute after hostname" if ($#ARGV < 0);
#print "Restore mode\n";
}
+my $ping = 0;
+if ($ARGV[0] eq "ping") {
+ shift(@ARGV);
+ $ping = 1;
+ #print "Ping mode\n";
+}
+
+print $dbgf ": server=$server, refresh=$refresh, restore=$restore ping=$ping";
+print $dbgf " on ".`date`;
if ($server) {
runServer($restore);
} elsif ($refresh) {
refreshConfig();
+} elsif ($ping) {
+ runPing();
} else {
runClient($restore);
}