What are the potential pitfalls of using the 'system' function in PHP for tasks like database backups?

Using the 'system' function in PHP for tasks like database backups can pose security risks as it allows for arbitrary commands to be executed on the server. To mitigate this risk, it is recommended to use PHP's built-in functions for interacting with the database or a secure library specifically designed for database backups.

// Example of using PHP's built-in functions for database backups
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Backup database
$backup_file = 'backup.sql';
exec("mysqldump --user=$username --password=$password --host=$servername $dbname > $backup_file");

// Close connection
$conn->close();