Take MySQL backup using php passthru function

In the last post i explained about taking the MySQL backup using php code , This post explains to take the backup of your MySQL database using the passthru function ( passthru documentation )

The passthru() function is similar to the exec() function in that it executes a command. This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser

$datestamp = date("Y-m-d"); // Present date to append in the filename for the identification
$dbuser = ""; // Database username
$dbpwd = ""; // Database password
$dbname = ""; // Database name. Use --all-databases if you have more than one
$filename= "backup-$datestamp.sql.gz"; // The name (and optionally path) of the dump file
$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

The SQL dump will generate and store in the default directory , You can set the full path in the $filename , Note this will work only in the locally hosted database server

One comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Everyone loves what you guys are usually up too.

    This type of clever work and reporting! Keep up
    the very good works guys I’ve added you guys to blogroll.