1

I'm writing a cake-shell-script that connects to a server via ssh and then dumps some mysql-tables.

This part is all ok, and the dumps are created; but because there are more than one host from which I want to collect the dumps, there has to be the host in the filename.

And here, the strange thing (which even none of my co-worker and instructors can explain...) happens:

There is a Variable, $host by name, that contains the url of the host, and this should prefix the filename ... But in the second call of GetDump() ... this variable is empty.

Here is my code:

use Cake\Console\Shell;

class DatabaseBackupShell extends Shell
{

    private $ssh_connection = null;
    private $host = null;

    public function getOptionParser()
    {
        $parser = parent::getOptionParser();

        return $parser;
    }

    public function main() 
    {
        $this->host = 'canopus.uberspace.de';

        $this->connect($this->host,'22','tsuki','~/.ssh/id_rsa.pub','~/.ssh/id_rsaNOPASSWORD');

        $databases = ['tsuki_testdatenbank','tsuki'];

        foreach ($databases as $database) {
            $this->getDump($database);
        }
    }

    public function connect($ssh_host, $ssh_port, $ssh_auth_user, $ssh_auth_public_key, $ssh_auth_private_key) 
    {
        $this->ssh_connection = ssh2_connect($ssh_host, $ssh_port);
        ssh2_auth_pubkey_file($this->ssh_connection, $ssh_auth_user, $ssh_auth_public_key, $ssh_auth_private_key); 

        echo 'Connected to server '.$ssh_host.' on port '.$ssh_port.' as '.$ssh_auth_user."\n";
    }

    public function getDump($database) 
    {

            $command = sprintf('mysqldump --add-drop-table --databases %1$s > ~/meep/%2$s__%1$s__%3$s;', $database, $this->host, date("Y-m-d"));

            ssh2_exec($this->ssh_connection, $command);
            echo $command . "\n";
    }
}

And here is the output: bin/cake database_backup

Welcome to CakePHP v3.3.8 Console
---------------------------------------------------------------
App : src
Path: /var/www/html/cake-cli/src/
PHP : 7.0.8-0ubuntu0.16.04.3
---------------------------------------------------------------
Connected to server canopus.uberspace.de on port 22 as tsuki
mysqldump --add-drop-table --databases tsuki_testdatenbank > ~/meep/canopus.uberspace.de__tsuki_testdatenbank__2016-11-21;
mysqldump --add-drop-table --databases tsuki > ~/meep/__tsuki__2016-11-21;

As you can See, the host (canopus.uberspace.de) is not there in the second call. Even if I'd reassign $host DIRECTLY before the assignement of $command ... it just stays empty in the second call.

P.S.: The library for ssh2 is installed via "apt-get install php-ssh2" on Linux Mint 18 64bit

Dennis Richter
  • 522
  • 6
  • 16
  • 1
    Where specifically is it empty? I see it is used in `main()` - where else is it used? Is this where it is empty? – WillardSolutions Nov 21 '16 at 16:00
  • @EatPeanutButter In the second call of GetDump(), the $command doesn't include the variable (see the output, expected output in last line should be mysqldump --add-drop-table --databases tsuki > ~/meep/canopus.uberspace.de__tsuki__2016-11-21;) – Dennis Richter Nov 21 '16 at 16:05
  • Is it passed into your `connect()` method correctly? It is a property of the class, so you don't really need to pass it as a parameter to `connect()`; can you access it from the method directly? – WillardSolutions Nov 21 '16 at 17:11
  • @EatPeanutButter It is passed correctly to the connect() method, as the documentation says at [DOKU:ssh2_connect()](http://php.net/manual/de/function.ssh2-connect.php) & [DOKU:ssh2_auth_pubkeyfile()]](http://php.net/manual/de/function.ssh2-auth-pubkey-file.php). The Connection is ok, the files are created at the remote host like expected (if you expect the missing $host path in the filename, like the echo on the client-side shows) – Dennis Richter Nov 22 '16 at 11:49

0 Answers0