[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.