From 5702f603acf39b00a46329bd93f23940de42abed Mon Sep 17 00:00:00 2001 From: odain Date: Thu, 12 Aug 2021 14:13:45 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B04227=20-=20Manual=20iTop=20restore=20-?= =?UTF-8?q?=20move=20restore=20mutex=20management=20inside=20in=20DBRestor?= =?UTF-8?q?e->RestoreFromCompressedBackup=20primitive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datamodels/2.x/itop-backup/ajax.backup.php | 5 - .../2.x/itop-backup/dbrestore.class.inc.php | 128 +++++++++--------- 2 files changed, 63 insertions(+), 70 deletions(-) diff --git a/datamodels/2.x/itop-backup/ajax.backup.php b/datamodels/2.x/itop-backup/ajax.backup.php index f94b3d703..a43094a78 100644 --- a/datamodels/2.x/itop-backup/ajax.backup.php +++ b/datamodels/2.x/itop-backup/ajax.backup.php @@ -170,10 +170,6 @@ JS require_once(dirname(__FILE__).'/dbrestore.class.inc.php'); $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data'); - $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment); - IssueLog::Info("Backup Restore - Acquiring the LOCK 'restore.$sEnvironment'"); - $oRestoreMutex->Lock(); - IssueLog::Info('Backup Restore - LOCK acquired, executing...'); try { set_time_limit(0); @@ -203,7 +199,6 @@ JS finally { unlink($tokenRealPath); - $oRestoreMutex->Unlock(); } $oPage->output(); diff --git a/datamodels/2.x/itop-backup/dbrestore.class.inc.php b/datamodels/2.x/itop-backup/dbrestore.class.inc.php index c591412a4..3410a892e 100644 --- a/datamodels/2.x/itop-backup/dbrestore.class.inc.php +++ b/datamodels/2.x/itop-backup/dbrestore.class.inc.php @@ -127,79 +127,77 @@ class DBRestore extends DBBackup */ public function RestoreFromCompressedBackup($sFile, $sEnvironment = 'production') { - $this->LogInfo("Starting restore of ".basename($sFile)); + $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment); + IssueLog::Info("Backup Restore - Acquiring the LOCK 'restore.$sEnvironment'"); + $oRestoreMutex->Lock(); - $sNormalizedFile = strtolower(basename($sFile)); - if (substr($sNormalizedFile, -4) == '.zip') - { - $this->LogInfo('zip file detected'); - $oArchive = new ZipArchiveEx(); - $oArchive->open($sFile); - } - elseif (substr($sNormalizedFile, -7) == '.tar.gz') - { - $this->LogInfo('tar.gz file detected'); - $oArchive = new TarGzArchive($sFile); - } - else - { - throw new BackupException('Unsupported format for a backup file: '.$sFile); - } + try { + IssueLog::Info('Backup Restore - LOCK acquired, executing...'); - // Load the database - // - $sDataDir = APPROOT.'data/tmp-backup-'.rand(10000, getrandmax()); + $this->LogInfo("Starting restore of ".basename($sFile)); - SetupUtils::builddir($sDataDir); // Here is the directory - $oArchive->extractTo($sDataDir); - $sDataFile = $sDataDir.'/itop-dump.sql'; - $this->LoadDatabase($sDataFile); - - // Update the code - // - $sDeltaFile = APPROOT.'data/'.$sEnvironment.'.delta.xml'; - - if (is_file($sDataDir.'/delta.xml')) - { - // Extract and rename delta.xml => .delta.xml; - rename($sDataDir.'/delta.xml', $sDeltaFile); - } - else - { - @unlink($sDeltaFile); - } - if (is_dir(APPROOT.'data/production-modules/')) - { - try - { - SetupUtils::rrmdir(APPROOT.'data/production-modules/'); + $sNormalizedFile = strtolower(basename($sFile)); + if (substr($sNormalizedFile, -4) == '.zip') { + $this->LogInfo('zip file detected'); + $oArchive = new ZipArchiveEx(); + $oArchive->open($sFile); + } elseif (substr($sNormalizedFile, -7) == '.tar.gz') { + $this->LogInfo('tar.gz file detected'); + $oArchive = new TarGzArchive($sFile); + } else { + throw new BackupException('Unsupported format for a backup file: '.$sFile); } - catch (Exception $e) - { - throw new BackupException("Can't remove production-modules dir", 0, $e); + + // Load the database + // + $sDataDir = APPROOT.'data/tmp-backup-'.rand(10000, getrandmax()); + + SetupUtils::builddir($sDataDir); // Here is the directory + $oArchive->extractTo($sDataDir); + + $sDataFile = $sDataDir.'/itop-dump.sql'; + $this->LoadDatabase($sDataFile); + + // Update the code + // + $sDeltaFile = APPROOT.'data/'.$sEnvironment.'.delta.xml'; + + if (is_file($sDataDir.'/delta.xml')) { + // Extract and rename delta.xml => .delta.xml; + rename($sDataDir.'/delta.xml', $sDeltaFile); + } else { + @unlink($sDeltaFile); + } + if (is_dir(APPROOT.'data/production-modules/')) { + try { + SetupUtils::rrmdir(APPROOT.'data/production-modules/'); + } catch (Exception $e) { + throw new BackupException("Can't remove production-modules dir", 0, $e); + } + } + if (is_dir($sDataDir.'/production-modules')) { + rename($sDataDir.'/production-modules', APPROOT.'data/production-modules/'); } - } - if (is_dir($sDataDir.'/production-modules')) - { - rename($sDataDir.'/production-modules', APPROOT.'data/production-modules/'); - } - $sConfigFile = APPROOT.'conf/'.$sEnvironment.'/config-itop.php'; - @chmod($sConfigFile, 0770); // Allow overwriting the file - rename($sDataDir.'/config-itop.php', $sConfigFile); - @chmod($sConfigFile, 0440); // Read-only + $sConfigFile = APPROOT.'conf/'.$sEnvironment.'/config-itop.php'; + @chmod($sConfigFile, 0770); // Allow overwriting the file + rename($sDataDir.'/config-itop.php', $sConfigFile); + @chmod($sConfigFile, 0440); // Read-only - try - { - SetupUtils::rrmdir($sDataDir); - } - catch (Exception $e) - { - throw new BackupException("Can't remove data dir", 0, $e); - } + try { + SetupUtils::rrmdir($sDataDir); + } catch (Exception $e) { + throw new BackupException("Can't remove data dir", 0, $e); + } - $oEnvironment = new RunTimeEnvironment($sEnvironment); - $oEnvironment->CompileFrom($sEnvironment); + $oEnvironment = new RunTimeEnvironment($sEnvironment); + $oEnvironment->CompileFrom($sEnvironment); + } + finally + { + IssueLog::Info('Backup Restore - LOCK released.'); + $oRestoreMutex->Unlock(); + } } }