This example of a PHP script creates a backup of a MySQL database using the mysqldump command:

 

<?php

// https://www.websitefunctions.com

// MySQL database configuration

$host = 'localhost';

$username = 'your_username';

$password = 'your_password';

$database = 'your_database';

// Backup file name and path

$backupPath = '/path/to/backup/directory';

$backupFile = $backupPath . '/' . $database . '_' . date('Y-m-d_H-i-s') . '.sql';

// Create backup command

$command = "mysqldump -h $host -u $username -p$password $database > $backupFile";

// Execute the backup command

system($command, $result);

// Check if backup was successful

if ($result === 0) {

    echo "Database backup created successfully.";

} else {

    echo "Error creating database backup.";

}

?>

Make sure to replace the following placeholders with your actual database credentials and backup path:

  • your_username: Replace with your MySQL username.
  • your_password: Replace with your MySQL password.
  • your_database: Replace with the name of the database you want to back up.
  • /path/to/backup/directory: Replace with the path to the directory where you want to store the backup file.

Save this script as a .php file (e.g., backup.php) and run it in a web browser or execute it from the command line using PHP.

Please note that it's important to secure this script and restrict access to it, as it contains sensitive database credentials. Additionally, consider implementing proper authentication and authorization mechanisms to ensure the security of your backups.