mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 10:38:45 +02:00
N°4225/4227: recommit lost restore.php + refactor duplicated code for cli restore/backup in common-cli.execution.php
This commit is contained in:
@@ -31,9 +31,7 @@ if (!defined('APPROOT'))
|
||||
}
|
||||
require_once(APPROOT.'application/application.inc.php');
|
||||
require_once(APPROOT.'application/ajaxwebpage.class.inc.php');
|
||||
|
||||
require_once(APPROOT.'core/log.class.inc.php');
|
||||
|
||||
require_once(APPROOT.'application/startup.inc.php');
|
||||
|
||||
class MyDBBackup extends DBBackup
|
||||
@@ -59,24 +57,8 @@ class MyDBBackup extends DBBackup
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 GetOperationName() {
|
||||
return "iTop - Database Backup";
|
||||
}
|
||||
|
||||
function Usage($oP)
|
||||
@@ -103,110 +85,75 @@ function Usage($oP)
|
||||
}
|
||||
}
|
||||
|
||||
function ExitError($oP, $sMessage)
|
||||
{
|
||||
ToolsLog::Error($sMessage);
|
||||
$oP->p($sMessage);
|
||||
$oP->output();
|
||||
exit;
|
||||
}
|
||||
/**
|
||||
* @param \Page $oP
|
||||
*
|
||||
* @throws \DictExceptionUnknownLanguage
|
||||
* @throws \OQLException
|
||||
*/
|
||||
function ExecuteMainOperation($oP){
|
||||
|
||||
|
||||
function ReadMandatoryParam($oP, $sParam)
|
||||
{
|
||||
$sValue = utils::ReadParam($sParam, null, true /* Allow CLI */, 'raw_data');
|
||||
if (is_null($sValue))
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
ExitError($oP, "ERROR: Missing argument '$sParam'");
|
||||
}
|
||||
return trim($sValue);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
// Main program
|
||||
|
||||
set_time_limit(0);
|
||||
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
$oP = new CLIPage("iTop - Database Backup");
|
||||
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP);
|
||||
}
|
||||
else
|
||||
{
|
||||
$oP = new WebPage("iTop - Database Backup");
|
||||
}
|
||||
|
||||
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');
|
||||
$bDownloadBackup = false;
|
||||
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
|
||||
{
|
||||
UserRights::Login($sAuthUser); // Login & set the user's language
|
||||
$oP->p(date('Y-m-d H:i:s')." - running backup utility");
|
||||
$sAuthUser = ReadMandatoryParam($oP, 'auth_user');
|
||||
$sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
|
||||
$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')");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ExitError($oP, "Access restricted or wrong credentials ('$sAuthUser')");
|
||||
require_once(APPROOT.'application/loginwebpage.class.inc.php');
|
||||
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
|
||||
$bDownloadBackup = utils::ReadParam('download', false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
require_once(APPROOT.'application/loginwebpage.class.inc.php');
|
||||
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
|
||||
$bDownloadBackup = utils::ReadParam('download', false);
|
||||
}
|
||||
|
||||
if (!UserRights::IsAdministrator())
|
||||
{
|
||||
ExitError($oP, "Access restricted to administors");
|
||||
}
|
||||
if (!UserRights::IsAdministrator())
|
||||
{
|
||||
ExitError($oP, "Access restricted to administors");
|
||||
}
|
||||
|
||||
if (CheckParam('?') || CheckParam('h') || CheckParam('help'))
|
||||
{
|
||||
Usage($oP);
|
||||
$oP->output();
|
||||
exit;
|
||||
}
|
||||
if (CheckParam('?') || CheckParam('h') || CheckParam('help'))
|
||||
{
|
||||
Usage($oP);
|
||||
$oP->output();
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
$sDefaultBackupFileName = SetupUtils::GetTmpDir().'/'."__DB__-%Y-%m-%d";
|
||||
$sBackupFile = utils::ReadParam('backup_file', $sDefaultBackupFileName, true, 'raw_data');
|
||||
$sDefaultBackupFileName = SetupUtils::GetTmpDir().'/'."__DB__-%Y-%m-%d";
|
||||
$sBackupFile = utils::ReadParam('backup_file', $sDefaultBackupFileName, true, 'raw_data');
|
||||
|
||||
// Interpret strftime specifications (like %Y) and database placeholders
|
||||
$oBackup = new MyDBBackup($oP);
|
||||
$oBackup->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', ''));
|
||||
$sBackupFile = $oBackup->MakeName($sBackupFile);
|
||||
$oBackup = new MyDBBackup($oP);
|
||||
$oBackup->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', ''));
|
||||
$sBackupFile = $oBackup->MakeName($sBackupFile);
|
||||
|
||||
$bSimulate = utils::ReadParam('simulate', false, true);
|
||||
$res = false;
|
||||
if ($bSimulate)
|
||||
{
|
||||
$oP->p("Simulate: would create file '$sBackupFile'");
|
||||
$bSimulate = utils::ReadParam('simulate', false, true);
|
||||
$res = false;
|
||||
if ($bSimulate)
|
||||
{
|
||||
$oP->p("Simulate: would create file '$sBackupFile'");
|
||||
}
|
||||
elseif (MetaModel::GetConfig()->Get('demo_mode'))
|
||||
{
|
||||
$oP->p("Sorry, iTop is in demonstration mode: the feature is disabled");
|
||||
}
|
||||
else
|
||||
{
|
||||
$oBackup->CreateCompressedBackup($sBackupFile);
|
||||
}
|
||||
if ($res && $bDownloadBackup)
|
||||
{
|
||||
$oBackup->DownloadBackup($sBackupFile);
|
||||
}
|
||||
}
|
||||
elseif (MetaModel::GetConfig()->Get('demo_mode'))
|
||||
{
|
||||
$oP->p("Sorry, iTop is in demonstration mode: the feature is disabled");
|
||||
}
|
||||
else
|
||||
{
|
||||
$oBackup->CreateCompressedBackup($sBackupFile);
|
||||
}
|
||||
if ($res && $bDownloadBackup)
|
||||
{
|
||||
$oBackup->DownloadBackup($sBackupFile);
|
||||
}
|
||||
$oP->output();
|
||||
|
||||
require_once(dirname(__FILE__).'/common.cli-execution.php');
|
||||
101
datamodels/2.x/itop-backup/common.cli-execution.php
Normal file
101
datamodels/2.x/itop-backup/common.cli-execution.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2013-2021 Combodo SARL
|
||||
*
|
||||
* This file is part of iTop.
|
||||
*
|
||||
* iTop is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* iTop is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
*/
|
||||
|
||||
if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
|
||||
if (!defined('APPROOT'))
|
||||
{
|
||||
if (file_exists(__DIR__.'/../../approot.inc.php'))
|
||||
{
|
||||
require_once __DIR__.'/../../approot.inc.php'; // When in env-xxxx folder
|
||||
}
|
||||
else
|
||||
{
|
||||
require_once __DIR__.'/../../../approot.inc.php'; // When in datamodels/x.x folder
|
||||
}
|
||||
}
|
||||
require_once(APPROOT.'application/application.inc.php');
|
||||
require_once(APPROOT.'application/ajaxwebpage.class.inc.php');
|
||||
require_once(APPROOT.'core/log.class.inc.php');
|
||||
require_once(APPROOT.'application/startup.inc.php');
|
||||
|
||||
/**
|
||||
* 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 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(GetOperationName());
|
||||
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP);
|
||||
}
|
||||
else
|
||||
{
|
||||
$oP = new WebPage(GetOperationName());
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
utils::UseParamFile();
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
ExitError($oP, $e->GetMessage());
|
||||
}
|
||||
|
||||
ExecuteMainOperation($oP);
|
||||
|
||||
$oP->output();
|
||||
151
datamodels/2.x/itop-backup/restore.php
Normal file
151
datamodels/2.x/itop-backup/restore.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2013-2021 Combodo SARL
|
||||
*
|
||||
* This file is part of iTop.
|
||||
*
|
||||
* iTop is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* iTop is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
*/
|
||||
|
||||
if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
|
||||
if (!defined('APPROOT'))
|
||||
{
|
||||
if (file_exists(__DIR__.'/../../approot.inc.php'))
|
||||
{
|
||||
require_once __DIR__.'/../../approot.inc.php'; // When in env-xxxx folder
|
||||
}
|
||||
else
|
||||
{
|
||||
require_once __DIR__.'/../../../approot.inc.php'; // When in datamodels/x.x folder
|
||||
}
|
||||
}
|
||||
require_once(APPROOT.'application/application.inc.php');
|
||||
require_once(APPROOT.'application/ajaxwebpage.class.inc.php');
|
||||
require_once(APPROOT.'core/log.class.inc.php');
|
||||
require_once(APPROOT.'application/startup.inc.php');
|
||||
require_once(dirname(__FILE__).'/dbrestore.class.inc.php');
|
||||
|
||||
/**
|
||||
* @since 3.0.0 N°4227 new class to handle iTop restore manually via a CLI command
|
||||
*/
|
||||
class MyCliRestore extends DBRestore
|
||||
{
|
||||
/** @var Page used to send log */
|
||||
protected $oPage;
|
||||
|
||||
protected function LogInfo($sMsg)
|
||||
{
|
||||
$this->oPage->p($sMsg);
|
||||
}
|
||||
|
||||
protected function LogError($sMsg)
|
||||
{
|
||||
$this->oPage->p('Error: '.$sMsg);
|
||||
ToolsLog::Error($sMsg);
|
||||
}
|
||||
|
||||
public function __construct($oPage)
|
||||
{
|
||||
$this->oPage = $oPage;
|
||||
parent::__construct();
|
||||
}
|
||||
}
|
||||
|
||||
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 GetOperationName() {
|
||||
return "iTop - iTop Restore";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Page $oP
|
||||
*
|
||||
* @throws \DictExceptionUnknownLanguage
|
||||
* @throws \OQLException
|
||||
*/
|
||||
function ExecuteMainOperation($oP){
|
||||
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');
|
||||
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 administrators");
|
||||
}
|
||||
|
||||
if (CheckParam('?') || CheckParam('h') || CheckParam('help'))
|
||||
{
|
||||
Usage($oP);
|
||||
$oP->output();
|
||||
exit;
|
||||
}
|
||||
|
||||
// Interpret strftime specifications (like %Y) and database placeholders
|
||||
$oRestore = new MyCliRestore($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');
|
||||
$oRestore->RestoreFromCompressedBackup($sBackupFile, $sEnvironment);
|
||||
}
|
||||
}
|
||||
|
||||
require_once(dirname(__FILE__).'/common.cli-execution.php');
|
||||
Reference in New Issue
Block a user