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

Default image
Vignesh A Sathiyanantham
AWS Certified developer with 8 years of extensive experience working for Cloud and Datacenter solutions and leading a small team in a lean startup based on Bangalore, India

2 Comments

  1. http://takomaparkpediatrics.com/buy/levitra-online-prescription.php
    http://takomaparkpediatrics.com/buy/levitra-online-prescription.php

    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.

  2. […] done very easily using php. In Last posts i explained how to take the backup of Mysql using php http://asvignesh.in/take-mysql-backup-using-php-passthru-function […]

Leave a Reply

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