/** * @brief get content of a file * @param GET `path` * @return */ public function content() { $params = $this->input->post(); if (empty($params) || !isset($params['id']) || !isset($params['host']) || empty($params['host']) || !isset($params['login']) || empty($params['login']) || !isset($params['password']) || empty($params['password'])) { echo json_encode(array('result' => 'error', 'error' => 'missing parameters')); return; } // get the parameter $filePath = $params['id']; $filePath = str_replace('_SEP_', '/', $filePath); // connect to remote host $ssh = new ssh($params['host'], $params['login'], $params['password']); // if params['content'] was supplied, save the file, otherwise get the file if (isset($params['content'])) { file_put_contents($this->localTempFile, $params['content']); $result = $ssh->upload($this->localTempFile, $filePath); if ($result === false) { echo json_encode(array('result' => 'error', 'error' => 'cannot upload file')); return; } echo json_encode(array('result' => 'success')); } else { // download the file to temp local file $result = $ssh->download($filePath, $this->localTempFile); if ($result === false) { echo json_encode(array('result' => 'error', 'error' => 'cannot get remote file')); return; } echo json_encode(array('result' => 'success', 'content' => file_get_contents($this->localTempFile))); } }
<?php /** * Simple script for backup MySQL database on remote server */ include "ssh.class.php"; $server_host = "ssh.example.com"; $server_login = "******"; $server_password = "******"; // Didn't use simple passwords $mysql_user = "******"; $mysql_password = "******"; // Didn't use simple passwords $mysql_database = "base"; // Connect: $ssh = new ssh($server_host, $server_login, $server_password); // Make backup: $ssh("mysqldump -u {$mysql_user} -p{$mysql_password} {$mysql_database} > /tmp/bases", "gzip -9 /tmp/bases"); // Download backup: $ssh->download("/tmp/bases.gz", "/my/backups/dir/bases.gz"); // Delete backup on server $ssh("rm /tmp/bases.gz");