1

I am trying to restore a mysql database using php. I googled and found out this piece of code :-

<?php

$restore_file  = "backup.bkp";
$server_name   = "localhost";
$username      = "root";
$password      = "pass";
$database_name = "db";

$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name}  
<  $restore_file";
exec($cmd);

?>

I am using a shared server (linux) and the file was restored using phpmyadmin successfully. But I want to use php to restore. The code does not work. Where am I being wrong?

5
  • Make echo $cmd; and run the printed string in console. Most probably the problem is in new line symbol before < (if, of course, it is not there and you made it to format the code in question). Or path to $restore_file is invalid. Commented Oct 1, 2014 at 4:16
  • @Cheery , yes I made it to format code in question Commented Oct 1, 2014 at 4:17
  • 1
    If it's a shared server it is quite likely that your scripts will not have permission to use exec. Make sure your error_reporting is set to E_ALL and let us know what messages you are getting. Commented Oct 1, 2014 at 4:17
  • yeah..i set the error_reporting but no error Commented Oct 1, 2014 at 4:19
  • Can you get a PHP info? (phpinfo()) Commented Oct 1, 2014 at 4:22

3 Answers 3

2

Thanks to @Scoopy, I found that exec() was disabled my by hosting provider. However, I have solved my problem using the following code I found here :-

$db = mysql_connect ( 'localhost', 'username', 'pass' ) or die('not connected');
mysql_select_db( 'test', $db) or die('Not found');
$fp = fopen ( 'backup-file', 'r' );
$fetchData = fread ( $fp, filesize ( 'backup-file') );
$sqlInfo = explode ( ";\n", $fetchData); // explode dump sql as a array data    

foreach ($sqlInfo AS $sqlData )
{
mysql_query ( $sqlData ) or die('Query not executed');
}

BUT, THE CODE IS DEPRECATED. So, I made up this code :-

$host = 'localhost'; 
$user = 'root';
$password = 'root';
$database = 'test';

$conn = new mysqli($host, $user, $password, $database);
$conn->autocommit(FALSE);

$fp = fopen('bkp-file', 'r');
$fetchData = fread($fp, filesize('bkp-file'));
$sqlInfo = explode(";\n", $fetchData); // explode dump sql as a array data

foreach ($sqlInfo AS $sqlData) {
    $conn->query($sqlData);
}
$conn->commit();
$conn->close();

This code is perfect to use. It uses transactions to prevent multiple writes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much, this code works perfectly. though I need some help on how to restore to a different schema from the original one. Thanks again
1

Generally a shared hosting server will disable access to system functions as it introduces a security risk for them. You can confirm this if your server provider allows you to use the phpinfo command (docs).

Create a page that dumps the PHP info and then look on that page for a setting called disable_functions - it's under the "Core" section. If exec appears there then you will know that exec is a disabled function and you will not be able to use it. Unfortunately one of the drawbacks of using some shared hosting services as it is impossible/very difficult to implement the sorts of things you are talking about.

1 Comment

Sorry..had to remove the tick.coz had to show how i solved the problem..thus ticking my own answer (in the next 2 days) but +1 for your answer
0

I don't see why you need PHP, you can do this directly in the terminal.

Backup

mysqldump -u root -p db> backup.bkp

Restore

mysql -u root -p db < backup.bkp

2 Comments

I do not have access to the terminal
what do you have access to, how do you access the PHP server? you can still hardcode this command in your php if you have no choice. you can omit the password

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.