Skip to content

Commit

Permalink
Abort external running command instead of wait for its execution duri…
Browse files Browse the repository at this point in the history
…ng abort
  • Loading branch information
Commifreak committed Mar 29, 2023
1 parent c9c6fc0 commit 3125d11
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/include/ABHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public static function backupContainer($container, $destination) {
self::backupLog("Generated tar command: " . $finalTarOptions, self::LOGLEVEL_DEBUG);
self::backupLog("Backing up " . $container['Name'] . '...');

exec("tar " . $finalTarOptions . " 2>&1", $output, $resultcode);
exec("tar " . $finalTarOptions . " 2>&1 " . ABSettings::$externalCmdPidCapture, $output, $resultcode);
self::backupLog("Tar out: " . implode('; ', $output), self::LOGLEVEL_DEBUG);

if ($resultcode > 0) {
Expand All @@ -329,7 +329,7 @@ public static function backupContainer($container, $destination) {
if ($containerSettings['verifyBackup'] == 'yes') {
self::backupLog("Verifying backup...");
self::backupLog("Final verify command: " . $finalTarVerifyOptions, self::LOGLEVEL_DEBUG);
exec("tar " . $finalTarVerifyOptions . " 2>&1", $output, $resultcode);
exec("tar " . $finalTarVerifyOptions . " 2>&1 " . ABSettings::$externalCmdPidCapture, $output, $resultcode);
self::backupLog("Tar out: " . implode('; ', $output), self::LOGLEVEL_DEBUG);

if ($resultcode > 0) {
Expand All @@ -353,16 +353,17 @@ public static function backupContainer($container, $destination) {
return true;
}

public static function scriptRunning() {
$pid = @file_get_contents(ABSettings::$tempFolder . '/' . ABSettings::$stateFileScriptRunning);
public static function scriptRunning($externalCmd = false) {
$pid = @file_get_contents(ABSettings::$tempFolder . '/' . ($externalCmd ? ABSettings::$stateExtCmd : ABSettings::$stateFileScriptRunning));
if (!$pid) {
// lockfile not there: process not running anymore
return false;
}
$pid = preg_replace("/\D/", '', $pid); // Filter any non digit characters.
if (file_exists('/proc/' . $pid)) {
return true;
return $pid;
} else {
@unlink(ABSettings::$tempFolder . '/' . ABSettings::$stateFileScriptRunning); // Remove dead state file
@unlink(ABSettings::$tempFolder . '/' . ($externalCmd ? ABSettings::$stateExtCmd : ABSettings::$stateFileScriptRunning)); // Remove dead state file
return false;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/include/ABSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class ABSettings {

public static $stateFileScriptRunning = 'running';
public static $stateFileAbort = 'abort';
public static $stateExtCmd = 'extCmd';

public static $emhttpVars = '/var/local/emhttp/var.ini';

public static $qemuFolder = '/etc/libvirt/qemu';
public static $externalCmdPidCapture = '';


public string|null $backupMethod = 'oneAfterTheOther';
Expand Down Expand Up @@ -154,4 +156,5 @@ public function checkCron() {
ABSettings::$tempFolder .= '.beta';
ABSettings::$cronFile .= '_beta';
ABSettings::$supportUrl = 'https://forums.unraid.net/topic/136995-pluginbeta-appdatabackup/';
}
}
ABSettings::$externalCmdPidCapture = '& echo $! > ' . escapeshellarg(ABSettings::$tempFolder . '/' . ABSettings::$stateExtCmd) . ' && wait $!';
7 changes: 6 additions & 1 deletion src/include/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
case 'abort':
touch(ABSettings::$tempFolder . '/' . ABSettings::$stateFileAbort);
if (ABHelper::scriptRunning()) {
ABHelper::backupLog("User want to abort - please wait until current action is done.", ABHelper::LOGLEVEL_WARN);
ABHelper::backupLog("User want to abort! Please wait!", ABHelper::LOGLEVEL_WARN);
$extCmdPid = ABHelper::scriptRunning(true);
if ($extCmdPid) {
ABHelper::backupLog("External cmd running, stopping PID " . $extCmdPid, ABHelper::LOGLEVEL_DEBUG);
exec("kill " . $extCmdPid);
}
}
break;
case 'checkRestoreSource':
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
if (!file_exists($script)) {
ABHelper::backupLog("The flash backup script is not available!", ABHelper::LOGLEVEL_ERR);
} else {
exec($script, $output);
exec($script . " " . ABSettings::$externalCmdPidCapture, $output);
ABHelper::backupLog("flash backup returned: " . implode(", ", $output), ABHelper::LOGLEVEL_DEBUG);
if (empty($output[0])) {
ABHelper::backupLog("Flash backup failed: no answer from script!", ABHelper::LOGLEVEL_ERR);
Expand Down Expand Up @@ -241,7 +241,7 @@
} else {
ABHelper::backupLog("VM meta backup enabled! Backing up...");

exec("tar -czf " . escapeshellarg($abDestination . '/vm_meta.tgz') . " " . ABSettings::$qemuFolder . '/', $output, $resultcode);
exec("tar -czf " . escapeshellarg($abDestination . '/vm_meta.tgz') . " " . ABSettings::$qemuFolder . '/ ' . ABSettings::$externalCmdPidCapture, $output, $resultcode);
ABHelper::backupLog("tar return: $resultcode and output: " . print_r($output), ABHelper::LOGLEVEL_DEBUG);
if ($resultcode != 0) {
ABHelper::backupLog("Error while backing up VM XMLs. Please see debug log!", ABHelper::LOGLEVEL_ERR);
Expand Down Expand Up @@ -326,7 +326,7 @@
ABHelper::backupLog("Generated tar command: " . $finalTarOptions, ABHelper::LOGLEVEL_DEBUG);
ABHelper::backupLog("Backing up extra files...");

exec("tar " . $finalTarOptions . " 2>&1", $output, $resultcode);
exec("tar " . $finalTarOptions . " 2>&1 " . ABSettings::$externalCmdPidCapture, $output, $resultcode);
ABHelper::backupLog("Tar out: " . implode('; ', $output), ABHelper::LOGLEVEL_DEBUG);

if ($resultcode > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
$finalTarCommand = 'tar ' . implode(' ', $tarOptions);
ABHelper::backupLog("Final tar command: " . $finalTarCommand, ABHelper::LOGLEVEL_DEBUG);

exec($finalTarCommand . " 2>&1", $output, $resultcode);
exec($finalTarCommand . " 2>&1 " . ABSettings::$externalCmdPidCapture, $output, $resultcode);
ABHelper::backupLog("Tar out: " . implode('; ', $output), ABHelper::LOGLEVEL_DEBUG);
if ($resultcode > 0) {
ABHelper::backupLog("restore failed! More output available inside debuglog, maybe.", ABHelper::LOGLEVEL_ERR);
Expand Down Expand Up @@ -150,7 +150,7 @@
$finalTarCommand = 'tar ' . implode(' ', $tarOptions);
ABHelper::backupLog("Final tar command: " . $finalTarCommand, ABHelper::LOGLEVEL_DEBUG);

exec($finalTarCommand . " 2>&1", $output, $resultcode);
exec($finalTarCommand . " 2>&1 " . ABSettings::$externalCmdPidCapture, $output, $resultcode);
ABHelper::backupLog("Tar out: " . implode('; ', $output), ABHelper::LOGLEVEL_DEBUG);
if ($resultcode > 0) {
ABHelper::backupLog("restore failed! More output available inside debuglog, maybe.", ABHelper::LOGLEVEL_ERR);
Expand All @@ -172,7 +172,7 @@
if (!file_exists(ABSettings::$qemuFolder)) {
ABHelper::backupLog("VM manager is NOT enabled! Cannot restore VM meta", ABHelper::LOGLEVEL_ERR);
} else {
exec('tar -C / -xzf ' . escapeshellarg($restoreSource . '/vm_meta.tgz'), $output, $resultcode);
exec('tar -C / -xzf ' . escapeshellarg($restoreSource . '/vm_meta.tgz') . " " . ABSettings::$externalCmdPidCapture, $output, $resultcode);
ABHelper::backupLog("tar return: $resultcode, output: " . print_r($output, true), ABHelper::LOGLEVEL_DEBUG);
if ($resultcode != 0) {
ABHelper::backupLog("restore failed, please see debug log.", ABHelper::LOGLEVEL_ERR);
Expand Down

0 comments on commit 3125d11

Please sign in to comment.