我想备份我的本地数据库并从实时网站导入该数据库。我尝试了一些东西。请看下面..
我的备份代码
<?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 中,我成功地测试了这个。对于我的实时站点,我认为文件路径检测不正确。我对此一无所知。请帮我。谢谢。