Luarb Balla
Luarb Balla

Reputation: 169

Backup mysql database through ssh using php

I'm trying to backup mysql database through SSH using PHP. I have made the ssh connection through ssh but I'm not making any progress with the database backup. This is my code :

<?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);
        }
    }
}
?>

Upvotes: 1

Views: 949

Answers (1)

meda
meda

Reputation: 45490

Your command is not correct, it should read like this:

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

Upvotes: 2

Related Questions