Backup/restore : apply COmbodo formatting

SVN:trunk[5667]
This commit is contained in:
Pierre Goiffon
2018-04-13 14:49:27 +00:00
parent d5b145e052
commit 9d5ab75dbd
3 changed files with 161 additions and 131 deletions

View File

@@ -35,37 +35,37 @@ try
{
$sOperation = utils::ReadParam('operation', '');
switch($sOperation)
switch ($sOperation)
{
case 'backup':
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
$oPage = new ajax_page("");
$oPage->no_cache();
$oPage->SetContentType('text/html');
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
$oPage = new ajax_page("");
$oPage->no_cache();
$oPage->SetContentType('text/html');
if (utils::GetConfig()->Get('demo_mode'))
{
$oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>");
}
else
{
try
if (utils::GetConfig()->Get('demo_mode'))
{
set_time_limit(0);
$oBB = new BackupExec(APPROOT.'data/backups/manual/', 0 /*iRetentionCount*/);
$sRes = $oBB->Process(time() + 36000); // 10 hours to complete should be sufficient!
$oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>");
}
catch (Exception $e)
else
{
$oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage());
try
{
set_time_limit(0);
$oBB = new BackupExec(APPROOT.'data/backups/manual/', 0 /*iRetentionCount*/);
$sRes = $oBB->Process(time() + 36000); // 10 hours to complete should be sufficient!
}
catch (Exception $e)
{
$oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage());
}
}
}
$oPage->output();
break;
$oPage->output();
break;
/*
* Fix a token :
@@ -74,118 +74,118 @@ try
* 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');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
$oPage = new ajax_page("");
$oPage->no_cache();
$oPage->SetContentType('text/html');
$oPage = new ajax_page("");
$oPage->no_cache();
$oPage->SetContentType('text/html');
$sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
$oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
if (!$oRestoreMutex->IsLocked())
{
$sFile = utils::ReadParam('file', '', false, 'raw_data');
$sToken = str_replace(' ', '', (string)microtime());
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
file_put_contents($sTokenFile, $sFile);
$oPage->add_ready_script(
<<<EOF
$sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
$oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
if (!$oRestoreMutex->IsLocked())
{
$sFile = utils::ReadParam('file', '', false, 'raw_data');
$sToken = str_replace(' ', '', (string)microtime());
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
file_put_contents($sTokenFile, $sFile);
$oPage->add_ready_script(
<<<EOF
$("#restore_token").val('$sToken');
EOF
);
}
else
{
$oPage->p(Dict::S('bkp-restore-running'));
}
$oPage->output();
break;
);
}
else
{
$oPage->p(Dict::S('bkp-restore-running'));
}
$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');
require_once(APPROOT.'/setup/backup.class.inc.php');
require_once(dirname(__FILE__).'/dbrestore.class.inc.php');
require_once(APPROOT."setup/runtimeenv.class.inc.php");
require_once(APPROOT.'/application/utils.inc.php');
require_once(APPROOT.'/setup/backup.class.inc.php');
require_once(dirname(__FILE__).'/dbrestore.class.inc.php');
IssueLog::Enable(APPROOT.'log/error.log');
IssueLog::Enable(APPROOT.'log/error.log');
$oPage = new ajax_page("");
$oPage->no_cache();
$oPage->SetContentType('text/html');
$oPage = new ajax_page("");
$oPage->no_cache();
$oPage->SetContentType('text/html');
if (utils::GetConfig()->Get('demo_mode'))
{
$oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>");
}
else
{
$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
if (utils::GetConfig()->Get('demo_mode'))
{
set_time_limit(0);
// Get the file and destroy the token (single usage)
$sToken = utils::ReadParam('token', '', false, 'raw_data');
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
if (!is_file($sTokenFile))
$oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>");
}
else
{
$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
{
throw new Exception("Error: missing token file: '$sTokenFile'");
set_time_limit(0);
// Get the file and destroy the token (single usage)
$sToken = utils::ReadParam('token', '', false, 'raw_data');
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
if (!is_file($sTokenFile))
{
throw new Exception("Error: missing token file: '$sTokenFile'");
}
$sFile = file_get_contents($sTokenFile);
unlink($sTokenFile);
// 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/';
$sBackupFile = $sBackupDir.$sFile;
$sRes = $oDBRS->RestoreFromCompressedBackup($sBackupFile, $sEnvironment);
IssueLog::Info('Backup Restore - Done, releasing the LOCK');
$oRestoreMutex->Unlock();
}
catch (Exception $e)
{
$oRestoreMutex->Unlock();
$oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage());
}
$sFile = file_get_contents($sTokenFile);
unlink($sTokenFile);
// 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/';
$sBackupFile = $sBackupDir.$sFile;
$sRes = $oDBRS->RestoreFromCompressedBackup($sBackupFile, $sEnvironment);
IssueLog::Info('Backup Restore - Done, releasing the LOCK');
$oRestoreMutex->Unlock();
}
catch (Exception $e)
{
$oRestoreMutex->Unlock();
$oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage());
}
}
$oPage->output();
break;
$oPage->output();
break;
case 'download':
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
if (utils::GetConfig()->Get('demo_mode'))
{
throw new Exception('iTop is in demonstration mode: the feature is disabled');
}
$sFile = utils::ReadParam('file', '', false, 'raw_data');
$oBackup = new DBBackupScheduled();
$sBackupDir = APPROOT.'data/backups/';
$oBackup->DownloadBackup($sBackupDir.$sFile);
break;
if (utils::GetConfig()->Get('demo_mode'))
{
throw new Exception('iTop is in demonstration mode: the feature is disabled');
}
$sFile = utils::ReadParam('file', '', false, 'raw_data');
$oBackup = new DBBackupScheduled();
$sBackupDir = APPROOT.'data/backups/';
$oBackup->DownloadBackup($sBackupDir.$sFile);
break;
}
}
catch (Exception $e)

View File

@@ -36,6 +36,7 @@ class DBRestore extends DBBackup
{
//IssueLog::Info('non juste info: '.$sMsg);
}
protected function LogError($sMsg)
{
IssueLog::Error($sMsg);
@@ -78,20 +79,20 @@ class DBRestore extends DBBackup
$aOutput = array();
$iRetCode = 0;
exec($sCommand, $aOutput, $iRetCode);
foreach($aOutput as $sLine)
foreach ($aOutput as $sLine)
{
$this->LogInfo("mysql said: $sLine");
}
if ($iRetCode != 0)
{
$this->LogError("Failed to execute: $sCommandDisplay. The command returned:$iRetCode");
foreach($aOutput as $sLine)
foreach ($aOutput as $sLine)
{
$this->LogError("mysql said: $sLine");
}
if (count($aOutput) == 1)
if (count($aOutput) == 1)
{
$sMoreInfo = trim($aOutput[0]);
$sMoreInfo = trim($aOutput[0]);
}
else
{
@@ -103,6 +104,7 @@ class DBRestore extends DBBackup
/**
* @deprecated Use RestoreFromCompressedBackup instead
*
* @param $sZipFile
* @param string $sEnvironment
*/

