Welcome to the PHP Database Backup Tool project! In this tutorial, we'll guide you through creating a simple yet powerful tool to backup your database, helping you safeguard your data.
π Note: Backing up your database is crucial to prevent data loss due to unforeseen circumstances like server crashes or human errors.
database_backup.php in your project directory.π― Tip: Establishing a connection to the database is the first step.
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>Replace your_username, your_password, and your_database with your actual database credentials.
π‘ Pro Tip: Our backup function will create a SQL dump file containing your database structure and data.
function create_backup($filename = 'backup.sql') {
global $conn;
// Create a temporary MySQL dump
$sql_dump = 'SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = "' . $conn->database . '"
AND TABLE_TYPE = "BASE TABLE"
INTO OUTFILE "' . $filename . '"
FROM MYSQL
WHERE TABLE_NAME IN (
SELECT table_name
FROM information_schema.tables
WHERE table_schema = "' . $conn->database . '"
)';
// Execute the query and check for errors
if (!$result = $conn->query($sql_dump)) {
die('Error creating backup file: ' . $conn->error);
}
}π Note: Call the create_backup function to run the backup.
create_backup();π― Tip: The backup file will be created in the same directory as your PHP script.
// Send headers to force a file download
header('Content-Type: text/sql');
header('Content-Disposition: attachment; filename=backup.sql');
header('Content-Length: ' . filesize('backup.sql'));
header('Cache-Control: public');
header('Pragma: public');
readfile('backup.sql');
exit;Now, combine the connection, backup function, and download sections in database_backup.php:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function create_backup($filename = 'backup.sql') {
global $conn;
// Create a temporary MySQL dump
$sql_dump = 'SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = "' . $conn->database . '"
AND TABLE_TYPE = "BASE TABLE"
INTO OUTFILE "' . $filename . '"
FROM MYSQL
WHERE TABLE_NAME IN (
SELECT table_name
FROM information_schema.tables
WHERE table_schema = "' . $conn->database . '"
)';
// Execute the query and check for errors
if (!$result = $conn->query($sql_dump)) {
die('Error creating backup file: ' . $conn->error);
}
}
create_backup();
// Send headers to force a file download
header('Content-Type: text/sql');
header('Content-Disposition: attachment; filename=backup.sql');
header('Content-Length: ' . filesize('backup.sql'));
header('Cache-Control: public');
header('Pragma: public');
readfile('backup.sql');
exit;What does the `create_backup` function do in this script?
With this PHP Database Backup Tool, you can now effortlessly backup your database, ensuring your data remains safe and secure! β
Happy coding! π