-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.php
executable file
·235 lines (196 loc) · 5.7 KB
/
backup.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
include "config.php";
session_start();
if (isset($_POST["login"])) {
$username = $_POST["username"];
$password = $_POST["password"];
// Replace with your desired username and password
if ($username === "xxx" && $password === "xxx") {
$_SESSION["logged_in"] = true;
} else {
echo "<p>Invalid username or password.</p>";
}
}
if (isset($_POST["logout"])) {
unset($_SESSION["logged_in"]);
}
// Check if backup request is made
if (isset($_POST['backup']) && isset($_SESSION["logged_in"])) {
$backup_count = 0;
$new_backup_table = '';
// Get list of tables with the prefix
$tables = $conn->query("SHOW TABLES LIKE 'posts_backup_%'");
if ($tables->num_rows > 0) {
$backup_count = $tables->num_rows;
}
$new_backup_table = "posts_backup_" . ($backup_count + 1);
// Create a new backup table
$conn->query("CREATE TABLE " . $new_backup_table . " LIKE posts");
$conn->query("INSERT " . $new_backup_table . " SELECT * FROM posts");
echo "<p>Backup created successfully.</p>";
}
function list_backups() {
global $conn;
$backups = $conn->query("SHOW TABLES LIKE 'posts_backup_%'");
if ($backups->num_rows > 0) {
echo "<ul>";
while ($row = $backups->fetch_row()) {
$backup_table = $row[0];
echo "<li>" . htmlspecialchars($backup_table) . " - <a href='backup.php?restore=" . urlencode($backup_table) . "'>Restore</a> - <a href='backup.php?delete=" . urlencode($backup_table) . "'>Delete</a> - <a href='backup.php?download=" . urlencode($backup_table) . "'>Download</a> - <a href='backup.php?visualize=" . urlencode($backup_table) . "'>Visualize</a></li>";
}
echo "</ul>";
} else {
echo "<p>No backups found.</p>";
}
$conn->close();
}
if (isset($_GET["restore"]) && isset($_SESSION["logged_in"])) {
$backup_table = $_GET["restore"];
$conn->query("TRUNCATE posts");
$conn->query("INSERT posts SELECT * FROM " . $backup_table);
echo "<p>Backup restored successfully.</p>";
}
if (isset($_GET["delete"]) && isset($_SESSION["logged_in"])) {
$backup_table = $_GET["delete"];
$conn->query("DROP TABLE " . $backup_table);
echo "<p>Backup deleted successfully.</p>";
}
if (isset($_GET["download"]) && isset($_SESSION["logged_in"])) {
$backup_table = $_GET["download"];
$cmd = "mysqldump -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " " . $backup_table . " > " . $backup_table . ".sql";
exec($cmd, $output, $result);
if ($result === 0) {
header("Content-Disposition: attachment; filename=" . $backup_table . ".sql");
header("Content-Type: application/sql");
readfile($backup_table . ".sql");
unlink($backup_table . ".sql");
exit;
} else {
echo "<p>Error creating backup file for download.</p>";
}
}
if (isset($_GET["visualize"]) && isset($_SESSION["logged_in"])) {
$backup_table = $_GET["visualize"];
$result = $conn->query("SELECT * FROM " . $backup_table);
echo "<h2>Contents of " . htmlspecialchars($backup_table) . "</h2>";
echo "<table border='1'>";
echo "<tr>";
$columnNames = $conn->query("SHOW COLUMNS FROM " . $backup_table);
while ($column = $columnNames->fetch_assoc()) {
echo "<th>" . htmlspecialchars($column["Field"]) . "</th>";
}
echo "</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
foreach ($row as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Backup Management</title>
<style>
body {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
background-color: #f9f9f9;
transition: background-color 0.3s ease-in-out, color 0.3s ease-in-out;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
font-size: 28px;
margin-bottom: 20px;
}
h2 {
font-size: 24px;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
gap: 10px;
}
input[type="text"], input[type="password"] {
padding: 10px;
font-size: 14px;
}
input[type="submit"] {
padding: 10px;
font-size: 14px;
cursor: pointer;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ccc;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
a {
color: #333;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
@media (prefers-color-scheme: dark) {
body {
color: #f9f9f9;
background-color: #333;
}
th {
background-color: #444;
}
tr:nth-child(even) {
background-color: #555;
}
a {
color: #f9f9f9;
}
}
</style>
</head>
<body>
<?php if (!isset($_SESSION["logged_in"])): ?>
<form action="backup.php" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required>
<br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required>
<br>
<input type="submit" name="login" value="Log in">
</form>
<?php else: ?>
<h1>Backup Management</h1>
<form action="backup.php" method="post">
<input type="submit" name="backup" value="Create Backup">
<input type="submit" name="logout" value="Logout">
</form>
<h2>Available Backups</h2>
<?php list_backups(); ?>
<?php endif; ?>
</body>
</html>