MySQL Database restore

I have already listed the PHP script to generate backup of database in one of my earlier post.
I am listing here the PHP script to restore database from mysql database file.

/* Database Restore */
$username = "dbusername";  //database username
$password = 'dbpass';   //database password
$hostname = "localhost";  //host
$dbname = "dbname"; //database name to be restored
$sConnString = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");

$restore_from = '/pathtodirectory/db.sql.gz'; //give full path of the directory where the your mysql database file is stored, this can be zip,gzip or sql file

//overwrite $dbname database with database backup stored at  $restore_from
$command = "gunzip < $restore_from | -u$username -p$password $dbname"; //this is for gzip file

if(system($command) === FALSE)
	echo "<br/>Failed to Restore database.";
	echo "<br/>Database Restored successfully.";


2 thoughts on “MySQL Database restore

    1. $command = “mysql -u $username -p $password $dbname < $restore_from"; Please test this once with sample DB, don't try on production environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top