1

I want backup my local database and import that database from live website. I'm tried something. please see below..

My Backup code

<?php
$dbhost = "localhost"; 
$dbuser = "root";  
$dbpass = 'password'; 
$dbname = "database"; 

$backupfile ='database.sql';
$backupdir = dirname(__FILE__);
$source = $backupdir.'/'.$backupfile;
system("mysqldump -h $dbhost -u $dbuser --password='$dbpass' $dbname  > $backupfile");

?>
<form action="http://www.example.com/restore_database.php" method="post">
    <input type="text" name="backup_file" value="<?php echo $source; ?>"/>
    <input type="submit" />
</form>

my restore_database.php (example.com/restore_database.php)

<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];

mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_select_db($dbname) or die('Error selecting MySQL database: ' . mysql_error());

$templine = '';
$lines = file($filename);
foreach ($lines as $line) {
    if (substr($line, 0, 2) == '--' || $line == '')
        continue;
    $templine .= $line;
    if (substr(trim($line), -1, 1) == ';') {
        mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
        $templine = '';
    }
}
?>

In my localhost , I'm successfully tested this. for my live site I think file path not detect correctly. I have no idea about this. please help me. Thanks.

7
  • why don't you use a shell script instead of leaving these URLS (example.com/restore_database.php) open to the users to play with ... on your server add the simple scripts to backup/restore or add a script on your local host to connect to the remote server and backup/restore. Commented Sep 1, 2013 at 15:27
  • Sorry , I'm new to php. can you show an example... Commented Sep 1, 2013 at 15:31
  • ok can you tell me if you want to have the backup on the local or live server ? Commented Sep 1, 2013 at 15:32
  • i have an localhost appliaction. i want backup that database and restore to my live site database. Commented Sep 1, 2013 at 15:33
  • so you have access to the live site ... and you want to backup the data on a directory on the live site or on a directory on your localhost ? Commented Sep 1, 2013 at 15:37

1 Answer 1

2

assuming that the live site lay on the ip 134.122.12.109 and you are using UNIX

on your local host create these files and ~/backupdir directory

backup.sh

#!/bin/bash
mysqldump -uusername -pPassword database > ~/backupdir/backupFile.sql

restore.sh

#!/bin.bash
mysql -h 134.122.12.109 -uliveSiteUsername -pLiveSitePassword database < ~/backupdir/backupFile.sql

now you can backup your localhost by running ./backup.sh

and you can restore the live site by using ./restore.sh

hope that demonstrated the idea.


responding to your comment

let the restore_database.php be

<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
system("mysql -h $dbhost -u $dbuser --password='$dbpass' $dbname < $filename");
?>
Sign up to request clarification or add additional context in comments.

2 Comments

1) my website hosted in shared ip. 2) my application run on wamp 3)in my question method , i just want one click. all other process will do jquery.
in that case i suggest you go with the scripts method... and since you run windows the you will have to know where mysql directory is. and where mysqldump directory is. they are probably in 'C:\wamp\bin\mysql\mysql5.1.33\bin\mysqldump.exe' and in 'C:\wamp\bin\mysql\mysql5.1.33\bin\mysql.exe'. so you have to run C:\wamp\bin\mysql\mysql5.1.33\bin\mysqldump.exe -uusername -pPassword database > file.sql to backup and run C:\wamp\bin\mysql\mysql5.1.33\bin\mysql.exe -h 134.122.12.109 -uliveSiteUsername -pLiveSitePassword database < file.sql to restore from the cmd yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.