-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup.php
More file actions
209 lines (181 loc) · 7.79 KB
/
Backup.php
File metadata and controls
209 lines (181 loc) · 7.79 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace modmore\SiteDashClient\Upgrade;
use modmore\SiteDashClient\CommandInterface;
use modmore\SiteDashClient\Communication\Pusher;
use modmore\SiteDashClient\Communication\Result;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
class Backup implements CommandInterface {
protected $modx;
protected $files = [];
protected $targetDirectory;
/**
* @var Pusher|null
*/
private $pusher;
public function __construct(\modX $modx, $pusher = null)
{
$this->modx = $modx;
$this->pusher = $pusher;
$this->files = [
MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php',
MODX_MANAGER_PATH . 'config.core.php',
MODX_CONNECTORS_PATH . 'config.core.php',
MODX_BASE_PATH . 'config.core.php',
MODX_BASE_PATH . 'index.php',
];
$optionalFiles = [
MODX_BASE_PATH . '.htaccess',
];
foreach ($optionalFiles as $file) {
if (file_exists($file)) {
$this->files[] = $file;
}
}
$this->targetDirectory = MODX_CORE_PATH . 'export/backup_' . date('Y-m-d-His') . '_' . rand(0,9999999) . '/';
}
public function run()
{
if (!function_exists('proc_open')) {
http_response_code(503);
echo json_encode([
'success' => false,
'message' => 'The proc_open() function is disabled on your server. This is required for the backup to run.',
'directory' => str_replace(MODX_CORE_PATH, '{core_path}', $this->targetDirectory)
], JSON_PRETTY_PRINT);
return;
}
if (!function_exists('proc_get_status')) {
http_response_code(503);
echo json_encode([
'success' => false,
'message' => 'The proc_get_status() function is disabled on your server. This is required to allow the status of the backup to be checked.',
'directory' => str_replace(MODX_CORE_PATH, '{core_path}', $this->targetDirectory)
], JSON_PRETTY_PRINT);
return;
}
if (!$this->createDirectory($this->targetDirectory)) {
http_response_code(503);
echo json_encode([
'success' => false,
'message' => 'Could not create the backup directory ' . str_replace(MODX_CORE_PATH, '{core_path}', $this->targetDirectory),
'directory' => str_replace(MODX_CORE_PATH, '{core_path}', $this->targetDirectory)
], JSON_PRETTY_PRINT);
return;
}
// If a push result was requested, send an ack response and continue processing
if ($this->pusher) {
$this->pusher->acknowledge();
}
$result = new Result($this->pusher);
/**
* Include the config file to access the database information
*
* @var $database_type
* @var $database_server
* @var $database_user
* @var $database_password
* @var $dbase
*/
include MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
$database_password = str_replace("'", '\'', $database_password);
$password_parameter = '';
if ($database_password !== '') {
$password_parameter = "-p'{$database_password}'";
}
$targetFile = $this->targetDirectory . $dbase . '.sql';
$finder = new ExecutableFinder();
$mysqldump = $finder->find('mysqldump');
if ($mysqldump === null) {
$mysqldump = $this->modx->getOption('sitedashclient.mysqldump_binary', null, 'mysqldump', true);
}
$cmd = [$mysqldump, "-u{$database_user}", $password_parameter, "-h {$database_server}", $dbase];
$excludes = ['modSession'];
foreach ($excludes as $exclude) {
$table = trim($this->modx->getTableName($exclude, false), '`');
$cmd[] = "--ignore-table=\"{$dbase}.{$table}\"";
}
$cmd = implode(' ', $cmd) . " > {$targetFile}";
$backupProcess = new Process($cmd);
$backupProcess->setTimeout(120);
$backupProcess->setIdleTimeout(120);
try {
$backupProcess->run();
}
catch (\Exception $e) {
$msg = $e->getMessage();
$msg = str_replace($password_parameter, '-p\'<PASS>\'', $msg);
$trace = $e->getTraceAsString();
$trace = str_replace($password_parameter, '-p\'<PASS>\'', $trace);
$result(503, [
'success' => false,
'message' => 'Received an error trying to run mysqlbackup: ' . $msg,
'binary' => $mysqldump,
'directory' => str_replace(MODX_CORE_PATH, '{core_path}', $this->targetDirectory),
'output' => $trace,
]);
return;
}
$output = $backupProcess->getErrorOutput() . ' ' . $backupProcess->getOutput();
$output = str_replace($password_parameter, '-p\'<PASS>\'', $output);
if (!$backupProcess->isSuccessful()) {
$code = $backupProcess->getExitCode();
if ($code === 127) {
$result(503, [
'success' => false,
'message' => 'Could not find the mysqldump program on your server; please configure the sitedashclient.mysqldump_binary system setting to point to mysqldump to create backups.',
'binary' => $mysqldump,
'directory' => str_replace(MODX_CORE_PATH, '{core_path}', $this->targetDirectory),
'output' => $output,
]);
return;
}
$result(503, [
'success' => false,
'message' => 'Received exit code ' . $code . ' trying to create a database backup using ' . $mysqldump . ' with message: ' . $output,
'output' => $output,
'return' => $code,
]);
return;
}
$backupSize = filesize($targetFile);
if ($backupSize < 150 * 1024) { // a clean install is ~ 200kb, so we ask for at least 150
$result(503, [
'success' => false,
'message' => 'While the backup with ' . $mysqldump . ' did not indicate an error, the mysql backup is only ' . number_format($backupSize / 1024, 0) . 'kb in size, so it probably failed.',
'output' => $output,
'return' => $backupProcess->getExitCode(),
]);
return;
}
foreach ($this->files as $source) {
$target = $this->targetDirectory . 'files/';
$target .= str_replace([MODX_CORE_PATH, MODX_BASE_PATH], ['core/', ''], $source);
if (!file_exists($source)) {
$this->modx->log(\modX::LOG_LEVEL_ERROR, '[SiteDashClient] Could not backup file '. $source . ' - it does not exist.');
continue;
}
if (!$this->createDirectory(dirname($target))) {
$this->modx->log(\modX::LOG_LEVEL_ERROR, '[SiteDashClient] Could not backup file '. $source . ' - tried creating target directory ' . $target . ' but failed.');
continue;
}
if (!copy($source, $target)) {
$this->modx->log(\modX::LOG_LEVEL_ERROR, '[SiteDashClient] Could not backup file '. $source . ' - copy failed.');
}
}
$result(200, [
'success' => true,
'directory' => str_replace(MODX_CORE_PATH, '', $this->targetDirectory),
]);
}
private function createDirectory($target)
{
if (file_exists($target) && is_dir($target)) {
return true;
}
if (!mkdir($target, 0755, true) && !is_dir($target)) {
return false;
}
return true;
}
}