Skip to content

Commit 4109bb1

Browse files
committed
PUSH
-> Finish #289 -> Finish #291 -> Finish #290 -> Finish #286 -> Added a health page -> Added an option to uploads logs easy
1 parent 4d9ad6f commit 4109bb1

File tree

19 files changed

+2424
-396
lines changed

19 files changed

+2424
-396
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,8 @@
139139
"backend/storage/packages/phpmailer/phpmailer/language"
140140
],
141141
"php.version": "8.4",
142+
"php.stubs": [
143+
"*",
144+
"opcache"
145+
],
142146
}

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
- Feature: You can onw delete snapshots!
4343
- Feature: Plugins can hook into backups!
4444
- Feature: Daily backups of the instance!
45+
- Feature: Added a health page!
46+
- Feature: Add a option to upload logs from health page!
47+
- Feature: MythicalCloud (Upload,Download,List,Purge) your backups in the cloud!
4548

4649
# Features Removed:
4750

backend/app/Api/Admin/Backups/Backups.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use MythicalDash\App;
1515
use MythicalDash\Hooks\Backup;
1616
use MythicalDash\Chat\User\Can;
17+
use MythicalDash\Hooks\MythicalCloud;
18+
use MythicalDash\Config\ConfigInterface;
1719
use MythicalDash\Chat\columns\UserColumns;
1820
use MythicalDash\Chat\User\UserActivities;
1921
use MythicalDash\CloudFlare\CloudFlareRealIP;
@@ -54,6 +56,53 @@
5456
}
5557
});
5658

