-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore.php
More file actions
64 lines (48 loc) · 1.69 KB
/
restore.php
File metadata and controls
64 lines (48 loc) · 1.69 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
<?php
require_once 'core/Database.php';
$storageDir = __DIR__ . '/storage';
$backupFiles = array_diff(scandir($storageDir), array('.', '..', '.gitkeep'));
if (empty($backupFiles)) {
die("No backup files found in the storage directory.\n");
}
echo "Available backup files:\n";
foreach ($backupFiles as $index => $file) {
echo "[$index] $file\n";
}
echo "Enter the number of the backup file to restore (or enter / to cancel): ";
$fileIndex = trim(fgets(STDIN));
if ($fileIndex === '/') {
die("Operation cancelled by user.\n");
}
if (!isset($backupFiles[$fileIndex])) {
die("Invalid selection. Exiting...\n");
}
$backupFilePath = $storageDir . '/' . $backupFiles[$fileIndex];
echo "Would you like to:\n";
echo "[1] Restore to a new database\n";
echo "[2] Restore to an existing database\n";
echo "[/] Cancel\n";
echo "Enter your choice: ";
$choice = trim(fgets(STDIN));
if ($choice === '/') {
die("Operation cancelled by user.\n");
}
if ($choice === '1') {
echo "Enter the new database name to restore to: ";
$newDbName = trim(fgets(STDIN));
$db = new Database();
$db->restoreDatabase($backupFilePath, $newDbName);
} elseif ($choice === '2') {
echo "Enter the name of the existing database to restore to: ";
$existingDbName = trim(fgets(STDIN));
echo "Restoring to the existing database: $existingDbName\n";
$db = new Database();
try {
$db->restoreDatabase($backupFilePath, $existingDbName);
echo "Database restoration to `$existingDbName` completed successfully.\n";
} catch (Exception $e) {
echo "Error: Unable to restore to the existing database. " . $e->getMessage() . "\n";
}
} else {
echo "Invalid choice. Exiting...\n";
}