1

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);
        }
    }
}
?>
2
  • What errors are you getting when running this script?? Commented Jul 10, 2015 at 19:13
  • your exec call is useless. mysql expects SQL as input, and you're feeing it some random text that happens to be a valid shell command. you DON'T need mysql for a dump. all it should be is mysqldump ... > dump.sql Commented Jul 10, 2015 at 19:14

1 Answer 1

2

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

mysqldump -h server –uuserdb  -ppass dbname > mydb_tab.sql
Sign up to request clarification or add additional context in comments.

3 Comments

This command generates the mydb_tab.sql but there is nothing inside it .
Im trying this one now but still nothing :( <?php $connection = ssh2_connect('server.hosting.com', 22); ssh2_auth_password($connection,'user', 'pass'); $stream = ssh2_exec($connection, 'mysqldump -h server.hosting.com -uuserdb -ppass dbname > mydb_tab.sql'); ?>
Go on ur sever and try it directly there before doing it trhough code look for errrors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.