-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathbackup.php
More file actions
138 lines (120 loc) · 4.2 KB
/
backup.php
File metadata and controls
138 lines (120 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Copyright (C) 2013-2024 Combodo SAS
*
* 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
*/
use Combodo\iTop\Application\WebPage\Page;
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.'core/log.class.inc.php');
require_once(APPROOT.'application/startup.inc.php');
class MyDBBackup extends DBBackup
{
/** @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 GetOperationName()
{
return "iTop - Database Backup";
}
function Usage($oP)
{
$oP->p('Perform a backup of the iTop database by running mysqldump');
$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 (https://www.php.net/manual/fr/function.strftime.php). The following placeholders are available: __HOST__, __DB__, __SUBNAME__');
$oP->p('simulate [optional]: set to check the name of the file that would be created');
$oP->p('mysql_bindir [optional]: specify the path for mysqldump');
if (utils::IsModeCLI()) {
$oP->p('Example: php -q backup.php --auth_user=admin --auth_pwd=myPassw0rd');
$oP->p('Known limitation: the current directory must be the directory of backup.php');
} else {
$oP->p('Example: .../backup.php?backup_file=/tmp/backup.__DB__-__SUBNAME__.%Y-%m');
}
}
/**
* @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');
$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 {
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 (CheckParam('?') || CheckParam('h') || CheckParam('help')) {
Usage($oP);
$oP->output();
exit;
}
$sBackupFile = utils::ReadParam('backup_file', BACKUP_DEFAULT_FORMAT, 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);
$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);
}
}
require_once(__DIR__.'/common.cli-execution.php');