I'm trying to use an SSH connection through PHP to run Bash scripts.
I wrote a script to make backups and restores for a MySQL database and I am still testing to achieve this. I have encountered a problem while trying to run two different simple commands. My code is:
<?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, "username", "password")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
if (!($stream = ssh2_exec($con, "cd directory " ))) {
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);
}
if (!($stream = ssh2_exec($con, "mkdir directiry2" ))) {
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);
}
}
}
?>
It still creates another directory but not inside the "directory"! Please help!!!
mkdir -p directory/directiry2? You don't need to cd into a dir to make something inside it.mysqldump -u... -p... mydb t1 t2 t3 > mydb_tables.sql