The PHP system()
function is used to execute an external program. Because MySQL already has a built-in tool for creating MySQL database backups, mysqldump
, let’s use it from our PHP script:
<?php include 'config.php'; include 'opendb.php'; $backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz'; $command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile"; system($command); include 'closedb.php'; ?>
The following post offers an alternative to this strategy: How to use PHP To Backup & Restore a MySQL Database Table