PHP mysqldump文件导入

时间:2013-09-01 15:23:18

标签: php mysql

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

我的备用码

<?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中,我成功测试了这个。对于我的实时网站,我认为文件路径无法正确检测。我不知道这个。请帮我。感谢。

1 个答案:

答案 0 :(得分:2)

假设现场网站位于ip 134.122.12.109 你正在使用UNIX

在本地主机上

创建这些文件和~/backupdir目录

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

现在您可以通过运行./backup.sh

备份您的localhost

您可以使用./restore.sh

恢复实际网站 希望表明这个想法。


回复您的评论

让restore_database.php为

<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
system("mysql -h $dbhost -u $dbuser --password='$dbpass' $dbname < $filename");
?>