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