View File

@@ -22,12 +22,14 @@ interface BackupArchive
{
/**
* @param string $sFile
*
* @return bool <b>TRUE</b> if the file is present, <b>FALSE</b> otherwise.
*/
public function hasFile($sFile);
/**
* @param string $sDirectory
*
* @return bool <b>TRUE</b> if the directory is present, <b>FALSE</b> otherwise.
*/
public function hasDir($sDirectory);
@@ -35,6 +37,7 @@ interface BackupArchive
/**
* @param string $sDestinationDir
* @param string $sArchiveFile
*
* @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
*/
public function extractFileTo($sDestinationDir, $sArchiveFile);
@@ -42,17 +45,22 @@ interface BackupArchive
/**
* Extract a whole directory from the archive.
* Usage: $oArchive->extractDirTo('/var/www/html/itop/data', '/production-modules/')
*
* @param string $sDestinationDir
* @param string $sArchiveDir Note: must start and end with a slash !!
*
* @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
*/
public function extractDirTo($sDestinationDir, $sArchiveDir);
/**
* Returns the entry contents using its name
*
* @param string $name Name of the entry
* @param int $length [optional] The length to be read from the entry. If 0, then the entire entry is read.
* @param int $flags [optional] The flags to use to open the archive. the following values may be ORed to it. <b>ZipArchive::FL_UNCHANGED</b>
* @param int $flags [optional] The flags to use to open the archive. the following values may be ORed to it.
* <b>ZipArchive::FL_UNCHANGED</b>
*
* @return string the contents of the entry on success or <b>FALSE</b> on failure.
*/
public function getFromName($name, $length = 0, $flags = null);
@@ -102,6 +110,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
/**
* @param string $sFile
*
* @return bool <b>TRUE</b> if the file is present, <b>FALSE</b> otherwise.
*/
public function hasFile($sFile)
@@ -111,6 +120,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
/**
* @param string $sDirectory
*
* @return bool <b>TRUE</b> if the directory is present, <b>FALSE</b> otherwise.
*/
public function hasDir($sDirectory)
@@ -121,6 +131,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
/**
* @param string $sDestinationDir
* @param string $sArchiveFile
*
* @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
*/
public function extractFileTo($sDestinationDir, $sArchiveFile)
@@ -131,14 +142,16 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
/**
* Extract a whole directory from the archive.
* Usage: $oZip->extractDirTo('/var/www/html/itop/data', '/production-modules/')
*
* @param string $sDestinationDir
* @param string $sZipDir Must start and end with a slash !!
*
* @return boolean
*/
public function extractDirTo($sDestinationDir, $sZipDir)
{
$aFiles = array();
for($i = 0; $i < $this->numFiles; $i++)
for ($i = 0; $i < $this->numFiles; $i++)
{
$sEntry = $this->getNameIndex($i);
//Use strpos() to check if the entry name contains the directory we want to extract
@@ -153,6 +166,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
{
return true;
}
return false;
}
} // class ZipArchiveEx
@@ -220,6 +234,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
/**
* Create a normalized backup name, depending on the current date/time and Database
*
* @param sNameSpec string Name and path, eventually containing itop placeholders + time formatting specs
*/
public function SetMySQLBinDir($sMySQLBinDir)
@@ -229,6 +244,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
/**
* Create a normalized backup name, depending on the current date/time and Database
*
* @param string sNameSpec Name and path, eventually containing itop placeholders + time formatting specs
*/
public function MakeName($sNameSpec = "__DB__-%Y-%m-%d")
@@ -239,6 +255,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
$sFileName = str_replace('__SUBNAME__', $this->sDBSubName, $sFileName);
// Transform %Y, etc.
$sFileName = strftime($sFileName);
return $sFileName;
}
@@ -264,7 +281,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
'dest' => 'itop-dump.sql',
);
foreach($this->GetAdditionalFiles($sSourceConfigFile) as $sArchiveFile => $sSourceFile)
foreach ($this->GetAdditionalFiles($sSourceConfigFile) as $sArchiveFile => $sSourceFile)
{
$aContents[] = array(
'source' => $sSourceFile,
@@ -345,6 +362,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
$sDataFile = $sTmpFolder.'/itop-dump.sql';
$this->DoBackup($sDataFile);
$aRet[] = $sDataFile;
return $aRet;
}
@@ -384,7 +402,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
throw new BackupException("No table has been found with the given prefix");
}
$aEscapedTables = array();
foreach($aTables as $sTable)
foreach ($aTables as $sTable)
{
$aEscapedTables[] = self::EscapeShellArg($sTable);
}
@@ -418,7 +436,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
$aOutput = array();
$iRetCode = 0;
exec($sCommand, $aOutput, $iRetCode);
foreach($aOutput as $sLine)
foreach ($aOutput as $sLine)
{
$this->LogInfo("mysqldump said: $sLine");
}
@@ -431,7 +449,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
}
$this->LogError("Failed to execute: $sCommandDisplay. The command returned:$iRetCode");
foreach($aOutput as $sLine)
foreach ($aOutput as $sLine)
{
$this->LogError("mysqldump said: $sLine");
}
@@ -457,7 +475,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
*/
protected function DoZip($aFiles, $sZipArchiveFile)
{
foreach($aFiles as $aFile)
foreach ($aFiles as $aFile)
{
$sFile = $aFile['source'];
if (!is_file($sFile) && !is_dir($sFile))
@@ -471,9 +489,9 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
$oZip = new ZipArchiveEx();
$res = $oZip->open($sZipArchiveFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($res === TRUE)
if ($res === true)
{
foreach($aFiles as $aFile)
foreach ($aFiles as $aFile)
{
if (is_dir($aFile['source']))
{
@@ -588,6 +606,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
{
$aTables[] = $aRow[0];
}
return $aTables;
}
@@ -695,6 +714,7 @@ class TarGzArchive implements BackupArchive
/**
* @param string $sFile
*
* @return bool <b>TRUE</b> if the file is present, <b>FALSE</b> otherwise.
*/
public function hasFile($sFile)
@@ -706,18 +726,20 @@ class TarGzArchive implements BackupArchive
// Initial load
$this->buildFileList();
}
foreach($this->aFiles as $aArchFile)
foreach ($this->aFiles as $aArchFile)
{
if ($aArchFile['filename'] == $sFile)
{
return true;
}
}
return false;
}
/**
* @param string $sDirectory
*
* @return bool <b>TRUE</b> if the directory is present, <b>FALSE</b> otherwise.
*/
public function hasDir($sDirectory)
@@ -729,19 +751,21 @@ class TarGzArchive implements BackupArchive
// Initial load
$this->buildFileList();
}
foreach($this->aFiles as $aArchFile)
foreach ($this->aFiles as $aArchFile)
{
if (($aArchFile['typeflag'] == 5) && ($aArchFile['filename'] == $sDirectory))
{
return true;
}
}
return false;
}
/**
* @param string $sDestinationDir
* @param string $sArchiveFile
*
* @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
*/
public function extractFileTo($sDestinationDir, $sArchiveFile)
@@ -752,8 +776,10 @@ class TarGzArchive implements BackupArchive
/**
* Extract a whole directory from the archive.
* Usage: $oArchive->extractDirTo('/var/www/html/itop/data', '/production-modules/')
*
* @param string $sDestinationDir
* @param string $sArchiveDir
*
* @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
*/
public function extractDirTo($sDestinationDir, $sArchiveDir)
@@ -763,9 +789,11 @@ class TarGzArchive implements BackupArchive
/**
* Returns the entry contents using its name
*
* @param string $name Name of the entry
* @param int $length unused.
* @param int $flags unused.
*
* @return string the contents of the entry on success or <b>FALSE</b> on failure.
*/
public function getFromName($name, $length = 0, $flags = null)