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

Backup mysql database through ssh using php

我正在尝试使用 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);
        }
    }
}
?>

你的命令不正确,应该是这样的:

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