/** * Executes the PullDbViaPhpMyAdmin Task. * * @return Robo\Result */ public function run() { // First lets login to the phpMyAdmin Server, if not already. if ($this->loggedIn == null) { $result = $this->taskPhpMyAdminLogin()->phpMyAdminUrl($this->phpMyAdminUrl)->phpMyAdminUser($this->phpMyAdminUser)->phpMyAdminPass($this->phpMyAdminPass)->remoteDbHost($this->remoteDbHost)->run(); if (!$result->wasSuccessful()) { throw new RuntimeException('Failed to Login!'); } $this->loggedIn = $result->getTask(); } // Get a list of tables from phpMyAdmin $result = $this->taskPhpMyAdminListTables($this->loggedIn)->remoteDbName($this->remoteDbName)->run(); if (!$result->wasSuccessful()) { throw new RuntimeException('Failed to get list of tabels!'); } $tables = $result->getTask()->getTables(); // Get sql dump $this->printTaskInfo('Downloading sql dump.'); $sql = $this->loggedIn->getClient()->post('export.php', ['form_params' => ['db' => $this->remoteDbName, 'server' => $this->loggedIn->getServerId(), 'token' => $this->loggedIn->getToken(), 'export_type' => 'database', 'export_method' => 'quick', 'quick_or_custom' => 'quick', 'template_id' => '', 'what' => 'sql', 'structure_or_data_forced' => 0, 'table_select' => $tables, 'table_structure' => $tables, 'table_data' => $tables, 'output_format' => 'sendit', 'filename_template' => '@DATABASE@', 'remember_template' => 'on', 'charset_of_file' => 'utf-8', 'compression' => 'none', 'maxsize' => '', 'sql_include_comments' => 'something', 'sql_header_comment' => '', 'sql_compatibility' => 'NONE', 'sql_structure_or_data' => 'structure_and_data', 'sql_create_table' => 'something', 'sql_auto_increment' => 'something', 'sql_create_view' => 'something', 'sql_procedure_function' => 'something', 'sql_create_trigger' => 'something', 'sql_backquotes' => 'something', 'sql_type' => 'INSERT', 'sql_insert_syntax' => 'both', 'sql_max_query_size' => '50000', 'sql_hex_for_binary' => 'something', 'sql_utc_time' => 'something']])->getBody(); // Create our dump filename $dump_name = tempnam(sys_get_temp_dir(), 'dump'); // Save the dump $this->printTaskInfo('Saving dump - <info>' . $dump_name . '</info>'); file_put_contents($dump_name, $sql); // Import the dump locally if (!$this->taskImportSqlDump($dump_name)->host($this->localDbHost)->user($this->localDbUser)->pass($this->localDbPass)->name($this->localDbName)->run()->wasSuccessful()) { throw new RuntimeException('Failed to import dump on local server.'); } $this->printTaskInfo('Deleting dump locally.'); if (!unlink($dump_name)) { return Result::error($this, 'Failed to delete dump on local server.'); } // If we get to here assume everything worked return Result::success($this); }
/** * Executes the ExecuteSqlViaPhpMyAdmin Task. * * Example usage: * ``php * $this->taskExecuteSqlViaPhpMyAdmin('sql goes here') * ->phpMyAdminUrl('http://example.org/phpmyadmin/') * ->phpMyAdminUser('...') * ->phpMyAdminPass('...') * ->remoteDbHost('localhost') * ->remoteDbName('mydb') * ->run(); * ``` * * @return Robo\Result */ public function run() { // First lets login to the phpMyAdmin Server, if not already. if ($this->loggedIn == null) { $result = $this->taskPhpMyAdminLogin()->phpMyAdminUrl($this->phpMyAdminUrl)->phpMyAdminUser($this->phpMyAdminUser)->phpMyAdminPass($this->phpMyAdminPass)->remoteDbHost($this->remoteDbHost)->run(); if (!$result->wasSuccessful()) { throw new RuntimeException('Failed to Login!'); } $this->loggedIn = $result->getTask(); } // Execute our sql $this->printTaskInfo('Executing the query.'); $response = $this->loggedIn->getClient()->post('import.php', ['form_params' => ['db' => $this->remoteDbName, 'server' => $this->loggedIn->getServerId(), 'token' => $this->loggedIn->getToken(), 'sql_query' => $this->query, 'sql_delimiter' => ';']])->getBody(); // Check to make sure it worked if (!$this->confirmSuccessfulImport($response)) { // Save the response to a temp file for later inspection $responseLog = tempnam(sys_get_temp_dir(), 'phpMyAdminResponse'); file_put_contents($responseLog, $response); // Bail out throw new RuntimeException('Your query failed. ' . 'A log of the complete HTTP response has been saved to: ' . $responseLog); } // If we get to here assume everything worked return Result::success($this); }
/** * Executes the PushDbViaPhpMyAdmin Task. * * @return Robo\Result */ public function run() { // First lets login to the phpMyAdmin Server, if not already. if ($this->loggedIn == null) { $result = $this->taskPhpMyAdminLogin()->phpMyAdminUrl($this->phpMyAdminUrl)->phpMyAdminUser($this->phpMyAdminUser)->phpMyAdminPass($this->phpMyAdminPass)->remoteDbHost($this->remoteDbHost)->run(); if (!$result->wasSuccessful()) { throw new RuntimeException('Failed to Login!'); } $this->loggedIn = $result->getTask(); } // Create our dump filename $dump_name = tempnam(sys_get_temp_dir(), 'dump'); // Create our dump locally $cmd = 'mysqldump ' . '-h' . $this->localDbHost . ' -u' . $this->localDbUser . ' ' . (empty($this->localDbPass) ? '' : '-p' . $this->localDbPass) . ' ' . $this->localDbName . ' > ' . $dump_name; $this->printTaskInfo('Dumping db on local server - <info>' . $cmd . '</info>'); if (!$this->taskExec($cmd)->run()->wasSuccessful()) { throw new RuntimeException('Failed to create dump locally.' . 'HINT: Is the `mysqldump` binary in your "PATH"?'); } // Compress the dump $this->printTaskInfo('Compressing dump on local server'); if ($fp_out = gzopen($dump_name . '.gz', 'wb9')) { if ($fp_in = fopen($dump_name, 'rb')) { while (!feof($fp_in)) { gzwrite($fp_out, fread($fp_in, 1024 * 512)); } fclose($fp_in); } else { throw new RuntimeException('Failed to open source dump file for reading.'); } gzclose($fp_out); } else { throw new RuntimeException('Failed to open destination compressed dump file for writing.'); } // Get a list of tables from phpMyAdmin $result = $this->taskPhpMyAdminListTables($this->loggedIn)->remoteDbName($this->remoteDbName)->run(); if (!$result->wasSuccessful()) { throw new RuntimeException('Failed to get list of tabels!'); } $tables = $result->getTask()->getTables(); // Check to see if we have any tables if (count($tables) > 0) { // Create the sql query to drop all those tables $sql = 'SET foreign_key_checks = 0; DROP TABLE '; foreach ($tables as $table) { $sql .= $table . ', '; } $sql = substr($sql, 0, -2) . '; SET foreign_key_checks = 1;'; // Droping the tables $this->printTaskInfo('Droping tables from phpmyadmin.'); $result = $this->taskExecuteSqlViaPhpMyAdmin($sql, $this->loggedIn)->remoteDbName($this->remoteDbName)->run(); // Check to make sure it worked if (!$result->wasSuccessful()) { throw new RuntimeException('Failed to drop tables via phpmyadmin.'); } } // Upload the dump $this->printTaskInfo('Uploading sql dump.'); $response = $this->loggedIn->getClient()->post('import.php', ['multipart' => [['name' => 'db', 'contents' => $this->remoteDbName], ['name' => 'server', 'contents' => $this->loggedIn->getServerId()], ['name' => 'token', 'contents' => $this->loggedIn->getToken()], ['name' => 'import_type', 'contents' => 'database'], ['name' => 'file_location', 'contents' => 'on'], ['name' => 'import_file', 'contents' => fopen($dump_name . '.gz', 'r')], ['name' => 'charset_of_file', 'contents' => 'utf-8'], ['name' => 'allow_interrupt', 'contents' => 'yes'], ['name' => 'skip_queries', 'contents' => '0'], ['name' => 'format', 'contents' => 'sql'], ['name' => 'sql_compatibility', 'contents' => 'NONE'], ['name' => 'sql_no_auto_value_on_zero', 'contents' => 'something']]])->getBody(); // Check that it worked if (!s::create($response)->contains('Import has been successfully finished')) { throw new RuntimeException('Failed to import dump via phpmyadmin. OH NO - we just dropped all the tables!!!'); } // Remove the dump from the local server $this->printTaskInfo('Removing dump from local server. - <info>' . $dump_name . '</info>'); unlink($dump_name); unlink($dump_name . '.gz'); // If we get to here assume everything worked return Result::success($this); }