From 00d65aeb45ffe56bf4931a56ae5805b8c5a99ce4 Mon Sep 17 00:00:00 2001 From: odain Date: Thu, 12 Aug 2021 14:14:05 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B04227=20-=20Manual=20iTop=20restore=20-?= =?UTF-8?q?=20create=20manual=20CLI=20restore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datamodels/2.x/itop-backup/restore.php | 212 +++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 datamodels/2.x/itop-backup/restore.php diff --git a/datamodels/2.x/itop-backup/restore.php b/datamodels/2.x/itop-backup/restore.php new file mode 100644 index 0000000000..59a0baf1c6 --- /dev/null +++ b/datamodels/2.x/itop-backup/restore.php @@ -0,0 +1,212 @@ +oPage->p($sMsg); + } + + protected function LogError($sMsg) + { + $this->oPage->p('Error: '.$sMsg); + ToolsLog::Error($sMsg); + } + + public function __construct($oPage) + { + $this->oPage = $oPage; + parent::__construct(); + } +} + + +/** + * Checks if a parameter (possibly empty) was specified when calling this page + */ +function CheckParam($sParamName) +{ + global $argv; + + if (isset($_REQUEST[$sParamName])) return true; // HTTP parameter either GET or POST + if (!is_array($argv)) return false; + foreach($argv as $sArg) + { + if ($sArg == '--'.$sParamName) return true; // Empty command line parameter, long unix style + if ($sArg == $sParamName) return true; // Empty command line parameter, Windows style + if ($sArg == '-'.$sParamName) return true; // Empty command line parameter, short unix style + if (preg_match('/^--'.$sParamName.'=(.*)$/', $sArg, $aMatches)) return true; // Command parameter with a value + } + return false; +} + +function Usage($oP) +{ + $oP->p('Restore an iTop from a backup file'); + $oP->p('Parameters:'); + if (utils::IsModeCLI()) + { + $oP->p('auth_user: login, must be administrator'); + $oP->p('auth_pwd: ...'); + } + $oP->p('backup_file [optional]: name of the file to store the backup into. Follows the PHP strftime format spec. The following placeholders are available: __HOST__, __DB__, __SUBNAME__'); + $oP->p('mysql_bindir [optional]: specify the path for mysql executable'); + + if (utils::IsModeCLI()) + { + $oP->p('Example: php -q restore.php --auth_user=admin --auth_pwd=myPassw0rd --backup_file=/tmp/backup.zip'); + $oP->p('Known limitation: the current directory must be the directory of backup.php'); + } + else + { + $oP->p('Example: .../restore.php?backup_file=/tmp/backup.zip'); + } +} + +function ExitError($oP, $sMessage) +{ + ToolsLog::Error($sMessage); + $oP->p($sMessage); + $oP->output(); + exit; +} + + +function ReadMandatoryParam($oP, $sParam) +{ + $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */, 'raw_data'); + if (is_null($sValue)) + { + ExitError($oP, "ERROR: Missing argument '$sParam'"); + } + return trim($sValue); +} + + +///////////////////////////////// +// Main program + +set_time_limit(0); + +if (utils::IsModeCLI()) +{ + $oP = new CLIPage("iTop - iTop Restore"); + + SetupUtils::CheckPhpAndExtensionsForCli($oP); +} +else +{ + $oP = new WebPage("iTop - iTop Restore"); +} + +try +{ + utils::UseParamFile(); +} +catch(Exception $e) +{ + ExitError($oP, $e->GetMessage()); +} + +if (utils::IsModeCLI()) +{ + $oP->p(date('Y-m-d H:i:s')." - running backup utility"); + $sAuthUser = ReadMandatoryParam($oP, 'auth_user'); + $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd'); + $sBackupFile = ReadMandatoryParam($oP, 'backup_file'); + $bDownloadBackup = false; + if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) + { + UserRights::Login($sAuthUser); // Login & set the user's language + } + else + { + ExitError($oP, "Access restricted or wrong credentials ('$sAuthUser')"); + } + + if (!is_file($sBackupFile) && is_readable($sBackupFile)){ + ExitError($oP, "Cannot access backup file ('$sBackupFile')"); + } +} +else +{ + require_once(APPROOT.'application/loginwebpage.class.inc.php'); + LoginWebPage::DoLogin(); // Check user rights and prompt if needed +} + +if (!UserRights::IsAdministrator()) +{ + ExitError($oP, "Access restricted to administors"); +} + +if (CheckParam('?') || CheckParam('h') || CheckParam('help')) +{ + Usage($oP); + $oP->output(); + exit; +} + + +// Interpret strftime specifications (like %Y) and database placeholders +$oRestore = new MyDBRestore($oP); +$oRestore->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', '')); + +$res = false; +if (MetaModel::GetConfig()->Get('demo_mode')) +{ + $oP->p("Sorry, iTop is in demonstration mode: the feature is disabled"); +} +else +{ + $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data'); + SetupUtils::EnterReadOnlyMode(MetaModel::GetConfig()); + try{ + $oRestore->RestoreFromCompressedBackup($sBackupFile, $sEnvironment); + } finally { + SetupUtils::ExitReadOnlyMode(); + } +} + +$oP->output();