What are some common methods for creating backups in PHP, specifically for databases like MySQL?

Creating backups for databases like MySQL in PHP can be done using various methods such as using the mysqldump command, using PHP functions to export data, or creating a script to automate the backup process. One common approach is to use the mysqldump command to export the database structure and data into a SQL file.

// Set the database credentials
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db_name = 'database_name';

// Set the backup location
$backup_file = 'backup.sql';

// Create the command to export the database
$command = "mysqldump --host=$host --user=$user --password=$pass $db_name > $backup_file";

// Execute the command
system($command);