我试图用两个按钮制作一个简单的页面,当按下其中一个按钮时,数据库被转储(mysqldump
),另一个被恢复(导入)。
我会尝试在一个开发环境中这样做,我没有集中式服务器,有时我发现自己根据日期和情况在多台不同的 PC 上工作,脚本本身(即代码)我只是与 SpiderOak 同步,我正在尝试让 mysqldump 在 PC 之间执行相同的操作。
index.html
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>Herramienta Back Up/Restore DataBase ;)</title>
<link rel="stylesheet" type="text/css" href="Babifu.css">
</head>
<body>
<center>
<img class="thumb" src="http://lorempixel.com/g/450/370/nightlife/"/><br /><br />
<form id="me1" action="backup.php" method="post"><br /><br /><br />
<input type="submit" value="Backup database" />
</form>
<form id="me2" action="restore.php" method="post"><br /><br /><br /><br /><br /><br />
<input type="submit" value="Importar" />
</form>
<div class="clear"></div>
</center>
</body>
</html>
备份.php
<?php
include('conection.php');
try {
$qls = "mysqldump --default-character-set=UTF8 --single-transaction -u root -p[Paswordofthedatabase] u739462586_dtren > restoresync.sql";
$stmt = $con->prepare($qls);
$rslt = $stmt->execute();
echo "Base de Datos Salvadas Correctamente";
} catch(PDOException $e) {
echo $e->getMessage;
}
?>
恢复.php
<?php
include('conection.php');
try {
$qls = "mysql -u root -p[Paswordofthedatabase] < restoresync.sql";
$stmt = $con->prepare($qls);
$rslt = $stmt->execute();
echo"Base de Datos Restaurados/Importados Correctamente";
} catch(PDOException $e) {
echo $e->getMessage;
}
?>
问题是,它不工作,控制台不显示任何错误消息,PHP 也是如此。
甚至可以通过查询来做到这一点吗?我该怎么做?这段代码有什么问题?
有什么方法可以确保在特定文件夹上创建备份?如有任何建议、意见、澄清请求、问题或答案,我们将不胜感激。
最佳答案
这些不是 MySQL 查询,而是您通过终端执行的命令。 使用它来执行命令并获得完整输出:
$dump_output = shell_exec("mysqldump --opt --default-character-set=UTF8 --single-transaction --protocol=TCP -u --user=root --password=dbpassword --host=localhost DB_NAME > restoresync.sql");
echo $dump_output; /* Your output of the dump command */
$restore_output = shell_exec("mysql -u root -p[Paswordofthedatabase] < restoresync.sql");
echo $restore_output; /* Your output of the restore command */
除了 shell_exec()
,您还可以使用 exec()
,如 halfer 所述。
它们之间的区别在于,使用 shell_exec()
可以获得完整的输出,而使用 exec()
则只能获得输出的最后一行。
关于php - 使用 PDO PHP 自动化 mysqldump,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485287/