diff --git a/datamodels/2.x/itop-backup/ajax.backup.php b/datamodels/2.x/itop-backup/ajax.backup.php index d2067371d..6ad42cf2f 100644 --- a/datamodels/2.x/itop-backup/ajax.backup.php +++ b/datamodels/2.x/itop-backup/ajax.backup.php @@ -67,6 +67,12 @@ try $oPage->output(); break; + /* + * Fix a token : + * We can't load the MetaModel because in DBRestore, after restore is done we're launching a compile ! + * So as \LoginWebPage::DoLogin needs a loaded DataModel, we can't use it + * As a result we're setting a token file to make sure the restore is called by an authenticated user with the correct rights ! + */ case 'restore_get_token': require_once(APPROOT.'/application/startup.inc.php'); require_once(APPROOT.'/application/loginwebpage.class.inc.php'); @@ -99,7 +105,10 @@ EOF $oPage->output(); break; - + /* + * We can't call \LoginWebPage::DoLogin because DBRestore will do a compile after restoring the DB + * Authentication is checked with a token file (see $sOperation='restore_get_token') + */ case 'restore_exec': require_once(APPROOT."setup/runtimeenv.class.inc.php"); require_once(APPROOT.'/application/utils.inc.php'); @@ -136,15 +145,13 @@ EOF } $sFile = file_get_contents($sTokenFile); unlink($sTokenFile); - - $sMySQLBinDir = utils::ReadParam('mysql_bindir', '', false, 'raw_data'); - $sDBHost = utils::ReadParam('db_host', '', false, 'raw_data'); - $sDBUser = utils::ReadParam('db_user', '', false, 'raw_data'); - $sDBPwd = utils::ReadParam('db_pwd', '', false, 'raw_data'); - $sDBName = utils::ReadParam('db_name', '', false, 'raw_data'); - $sDBSubName = utils::ReadParam('db_subname', '', false, 'raw_data'); - - $oDBRS = new DBRestore($sDBHost, $sDBUser, $sDBPwd, $sDBName, $sDBSubName); + + // Loading config file : we don't have the MetaModel but we have the current env ! + $sConfigFilePath = utils::GetConfigFilePath($sEnvironment); + $oItopConfig = new Config($sConfigFilePath, true); + $sMySQLBinDir = $oItopConfig->GetModuleSetting('itop-backup', 'mysql_bindir', ''); + + $oDBRS = new DBRestore($oItopConfig); $oDBRS->SetMySQLBinDir($sMySQLBinDir); $sBackupDir = APPROOT.'data/backups/'; diff --git a/datamodels/2.x/itop-backup/dbrestore.class.inc.php b/datamodels/2.x/itop-backup/dbrestore.class.inc.php index d67f551f5..1fb84103a 100644 --- a/datamodels/2.x/itop-backup/dbrestore.class.inc.php +++ b/datamodels/2.x/itop-backup/dbrestore.class.inc.php @@ -19,6 +19,19 @@ class DBRestore extends DBBackup { + /** @var string */ + private $sDBPwd; + /** @var string */ + private $sDBUser; + + public function __construct(\Config $oConfig = null) + { + parent::__construct($oConfig); + + $this->sDBUser = $oConfig->Get('db_user'); + $this->sDBPwd = $oConfig->Get('db_pwd'); + } + protected function LogInfo($sMsg) { //IssueLog::Info('non juste info: '.$sMsg); @@ -99,8 +112,14 @@ class DBRestore extends DBBackup } /** - * @param $sFile A file with the extension .zip or .tar.gz + * Warning : can't be called with a loaded DataModel as we're compiling after restore + * + * @param string $sFile A file with the extension .zip or .tar.gz * @param string $sEnvironment Target environment + * + * @throws \BackupException + * + * @uses \RunTimeEnvironment::CompileFrom() */ public function RestoreFromCompressedBackup($sFile, $sEnvironment = 'production') { @@ -111,7 +130,7 @@ class DBRestore extends DBBackup { $this->LogInfo('zip file detected'); $oArchive = new ZipArchiveEx(); - $res = $oArchive->open($sFile); + $oArchive->open($sFile); } elseif (substr($sNormalizedFile, -7) == '.tar.gz') { @@ -120,7 +139,7 @@ class DBRestore extends DBBackup } else { - throw new Exception('Unsupported format for a backup file: '.$sFile); + throw new BackupException('Unsupported format for a backup file: '.$sFile); } // Load the database @@ -131,7 +150,14 @@ class DBRestore extends DBBackup $oArchive->extractFileTo($sDataDir, 'itop-dump.sql'); $sDataFile = $sDataDir.'/itop-dump.sql'; $this->LoadDatabase($sDataFile); - SetupUtils::rrmdir($sDataDir); + try + { + SetupUtils::rrmdir($sDataDir); + } + catch (Exception $e) + { + throw new BackupException("Can't remove data dir", 0, $e); + } // Update the code // @@ -147,7 +173,14 @@ class DBRestore extends DBBackup } if (is_dir(APPROOT.'data/production-modules/')) { - SetupUtils::rrmdir(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 ($oArchive->hasDir('production-modules/') !== false) { diff --git a/datamodels/2.x/itop-backup/status.php b/datamodels/2.x/itop-backup/status.php index 44493467c..58c3eec6c 100644 --- a/datamodels/2.x/itop-backup/status.php +++ b/datamodels/2.x/itop-backup/status.php @@ -352,14 +352,8 @@ function LaunchRestoreNow(sBackupFile, sConfirmationMessage) var oParams = {}; oParams.operation = 'restore_exec'; - oParams.token = $("#restore_token").val(); - oParams.mysql_bindir = '$sMySQLBinDir'; - oParams.db_host = '$sDBHost'; - oParams.db_user = '$sDBUser'; - oParams.db_pwd = '$sDBPwd'; - oParams.db_name = '$sDBName'; - oParams.db_subname = '$sDBSubName'; - oParams.environment = '$sEnvironment'; + oParams.token = $("#restore_token").val(); // token to check auth + rights without loading MetaModel + oParams.environment = '$sEnvironment'; // needed to load the config if (oParams.token.length > 0) { $.post(GetAbsoluteUrlModulePage('itop-backup', 'ajax.backup.php'), oParams, function(data){