59+
$router->post('/api/admin/backup/(.*)/upload-to-cloud', function (string $backupId): void {
60+
App::init();
61+
$appInstance = App::getInstance(true);
62+
$appInstance->allowOnlyPOST();
63+
$session = new MythicalDash\Chat\User\Session($appInstance);
64+
65+
if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) {
66+
try {
67+
68+
try {
69+
if (Backup::exists($backupId)) {
70+
$bkPath = __DIR__ . '/../../../../storage/backups/backup_' . $backupId . '.mydb';
71+
} else {
72+
throw new Exception('Backup not found');
73+
}
74+
} catch (Exception $e) {
75+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'BACKUP_NOT_FOUND']);
76+
}
77+
// Get license key for MythicalCloud
78+
$licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL');
79+
if (!$licenseKey) {
80+
throw new Exception('MythicalCloud license key not configured');
81+
}
82+
83+
try {
84+
// Upload the backup to MythicalCloud
85+
if (file_exists($bkPath)) {
86+
$result = MythicalCloud::uploadBackup($licenseKey, $bkPath, 'Windows', '10.0', 'x64');
87+
if ($result === null || !$result) {
88+
$appInstance->InternalServerError('Failed to upload backup to MythicalCloud', ['error_code' => 'UPLOAD_ERROR']);
89+
}
90+
} else {
91+
$appInstance->InternalServerError('Backup not found', ['error_code' => 'BACKUP_NOT_FOUND', 'path' => $bkPath]);
92+
}
93+
94+
} catch (Exception $e) {
95+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'UPLOAD_ERROR']);
96+
}
97+
$appInstance->OK('Backup uploaded to MythicalCloud successfully', $result);
98+
} catch (Exception $e) {
99+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'UPLOAD_ERROR']);
100+
}
101+
} else {
102+
$appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']);
103+
}
104+
});
105+
57106
$router->get('/api/admin/backup/(.*)/delete', function (string $backupId): void {
58107
App::init();
59108
$appInstance = App::getInstance(true);
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
/*
4+
* This file is part of MythicalDash.
5+
* Please view the LICENSE file that was distributed with this source code.
6+
*
7+
* # MythicalSystems License v2.0
8+
*
9+
* ## Copyright (c) 2021–2025 MythicalSystems and Cassian Gherman
10+
*
11+
* Breaking any of the following rules will result in a permanent ban from the MythicalSystems community and all of its services.
12+
*/
13+
14+
use MythicalDash\App;
15+
use MythicalDash\Chat\User\Can;
16+
use MythicalDash\Chat\User\Session;
17+
use MythicalDash\Hooks\MythicalCloud;
18+
use MythicalDash\Config\ConfigInterface;
19+
use MythicalDash\Chat\columns\UserColumns;
20+
21+
// Get all backups
22+
$router->get('/api/admin/cloud/backups', function (): void {
23+
App::init();
24+
$appInstance = App::getInstance(true);
25+
$appInstance->allowOnlyGET();
26+
$session = new Session($appInstance);
27+
28+
if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) {
29+
try {
30+
$licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL');
31+
if (!$licenseKey) {
32+
throw new Exception('Mythical Cloud license key not configured');
33+
}
34+
35+
$backups = MythicalCloud::getBackups($licenseKey);
36+
if ($backups === null) {
37+
throw new Exception('Failed to retrieve backups');
38+
}
39+
40+
$appInstance->OK('Backups retrieved successfully', $backups);
41+
} catch (Exception $e) {
42+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']);
43+
}
44+
} else {
45+
$appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']);
46+
}
47+
});
48+
49+
// Download backup
50+
$router->get('/api/admin/cloud/backup/(.*)/download', function (string $backupId): void {
51+
App::init();
52+
$appInstance = App::getInstance(true);
53+
$appInstance->allowOnlyGET();
54+
$session = new Session($appInstance);
55+
56+
if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) {
57+
try {
58+
$licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL');
59+
60+
if (!$licenseKey) {
61+
throw new Exception('Mythical Cloud license key not configured');
62+
}
63+
64+
$backupDir = __DIR__ . '/../../../../storage/backups';
65+
if (!is_dir($backupDir)) {
66+
mkdir($backupDir, 0755, true);
67+
}
68+
$savePath = $backupDir . '/backup_' . $backupId . '.mydb';
69+
$backupContent = MythicalCloud::downloadBackup($licenseKey, $backupId, $savePath);
70+
if ($backupContent === false) {
71+
throw new Exception('Failed to download backup');
72+
}
73+
74+
$appInstance->OK('Backup downloaded successfully', []);
75+
exit;
76+
} catch (Exception $e) {
77+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']);
78+
}
79+
} else {
80+
$appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']);
81+
}
82+
});
83+
84+
// Delete specific backup
85+
$router->post('/api/admin/cloud/backup/(.*)/delete', function (string $backupId): void {
86+
App::init();
87+
$appInstance = App::getInstance(true);
88+
$appInstance->allowOnlyPOST();
89+
$session = new Session($appInstance);
90+
91+
if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) {
92+
try {
93+
$licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL');
94+
95+
if (!$licenseKey) {
96+
throw new Exception('Mythical Cloud license key not configured');
97+
}
98+
99+
$result = MythicalCloud::deleteBackup($licenseKey, $backupId);
100+
if ($result === null) {
101+
throw new Exception('Failed to delete backup');
102+
}
103+
104+
$appInstance->OK('Backup deleted successfully', $result);
105+
} catch (Exception $e) {
106+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']);
107+
}
108+
} else {
109+
$appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']);
110+
}
111+
});
112+
113+
// Get specific backup info
114+
$router->get('/api/admin/cloud/backup/(.*)', function (string $backupId): void {
115+
App::init();
116+
$appInstance = App::getInstance(true);
117+
$appInstance->allowOnlyGET();
118+
$session = new Session($appInstance);
119+
120+
if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) {
121+
try {
122+
$licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL');
123+
124+
if (!$licenseKey) {
125+
throw new Exception('Mythical Cloud license key not configured');
126+
}
127+
128+
$backupInfo = MythicalCloud::getBackupInfo($licenseKey, $backupId);
129+
if ($backupInfo === null) {
130+
throw new Exception('Failed to retrieve backup info');
131+
}
132+
133+
$appInstance->OK('Backup info retrieved successfully', $backupInfo);
134+
} catch (Exception $e) {
135+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']);
136+
}
137+
} else {
138+
$appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']);
139+
}
140+
});
141+
142+
// Delete all backups
143+
$router->post('/api/admin/cloud/backups/wipe', function (): void {
144+
App::init();
145+
$appInstance = App::getInstance(true);
146+
$appInstance->allowOnlyPOST();
147+
$session = new Session($appInstance);
148+
149+
if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) {
150+
try {
151+
$licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL');
152+
153+
if (!$licenseKey) {
154+
throw new Exception('Mythical Cloud license key not configured');
155+
}
156+
157+
$result = MythicalCloud::deleteAllBackups($licenseKey);
158+
if ($result === null) {
159+
throw new Exception('Failed to delete all backups');
160+
}
161+
162+
$appInstance->OK('All backups deleted successfully', $result);
163+
} catch (Exception $e) {
164+
$appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']);
165+
}
166+
} else {
167+
$appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']);
168+
}
169+
});

0 commit comments

Comments
 (0)