N°1260 fix DB restore regression

* add comments to explain use of the token file
* only pass current env to the ajax call (it is enough to load the corresponding config file and get everything we need !)
* DBRestore : initialize user & pwd as needed
* DBRestore : do not throw Exception anymore but only BackupException

SVN:trunk[5659]
This commit is contained in:
Pierre Goiffon
2018-04-13 09:43:03 +00:00
parent 12093c311c
commit de54575e04
3 changed files with 57 additions and 23 deletions

View File

@@ -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/';

View File

@@ -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
* <strong>Warning</strong> : 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)
{

View File

@@ -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){