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

<?php
$datestamp = date("Y-m-d"); // Present date to append in the filename for the identification
/* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
$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

  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.

Leave a Reply

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

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading