What are some common methods for backing up a database in PHP?

Backing up a database in PHP is essential to prevent data loss in case of unexpected events. One common method is to use the `mysqldump` command to export the database structure and data into a SQL file. This file can then be stored securely for future restoration if needed.

<?php
// Set database credentials
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database_name';

// Set backup filename
$backup_file = 'backup.sql';

// Execute mysqldump command to backup the database
exec("mysqldump --user={$user} --password={$pass} --host={$host} {$db} > {$backup_file}");

echo "Database backup has been created successfully.";
?>