A basic example of backing up a MySQL database using mysqldumb and exec.
You may need to allow PHP to use the exec() function. Run phpinfo() to check if exec() is enabled.
This code sample uses a .env files as described on the php-env repo.
For information on what privileges the MySQL user needs to execute the mysqldump command check out this Stack Overflow question: https://dba.stackexchange.com/questions/55546