我正试图制作一个带有两个按钮的简单页面,当按下其中一个按钮时,数据库将被转储(mysqldump
),而另一个按钮将被还原(导入)。
我会尝试在一个开发环境中这样做,我没有一个集中的服务器,有时我发现自己根据当天和情况在几台不同的电脑上工作,脚本本身(即代码)我只是与SpiderOak同步,我正试图让mysqldump在电脑之间做同样的事情。
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;
}
?>
restore.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 */
您也可以使用exec()
来代替shell_exec()
,就像前面提到的halfer一样。
它们之间的区别在于,使用shell_exec()
可以得到完整的输出,而使用exec()
只能得到输出的最后一行。