/** * Ask for a Question * * @param object $question The Question to ask for * * @return string */ function askQuestion($question) { if ($question instanceof Question) { return recup($question->qt, $question->def); } return null; }
<?php require_once 'include/process.php'; require_once 'include/recup.php'; require_once 'include/parseConf.php'; $tab = recup($defInit, $path); $tabOld = recupOld($previous, $path); killOld($tabOld, $previous, $path); process($tab, $defInit, $path);
/** * Run SQL queries * * @param string $file Used configuration file * @param string $name Server name * * @return void */ function runSQLQueries($file, $name) { // Create a new DOMDocument object $dom = new DOMDocument(); // Load the XML file check_errs($dom->load($file), false, "File " . $file . " not correctly formed.", "File " . $file . " is OK!"); // Get the root node $root = $dom->documentElement; echo "\n"; // Get the list of servers already in file $serverList = $dom->getElementsByTagName('serveur'); switch ($serverList->length) { // If two, we can continue case 2: // Get the server $myServer = getServerByName($name, $file); // Get the other server $myOtherServer = getOtherServerByName($name, $file); // Get the SHOW MASTER STATUS of the other server because we need for CHANGE MASTER TO... if ($myOtherServer->getAttribute('localhost') == "y") { $masterStatus = getFileAndPosition("localhost:" . $myOtherServer->getAttribute('dbport'), $myOtherServer->getAttribute('dbusername'), $myOtherServer->getAttribute('dbpassword')); } else { $masterStatus = getFileAndPosition($myOtherServer->getAttribute('ip') . ":" . $myOtherServer->getAttribute('dbport'), $myOtherServer->getAttribute('dbusername'), $myOtherServer->getAttribute('dbpassword')); } check_errs($masterStatus, false, "Unable to get information.", "Information collected!"); // SQL first query to execute on server $sql1 = "GRANT REPLICATION SLAVE ON *.* TO '" . $myServer->getElementsByTagName('esclave')->item(0)->getAttribute('username') . "'@'%' IDENTIFIED BY '" . $myServer->getElementsByTagName('esclave')->item(0)->getAttribute('password') . "';\nFLUSH PRIVILEGES;"; // SQL second query $sql2 = "SLAVE STOP;\nCHANGE MASTER TO MASTER_HOST='" . $myOtherServer->getAttribute('ip') . "', MASTER_USER='******'esclave')->item(0)->getAttribute('username') . "', MASTER_PASSWORD='******'esclave')->item(0)->getAttribute('password') . "', MASTER_LOG_FILE='" . $masterStatus['File'] . "', MASTER_LOG_POS=" . $masterStatus['Position'] . ";\nSTART SLAVE;"; // Ask confirmation for the first query echo "\n"; cecho("Do you want to execute these queries on " . $myServer->getAttribute('ip') . " ?", "red"); echo "\n\n"; echo $sql1 . "\n\n"; $answerSQL = recup("y or n [default y]: ", "y"); if ($answerSQL != "n") { if ($myServer->getAttribute('localhost') == "y") { // Execute query as "localhost" executeSQLRequest("localhost:" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql1); } else { // Execute distant query executeSQLRequest($myServer->getAttribute('ip') . ":" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql1); } } // Ask confirmation for the second query echo "\n"; cecho("Do you want to execute these queries on " . $myServer->getAttribute('ip') . " ?", "red"); echo "\n\n"; echo $sql2 . "\n\n"; $answerSQL = recup("y or n [default y]: ", "y"); if ($answerSQL != "n") { if ($myServer->getAttribute('localhost') == "y") { // Execute query as "localhost" executeSQLRequest("localhost:" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql2); } else { // Execute distant query executeSQLRequest($myServer->getAttribute('ip') . ":" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql2); } } // Ask confirmation for mysqldump query echo "\n"; // If server if the "first", we do the mysqldump if ($myServer->getAttribute('first') == "y") { cecho("Do you want to execute this command on " . $myServer->getAttribute('ip') . " ?", "red"); echo "\n\n"; echo "mysqldump -u " . $myServer->getAttribute('dbusername') . " -p --databases " . $root->getAttribute('db') . " > /tmp/mysqldump.sql" . "\n\n"; $answerSQL = recup("y or n [default y]: ", "y"); if ($answerSQL != "n") { // If localhost, local command if ($myServer->getAttribute('localhost') == "y") { exec("mysqldump -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " --databases " . $root->getAttribute('db') . " > /tmp/mysqldump.sql", $result, $returnVar); } else { $sshPort = recup("SSH port [default 22]? ", "22"); // User allowed $sshUser = recup("Username allowed to connect to " . $myServer->getAttribute('ip') . ": "); // Command via SSH exec("ssh -p " . $sshPort . " " . $sshUser . "@" . $myServer->getAttribute('ip') . " mysqldump -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " --databases " . $root->getAttribute('db') . " > /tmp/mysqldump.sql", $result, $returnVar); } check_errs($returnVar, true, "Unable to perform MySQLDump.", "MySQLDump performed!"); } // If server is the "second", we get the mysqldump of the "first" } else { cecho("Do you want to execute this command on " . $myServer->getAttribute('ip') . " ?", "red"); echo "\n\n"; // /tmp/mysqldump.sql is a local file echo "mysql -u " . $myServer->getAttribute('dbusername') . " -p " . $root->getAttribute('db') . " < /tmp/mysqldump.sql" . "\n\n"; $answerSQL = recup("y or n [default y]: ", "y"); if ($answerSQL != "n") { if ($myServer->getAttribute('localhost') == "y") { exec("mysql -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " < /tmp/mysqldump.sql", $result, $returnVar); } else { $sshPort = recup("SSH port [default 22]? ", "22"); // User allowed $sshUser = recup("Username allowed to connect to " . $myServer->getAttribute('ip') . ": "); // Command via SSH exec("ssh -p " . $sshPort . " " . $sshUser . "@" . $myServer->getAttribute('ip') . " mysql -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " < /tmp/mysqldump.sql", $result, $returnVar); } check_errs($returnVar, true, "Failed.", "Success!"); } } break; // Else, quit // Else, quit default: echo exec("clear"); cecho("You must have two servers in your XML file.", "red"); echo "\n\n"; menu(); } }
/** * Show the Menu (its list of tasks) * * @param Menu $menu The Menu to show * @param bool $zero [optional] If true, we also show the Task with the ID 0, which is almost "Quit" * * @return void */ function showMenu(Menu $menu, $zero = false) { $this->present($menu->name); echo "\nSelect a task:\n\n"; $menu->showTasks(); if ($zero) { echo "-------------------------------------------------------\n"; echo "[ 0] " . $menu->task_list[0]->description . "\n"; } // Waiting for input $task = recup("\nSelected task: "); $success = false; foreach ($menu->task_list as $key => $oneTask) { if ("{$key}" === $task) { if (is_callable($oneTask->procedure)) { $success = true; $this->clearScreen(); $oneTask->presentTask(); call_user_func($oneTask->procedure, $menu); } else { if ($oneTask->procedure === "quit") { $this->quit(); } } break; } } if (!$success) { $this->clearScreen(); cecho("Incorrect input", "red"); echo "\n"; } echo "\n"; $this->showMenu($menu, true); }