-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.php
589 lines (506 loc) · 22.9 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
//Config
//########################################
$registerNew = true; //Turn this to false if you dont want to register a new password
$dir_path = "./"; //This is for the directory the backup.php lies in.
$correct_password = "827ccb0eea8a706c4c34a16891f84e7b"; // pass=12345, Change this to your actual password as an MD5 hash
//Database conf, if you want them permanent
$set_dbhost = '127.0.0.1';
$set_dbname = "db_name";
$set_dbuser = "db_user";
$set_dbpass = "db_pass";
//########################################
if (!empty($_POST['newpassword']) && $registerNew == true) {
$newpass = md5($_POST['newpassword']);
$backupFile = file_get_contents("." . $_SERVER['PHP_SELF']);
$backupFile = str_replace($correct_password, $newpass, $backupFile);
$backupFile = preg_replace('/\$registerNew = true;/', '$registerNew = false;', $backupFile, 1);
file_put_contents("." . $_SERVER['PHP_SELF'] . ".tmp", $backupFile);
if (unlink("." . $_SERVER['PHP_SELF']) && rename("." . $_SERVER['PHP_SELF'] . ".tmp", "." . $_SERVER['PHP_SELF'])) {
$registerNew = false;
} else {
echo '<div class="alert alert-success" role="alert">Failed to register, check file permissions</div>';
}
}
session_start();
function logout()
{
$_SESSION = array();
// Destroy the session
session_destroy();
}
function preventUnAuthorized()
{
if (!authorizeLogggedIn()) {
die("Unauthorized action");
}
}
function authorizeLogggedIn()
{
global $correct_password, $set_dbhost, $set_dbname, $set_dbuser, $set_dbpass;
if (isset($_SESSION['backup_logged_in']) && $_SESSION['backup_logged_in'] === $correct_password) {
if (isset($_SESSION['set_dbhost'])) {
$set_dbhost = $_SESSION['set_dbhost'];
}
if (isset($_SESSION['set_dbuser'])) {
$set_dbuser = $_SESSION['set_dbuser'];
}
if (isset($_SESSION['set_dbname'])) {
$set_dbname = $_SESSION['set_dbname'];
}
if (isset($_SESSION['set_dbpass'])) {
$set_dbpass = $_SESSION['set_dbpass'];
}
return true;
} else {
return false;
}
}
if (isset($_GET["logout"]) && $_GET["logout"] == 1) {
logout();
header("Location: " . $_SERVER['PHP_SELF']);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if the password is correct (you need to define your own password)
if (isset($_POST["password"]) && !$_SESSION['backup_logged_in'] && md5($_POST["password"]) == $correct_password) {
// Password is correct, set backup_logged_in session variable
$_SESSION['backup_logged_in'] = $correct_password;
} else {
if (isset($_POST["password"])) {
echo '<div class="alert alert-danger" role="alert">Wrong password.</div>';
}
}
}
function unzipFile($zipFile)
{
global $dir_path;
preventUnAuthorized();
$zip = new ZipArchive;
if ($zip->open($zipFile) === TRUE) {
$zip->extractTo($dir_path);
$zip->close();
echo '<div class="alert alert-success" role="alert">File unzipped successfully!</div>';
} else {
echo '<div class="alert alert-danger" role="alert">Failed to unzip the file.</div>';
}
}
// Handle sql import action
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['sql_file'])) {
preventUnAuthorized();
$disable_foreign = false;
if (isset($_POST['disableforeginkey'])) {
$disable_foreign = true;
}
$selectedSQLFile = $_POST['sql_file'];
doSqlImport($selectedSQLFile, $disable_foreign);
}
// Handle unzip action
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['zip_file'])) {
preventUnAuthorized();
$selectedZipFile = $_POST['zip_file'];
unzipFile($selectedZipFile);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['mysql'])) {
preventUnAuthorized();
// Backup SQL
$db_host = $_POST['db_host'];
$db_user = $_POST['db_user'];
$db_pass = $_POST['db_pass'];
$db_name = $_POST['db_name'];
$_SESSION['set_dbhost'] = $db_host;
$_SESSION['set_dbuser'] = $db_user;
$_SESSION['set_dbpass'] = $db_pass;
$_SESSION['set_dbname'] = $db_name;
$filename = doSqlBackup($db_host, $db_user, $db_pass, $db_name);
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$filename");
header("Content-length: " . filesize($filename));
header("Pragma: no-cache");
header("Expires: 0");
readfile($filename);
unlink($filename);
exit;
} elseif (isset($_POST['full_backup'])) {
preventUnAuthorized();
$db_host = $_POST['db_host'];
$db_user = $_POST['db_user'];
$db_pass = $_POST['db_pass'];
$db_name = $_POST['db_name'];
$_SESSION['set_dbhost'] = $db_host;
$_SESSION['set_dbuser'] = $db_user;
$_SESSION['set_dbpass'] = $db_pass;
$_SESSION['set_dbname'] = $db_name;
$rootPath = realpath($dir_path);
$sqlfile = doSqlBackup($db_host, $db_user, $db_pass, $db_name);
$archive_file_name = "backup-" . time() . ".zip";
// Initialize archive object
$zip = new ZipArchive();
$zip->open($archive_file_name, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
readfile($archive_file_name);
unlink($sqlfile);
unlink($archive_file_name);
exit;
} elseif (isset($_POST['connect'])) {
preventUnAuthorized();
try {
$db_host = $_POST['db_host'];
$db_user = $_POST['db_user'];
$db_pass = $_POST['db_pass'];
$db_name = $_POST['db_name'];
$dbConnection = new mysqli($db_host, $db_user, $db_pass, $db_name);
} catch (Exception $e) {
header("Location: " . $_SERVER['PHP_SELF'] . "?error=Connection failed: " . $e->getMessage());
exit;
}
$_SESSION['set_dbhost'] = $db_host;
$_SESSION['set_dbuser'] = $db_user;
$_SESSION['set_dbpass'] = $db_pass;
$_SESSION['set_dbname'] = $db_name;
$_SESSION['db_connection'] = true;
header("Location: " . $_SERVER['PHP_SELF'] . "?success=Connected to Database ");
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["file"])) {
preventUnAuthorized();
$uploadFile = $dir_path . basename($_FILES["file"]["name"]);
// Check if file already exists
if (file_exists($uploadFile)) {
echo '<div class="alert alert-danger" role="alert">File already exists.</div>';
} else {
// Try to upload file
if (move_uploaded_file($_FILES["file"]["tmp_name"], $uploadFile)) {
echo '<div class="alert alert-success" role="alert">File uploaded successfully!</div>';
} else {
echo '<div class="alert alert-danger" role="alert">Error uploading file.</div>';
}
}
}
function doSqlImport($sqlFile, $disable_foreign)
{
preventUnAuthorized();
global $set_dbhost, $set_dbname, $set_dbuser, $set_dbpass;
try {
$dbConnection = new mysqli($set_dbhost, $set_dbuser, $set_dbpass, $set_dbname);
} catch (Exception $e) {
header("Location: " . $_SERVER['PHP_SELF'] . "?error=Connection failed: " . urlencode($e->getMessage()));
exit;
}
if ($disable_foreign) {
$dbConnection->query('SET FOREIGN_KEY_CHECKS=0');
}
$sql = file($sqlFile);
$query = '';
foreach ($sql as $line) {
$startWith = substr(trim($line), 0, 2);
$endWith = substr(trim($line), -1, 1);
if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {
continue;
}
$query = $query . $line;
if ($endWith == ';') {
try {
$result = mysqli_query($dbConnection, $query);
if (!$result) {
header("Location: " . $_SERVER['PHP_SELF'] . "?error='Error : " . mysqli_error($dbConnection));
exit;
}
} catch (Exception $e) {
header("Location: " . $_SERVER['PHP_SELF'] . "?error=Connection failed: " . urlencode($e->getMessage()));
exit;
}
$query = '';
}
}
mysqli_close($dbConnection);
header("Location: " . $_SERVER['PHP_SELF'] . "?success=Database imported");
exit;
}
function doSqlBackup($host, $user, $pass, $dbname)
{
preventUnAuthorized();
try {
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
header("Location: " . $_SERVER['PHP_SELF'] . "?error=Connection failed: " . $conn->connect_error);
exit;
}
} catch (Exception $e) {
header("Location: " . $_SERVER['PHP_SELF'] . "?error=Connection failed: " . urlencode($e->getMessage()));
exit;
}
$outsql = '';
$tables = array();
$sql = "SHOW TABLES";
$query = $conn->query($sql);
while ($row = $query->fetch_row()) {
$tables[] = $row[0];
}
foreach ($tables as $table) {
$outsql .= "DROP TABLE IF EXISTS `$table`;\n";
$sql = "SHOW CREATE TABLE $table";
$query = $conn->query($sql);
$createTable = $query->fetch_assoc()['Create Table'];
$outsql .= $createTable . ";\n";
$sql = "SELECT * FROM $table";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$keys = array_keys($row);
$values = array_map(function ($value) use ($conn) {
return $conn->real_escape_string($value);
}, $row);
$outsql .= "INSERT INTO `$table` (`" . implode('`,`', $keys) . "`) VALUES ('" . implode("','", $values) . "');\n";
}
$outsql .= "\n";
}
$backup_file_name = $dbname . '_' . date('Ymd_His') . '.sql';
$fileHandler = fopen($backup_file_name, 'w+');
fwrite($fileHandler, $outsql);
fclose($fileHandler);
return $backup_file_name;
}
if (!isset($_POST['full_backup']) && !isset($_POST['mysql'])) {
if (!empty($_GET['error'])) {
echo '<div class="alert alert-danger" role="alert">' . $_GET['error'] . '</div>';
}
if (!empty($_GET['success'])) {
echo '<div class="alert alert-success" role="alert">' . $_GET['success'] . '</div>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Backup Helper</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<style>
html,
body {
height: 100%;
}
.content-wrap {
min-height: 100%;
margin-bottom: -100px;
}
.footer {
height: 100px;
}
.alert {
position: absolute;
top: 30px;
left: 50%;
transform: translate(-50%, 0);
z-index: 1000;
}
</style>
</head>
<body>
<header>
<nav class="navbar navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#"><i class="fas fa-cloud-upload-alt"></i> Backup Helper</a>
<?php if (authorizeLogggedIn()): ?>
<a href="?logout=1" class="btn btn-danger"><i class="fas fa-sign-out-alt"></i> Logout</a>
<?php endif; ?>
</div>
</nav>
</header>
<div class="container mt-5">
<?php if (!authorizeLogggedIn()): ?>
<div class="row justify-content-center">
<div class="col-md-6">
<?php if (!$registerNew): ?>
<!-- Login form -->
<div class="card">
<div class="card-header"><i class="fas fa-sign-in-alt"></i> Login</div>
<div class="card-body">
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
<div class="form-group">
<label for="password">Password:</label>
<input type="password" name="password" id="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary"><i class="fas fa-sign-in-alt"></i>
Login</button>
</form>
</div>
</div>
<?php else: ?>
<!-- Register form -->
<div class="card">
<div class="card-header"><i class="fas fa-user-plus"></i> Register</div>
<div class="card-body">
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
<div class="form-group">
<label for="password">New Password:</label>
<input type="password" name="newpassword" id="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary"><i class="fas fa-user-plus"></i>
Register</button>
</form>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php else: ?>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card mt-3">
<div class="card-header"><i class="fas fa-cloud-upload-alt"></i> Backup</div>
<div class="card-body">
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
<!-- Database Credentials -->
<div class="form-group">
<label for="db_host">Database Host:</label>
<input type="text" name="db_host" value="<?= $set_dbhost ?>" id="db_host"
class="form-control" required>
</div>
<div class="form-group">
<label for="db_user">Database Username:</label>
<input type="text" name="db_user" id="db_user" value="<?= $set_dbuser ?>"
class="form-control" required>
</div>
<div class="form-group">
<label for="db_pass">Database Password:</label>
<input type="password" name="db_pass" value="<?= $set_dbpass ?>" id="db_pass"
class="form-control">
</div>
<div class="form-group">
<label for="db_name">Database Name:</label>
<input type="text" name="db_name" value="<?= $set_dbname ?>" id="db_name"
class="form-control" required>
</div>
<button type="submit" name="connect" class="btn mb-1 btn-primary"><i
class="fas fa-database"></i>
Connect</button>
<hr>
<!-- Backup Options -->
<button type="submit" name="mysql" class="btn mb-1 btn-success"><i
class="fas fa-database"></i>
Backup SQL file</button>
<button type="submit" name="full_backup"
onclick="javascript:alert('Wait, It may take some time')"
class="btn mb-1 btn-success"><i class="fas fa-cloud-upload-alt"></i> Backup all
files+SQL</button>
</form>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mt-3">
<div class="card-header"><i class="fas fa-file-archive"></i> Unzip</div>
<div class="card-body">
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
<?php
$zip_files = glob('*.zip');
if (count($zip_files) > 0): ?>
<div class="form-group">
<label for="zip_file">Select ZIP file to Unzip:</label>
<select name="zip_file" id="zip_file" class="form-control">
<?php
foreach ($zip_files as $file) {
echo "<option value=\"$file\">$file</option>";
}
?>
</select>
</div>
<button type="submit" class="btn btn-primary"><i class="fas fa-file-archive"></i>
Unzip</button>
<?php else: ?>
<p>No zip files found in the root directory</p>
<?php endif; ?>
</form>
</div>
</div>
<div class="card mt-3">
<div class="card-header"><i class="fas fa-database"></i> SQL Import</div>
<div class="card-body">
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
<?php
$sql_files = glob('*.sql');
if (count($sql_files) > 0): ?>
<div class="form-group">
<label for="zip_file">Select SQL file to Import:</label>
<select name="sql_file" id="sql_file" class="form-control">
<?php
foreach ($sql_files as $file) {
echo "<option value=\"$file\">$file</option>";
}
?>
</select>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" checked name="disableforeginkey"
id="foreginkey">
<label class="form-check-label mb-2" for="foreginkey">
Disable Foreign Key Constraints
</label>
</div>
<button type="submit" class="btn btn-primary"><i class="fas fa-database"></i>
Import</button>
<?php else: ?>
<p>No SQL files found in the root directory</p>
<?php endif; ?>
</form>
</div>
</div>
<div class="card mt-3">
<div class="card-header"><i class="fas fa-upload"></i> Upload</div>
<div class="card-body">
<form action="<?= $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="file">Choose File:</label>
<input type="file" name="file" id="file" class="form-control-file">
</div>
<button type="submit" class="btn btn-primary"><i class="fas fa-upload"></i> Upload</button>
</form>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
</br></br>
<footer class="footer mt-5 bg-dark py-3">
<div class="container">
<span class="text-muted">PHP BackUp Helper v1 <a href="https://github.com/captainerd"
target="_blank">Captainerd</a> - <a
href="https://github.com/captainerd/Backup-SQL-database-or-files"
target="_blank">Backup-SQL-database-or-files repository</a></span>
</div>
</footer>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
setTimeout(() => {
$(".alert").hide();
}, 4000);
</script>
</body>
</html>