php - 使用 PDO PHP 自动化 mysqldump,可能吗?

标签 php mysql apache pdo mysqldump

我试图用两个按钮制作一个简单的页面,当按下其中一个按钮时,数据库被转储(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/

相关文章:

php - 如何防止 json_encode() 删除包含无效字符的字符串

php - 如何将 php 变量传递到 Windows 批处理文件

mysql - 尝试编写 MySQL 连接查询

apache - 如何在使用 mod_proxy 的同时将用户发送到 Apache 中的另一个 "site"?

Java 程序从 Excel 文件读取数据抛出错误..需要帮助解决

java - 无法转换为相同类型的不同 jar(Apache FOP、URL ClassLoader)

php - 计算特定值在数组中出现的频率

php - 生成自定义订单号

mysql - 来自已知点查询的 SQL 位置

php - 如何从 MySQL 表中获取行号?