Implementing Data Backup and Recovery with PHP
This tutorial demonstrates how to use PHP to create a backup directory, export MySQL tables to text files, and restore them by reading the files and executing INSERT statements, providing complete code examples for both backup and recovery processes.
In the modern technology era, data backup and recovery functions are essential. Both personal and enterprise users need to ensure the safety and reliability of important data. PHP, a popular server‑side scripting language, is flexible, efficient, and easy to learn, making it widely used for data management and processing. This article explains how to develop data backup and recovery features using PHP and provides corresponding code examples.
1. Data Backup
Create Backup Folder
First, we need to create a folder to store backup files. In PHP we can use the mkdir() function to create the directory. Below is an example:
<code>$folderName = 'backup';
if (!file_exists($folderName)) {
mkdir($folderName, 0777, true);
echo "文件夹创建成功!";
} else {
echo "文件夹已存在!";
}
</code>Database Table Backup
Next, we back up the tables in the database. Using the mysqli extension we connect to the database and retrieve data with a SELECT statement, then save the results to a text file. Example code:
<code>$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// Create database connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// Query table
$sql = "SELECT * FROM your_table";
$result = $conn->query($sql);
// Save result to file
$fileName = 'backup/your_table_backup.txt';
$file = fopen($fileName, 'w');
while ($row = $result->fetch_assoc()) {
fwrite($file, implode(',', $row) . "\n");
}
fclose($file);
$conn->close();
echo "数据备份成功!";
</code>2. Data Recovery
Database Table Restoration
To restore, we read the backup file and insert the data back into the database using INSERT statements. Example code:
<code>$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// Create database connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// Read backup file
$fileName = 'backup/your_table_backup.txt';
$file = fopen($fileName, 'r');
// Insert backup data
while (!feof($file)) {
$rowData = fgets($file);
$rowData = trim($rowData);
if (!empty($rowData)) {
$sql = "INSERT INTO your_table VALUES (" . implode(',', explode(',', $rowData)) . ")";
$conn->query($sql);
}
}
fclose($file);
$conn->close();
echo "数据恢复成功!";
</code>Conclusion
Through the examples above, we learned how to develop data backup and recovery functions with PHP. Backup allows periodic saving of important database data to text files, ensuring safety and reliability, while restoration reads those files and reinserts the data, enabling data recovery and reconstruction.
References
PHP official documentation: https://www.php.net/
MySQL official documentation: https://dev.mysql.com/doc/
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.