使用php通过ssh备份mysql数据库

时间:2015-07-10 19:07:37

标签: php mysql ssh

我尝试使用PHP通过SSH备份mysql数据库。 我通过ssh建立了ssh连接,但我没有取得任何进展 用数据库备份。 这是我的代码:

<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");

if(!($con = ssh2_connect("server.hosting.com", 22))){
    echo "fail: unable to establish connection\n";
} else {

    if(!ssh2_auth_password($con, "user", "password")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";




        if (!($stream = ssh2_exec($con, 'echo "mysqldump -u userdb -p pass

dbname tablename > mydb_tab.sql"|mysql'))) {
            echo "fail: unable to execute command\n";
        } else {
            // collect returning data from command
            stream_set_blocking($stream, true);
            $data = "";
            while ($buf = fread($stream,4096)) {
                $data .= $buf;
            }
            fclose($stream);
        }
    }
}
?>

1 个答案:

答案 0 :(得分:2)

您的命令不正确,应如下所示:

mysqldump -h server –uuserdb  -ppass dbname > mydb_tab.sql