-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackups.php
98 lines (76 loc) · 2.77 KB
/
backups.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
<?php
/* ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); */
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Check if the user is logged in
if (isset($_COOKIE['logged_in_' . DB_NAME]) && $_COOKIE['logged_in_' . DB_NAME] == 'true') {
header('Location: login.php');
exit;
}
include 'config.php';
include 'db.php'; // Include the database connection file
// Paths and filenames
$backup_dir = 'backups';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Only run the backup creation when the form is submitted
$backup_file = $backup_dir . '/' . DB_NAME . '_' . date('Y-m-d_H-i-s') . '.sql';
$zip_file = $backup_dir . '/' . DB_NAME . '_' . date('Y-m-d_H-i-s') . '.zip';
// Create backups directory if not exists
if (!file_exists($backup_dir)) {
mkdir($backup_dir, 0755, true);
}
// Step 1: Dump the database into a SQL file
$command = "mysqldump --user=" . DB_USER . " --password=" . DB_PASS . " " . DB_NAME . " > $backup_file";
$output = null;
$return_var = null;
exec($command, $output, $return_var);
// Check if the command was successful
if ($return_var !== 0) {
/* echo "<!-- $command -->"; */
die("Failed to execute mysqldump: " . implode("\n", $output));
}
// Step 2: Create a zip file containing the SQL dump
$zip = new ZipArchive();
if ($zip->open($zip_file, ZipArchive::CREATE) === TRUE) {
$zip->addFile($backup_file, basename($backup_file));
$zip->close();
// Remove the SQL file after creating the zip
unlink($backup_file);
} else {
die('Failed to create ZIP file');
}
// Step 3: Force download the ZIP file
if (file_exists($zip_file)) {
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=' . basename($zip_file));
header('Content-Length: ' . filesize($zip_file));
ob_clean(); // Clean the output buffer
flush(); // Flush the system output buffer
readfile($zip_file);
unlink($zip_file); // Optionally, delete the zip file after download
exit;
} else {
die('Failed to create backup');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Backup Database</title>
<link rel="stylesheet" href="styles/styles.css?id=V7"> <!-- Link to your stylesheet if any -->
</head>
<body class="<?php echo IS_DEMO ? 'demo-mode' : ''; ?>">
<h1>Backup Database</h1>
<p>Click the button below to download a backup of the database.</p>
<form method="post">
<button type="submit">Download Backup</button>
</form>
<?php include 'templates/footer.php'; ?>
</body>
</html>