加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

用PDO PHP自动化mysqldump,有可能吗?

发布时间:2020-12-13 16:55:30 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用两个按钮创建一个简单的页面,当按下其中一个按钮时,数据库被转储( mysqldump)并且另一个被恢复(导入). 我会尝试在一个开发环境中这样做,我没有集中式服务器,有时我发现自己根据日期和情况从几台不同的PC工作,脚本本身(即代码)我只是同步使用Sp
我正在尝试使用两个按钮创建一个简单的页面,当按下其中一个按钮时,数据库被转储( 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>

backup.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 */

而不是shell_exec(),您也可以像提到的halfer一样使用exec().

它们之间的区别在于,您使用shell_exec()获得完整输出,而使用exec()只能获得输出返回的最后一行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读