1

我想备份我的本地数据库并从实时网站导入该数据库。我尝试了一些东西。请看下面..

我的备份代码

<?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>

我的 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 = '';
    }
}
?>

在我的 localhost 中,我成功地测试了这个。对于我的实时站点,我认为文件路径检测不正确。我对此一无所知。请帮我。谢谢。

4

1 回答 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");
?>
于 2013-09-01T15:46:09.060 回答