-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrmanage.php
172 lines (144 loc) · 4.96 KB
/
rmanage.php
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
<?php
// Use the composer PSR-4 autoloader.
$loader = require '/opt/app-root/src/vendor/autoload.php';
$loader->addPsr4('RemoteManage\\', __DIR__ . '/src/');
use RemoteManage\RemoteManageServer;
// If a shared secret has been defined, then it is required. Also, using a
// shared secret means that only the POST method is supported.
if ($secret = getenv('RMANAGE_SECRET')) {
if ($secret != $_POST['secret']) {
header('Content-type: application/json');
echo json_encode([
'status' => 'error',
'message' => 'Invalid credentials.'
], JSON_PRETTY_PRINT) . PHP_EOL;
exit;
}
}
$rmserver = new RemoteManageServer();
// Get the main operation
$operation = $_REQUEST['operation'] ?? '';
// Get the options and assemble into an array
$options = [];
if (isset($_REQUEST['verbose']) && $_REQUEST['verbose'] == 'true') {
$options[] = '--verbose --log-stderr';
}
if (isset($_REQUEST['format'])) {
$options[] = '--format=' . $_REQUEST['format'];
}
// Determine Job ID.
// If a job id is specified, backup and restore will run as a background task.
if (isset($_REQUEST['job'])) {
if ($_REQUEST['job'] == 'true') {
// New job ID. System will generate and return job id.
$job = getmypid();
} elseif (is_numeric($_REQUEST['job'])) {
// Specific job id or for job already in progress.
// For static systems, use job=0.
$job = $_REQUEST['job'];
} else {
// If no job id was specified, task will run in foreground.
// No log file will be created.
$job = '';
}
}
// Determine location for storing rmanage_##.log file.
if (file_exists(getenv('HOME') . '/lang/en/moodle.php')) { // Moodle.
$rmanageLog = "/data/moodle";
} elseif (is_dir(getenv('HOME') . '/drush')) { // Drupal.
$rmanageLog = "/opt/app-root/src/data";
} else { // Unknown type.
$rmanageLog = "/tmp";
}
// Clean-up log files older than 7 days.
if ($files = glob($rmanageLog . '/rmanage_*.log')) {
$now = time();
$seconds = 259200; // 60 * 60 * 24 * 3 = 3 days.
foreach ($files as $file) {
if (is_file($file)) {
if ($now - filemtime($file) >= $seconds) { // Too old, delete it.
unlink($file);
}
}
}
}
// Determine name of log file.
$rmanageLog .= "/rmanage_$job.log";
$json = [];
// Assemble the basic command to run
$cmd = 'php ' . dirname(__FILE__) . '/manage.php';
if ($options) {
$cmd .= ' ' . join(' ', $options);
}
// Delete the log file if it already exists, except when using the query command.
if ($operation != 'query' && file_exists($rmanageLog)) {
unlink($rmanageLog);
}
switch ($operation) {
case 'backup':
$rmserver->getS3Credentials();
if ($job != '') { // Background mode.
`$cmd backup > $rmanageLog &`;
$json = ['status' => 'ok', 'job' => $job];
} else { // Immediate mode.
$json = $rmserver->getJSONResult(`$cmd backup`);
}
break;
case 'restore':
if (empty($_REQUEST['s3file'])) {
$json = ['result' => 'error', 'message' => 'Missing s3file'];
break;
}
$rmserver->getS3Credentials();
$s3file = $_REQUEST['s3file'];
if ($job != '') { // Background mode.
`$cmd restore $s3file --exclude $rmanageLog > $rmanageLog &`;
$json = ['status' => 'ok', 'job' => $job];
} else { // Immediate mode.
$json = $rmserver->getJSONResult(`$cmd restore $s3file`);
}
break;
case 'query':
$json = @file_get_contents($rmanageLog);
if (empty($json)) {
$json = ['result' => 'error', 'message' => 'Log file is missing.'];
} else {
$json = $rmserver->getJSONResult($json);
}
break;
case 'maint':
$mode = $_REQUEST['mode'] ?? '';
$json = $rmserver->getJSONResult(`$cmd maint $mode`);
break;
case 's3list':
$rmserver->getS3Credentials();
$json = $rmserver->getJSONResult(`$cmd s3list`);
break;
case 'cr':
if ($job != '') { // Background mode.
`$cmd cr > $rmanageLog &`;
$json = ['status' => 'ok', 'job' => $job];
} else { // Immediate mode.
$json = $rmserver->getJSONResult(`$cmd cr`);
}
break;
case 'updb':
if ($job != '') { // Background mode.
`$cmd updb > $rmanageLog &`;
$json = ['status' => 'ok', 'job' => $job];
} else { // Immediate mode.
$json = $rmserver->getJSONResult(`$cmd updb`);
}
break;
case 'pmlist':
$json = $rmserver->getJSONResult(`$cmd pmlist`);
break;
case 'space':
$json = $rmserver->getJSONResult(`$cmd space`);
break;
default:
$json = ['status' => 'error', 'message' => "Unknown operation $operation"];
}
// Exit with a JSON result
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT) . PHP_EOL;