Current Location: Home> Latest Articles> How to Implement Automated Data Backup and Recovery in PHP

How to Implement Automated Data Backup and Recovery in PHP

M66 2025-06-20

How to Implement Automated Data Backup and Recovery in PHP

When developing and maintaining web applications, data backup and recovery functionality is crucial. Whether due to data loss, unexpected failures, or system crashes, timely backup and recovery mechanisms ensure data security and help the system quickly restore. This article will explain how to implement automated data backup and recovery in PHP, with detailed code examples.

Data Backup

Data backup refers to copying databases or other critical data to a safe storage location to prevent data loss. Below are the steps to implement data backup:

  1. Connect to the Database: First, use PHP's database extension to connect to the target database.
$db_host = 'localhost';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'database_name';

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die("Database connection failed: " . mysqli_connect_error());
}
    
  1. Get the List of Tables: Execute the SHOW TABLES statement to get all tables in the database.
$tables = mysqli_query($conn, "SHOW TABLES");
    
  1. Create a Backup Folder: Create a folder to store backup files.
$backup_folder = '/path/to/backup/folder';
if (!file_exists($backup_folder)) {
    mkdir($backup_folder, 0777, true);
}
    
  1. Loop to Backup Tables: Use the `mysqldump` command to back up each table and save the backup files in the designated folder.
while ($table = mysqli_fetch_row($tables)) {
    $table_name = $table[0];
    $output_file = $backup_folder . '/' . $table_name . '-' . date('Y-m-d-H-i-s') . '.sql';
    $command = "mysqldump -h {$db_host} -u {$db_user} -p{$db_pass} {$db_name} {$table_name} > {$output_file}";
    system($command, $output);
}
    
  1. Close the Database Connection: Once the backup is completed, close the database connection.
mysqli_close($conn);
    

Data Recovery

Data recovery refers to importing backup data back into the database to restore the system to the state it was in when the backup was made. Below are the steps to implement data recovery:

  1. Connect to the Database: Similar to the backup process, first, establish a database connection.
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die("Database connection failed: " . mysqli_connect_error());
}
    
  1. Get the List of Backup Files: Retrieve all backup files from the backup folder.
$backup_files = glob($backup_folder . '/*.sql');
    
  1. Loop to Restore Backup Data: Use the `mysql` command to import data from the backup files into the database.
foreach ($backup_files as $backup_file) {
    $command = "mysql -h {$db_host} -u {$db_user} -p{$db_pass} {$db_name} < {$backup_file}";
    system($command, $output);
}
    
  1. Close the Database Connection: After completing the restoration, close the database connection.
mysqli_close($conn);
    

In conclusion, using PHP's database extension and system commands, we can easily implement automated data backup and recovery. By running backup scripts periodically, we ensure data security and prevent data loss or system failures. Additionally, developers can customize backup and recovery strategies based on specific needs to meet various use cases.