public function load($param) { $this->view = false; $db = $this->di['db']->sql(DB_DEFAULT); $server_dest = $param[0]; $databases = explode(",", $param[1]); $dump = array(); foreach ($databases as $database) { $sql = "SELECT a.id,b.`name`,b.ip,b.port,a.date_start, a.`time`, a.`database` FROM mysql_dump a" . " inner join mysql_server b ON a.id_mysql_server = b.id" . " WHERE `database` ='" . $db->sql_real_escape_string($database) . "'" . " order by a.id"; $res = $db->sql_query($sql); $tab = new Table(1); $tab->addHeader(array("ID", "name", "IP", "port", "date_start", "time", "database")); while ($ob = $db->sql_fetch_object($res)) { $tab->addLine(array($ob->id, $ob->name, $ob->ip, $ob->port, $ob->date_start, $ob->time, $ob->database)); } $msg = $tab->display(); $msg .= "Select the dump (id) to restore on '" . $server_dest . "' ? \n"; $msg .= "[[INPUT]]\n"; new Window("Load database", $msg); } }
public function switchMaster($servers) { $this->view = false; $slave = $this->di['db']->sql(str_replace('-', '_', $servers[0])); $master = $this->di['db']->sql(str_replace('-', '_', $servers[1])); // first test that the master have log_slave_updates = ON $log_slave_updates = $master->getVariables("log_slave_updates"); if ($log_slave_updates !== 'ON') { throw new \Exception("[ERROR] PMACLI-001 : log_slave_updates must be unabled on new master : '" . $servers[1] . "'"); } $MS = new MasterSlave(); $MS->setInstance($slave); $thread_slave = $MS->isSlave(); $slave_master_host = array(); if ($thread_slave) { foreach ($thread_slave as $thread) { $conn = empty($thread['Connection_name']) ? '' : " '" . $thread['Connection_name'] . "'"; $slave_master_host[$thread['Master_Host']] = $conn; } } else { throw new \Exception("[ERROR] PMACLI-002 : the server should configured as slave : '" . $servers[0] . "'"); } $MS->setInstance($master); $thread_master = $MS->isSlave(); $master_master_host = array(); if ($thread_master) { foreach ($thread_master as $thread) { $conn = empty($thread['Connection_name']) ? '' : " '" . $thread['Connection_name'] . "'"; $master_master_host[$thread['Master_Host']] = $conn; } } else { throw new \Exception("[ERROR] PMACLI-002 : the server should configured as slave : '" . $servers[1] . "'"); } $cmp = array_intersect_key($slave_master_host, $master_master_host); if (count($cmp) !== 1) { throw new \Exception("[ERROR] PMACLI-003 : the servers must have the same master"); } $ip_of_master = array_keys($cmp)[0]; //stop du slave $sql = "STOP SLAVE" . $slave_master_host[$ip_of_master] . ";"; echo $servers[0] . "> " . $sql . "\n"; $slave->sql_query($sql); $MS->setInstance($slave); $thread_slave = $MS->isSlave(); foreach ($thread_slave as $thread) { if ($thread['Master_Host'] == $ip_of_master) { $SLAVE_MASTER_LOG_FILE = $thread['Master_Log_File']; $SLAVE_MASTER_LOG_POS = $thread['Exec_Master_Log_Pos']; } } sleep(1); //stop slave n°2 (future master) $sql = "STOP SLAVE" . $master_master_host[$ip_of_master] . ";"; echo $servers[1] . "> " . $sql . "\n"; $master->sql_query($sql); $MS->setInstance($master); $thread_master = $MS->isSlave(); foreach ($thread_master as $thread) { if ($thread['Master_Host'] == $ip_of_master) { $MASTER_MASTER_LOG_FILE = $thread['Master_Log_File']; $MASTER_MASTER_LOG_POS = $thread['Exec_Master_Log_Pos']; } } if ($MASTER_MASTER_LOG_FILE !== $SLAVE_MASTER_LOG_FILE || $SLAVE_MASTER_LOG_POS > $MASTER_MASTER_LOG_POS) { throw new \Exception("[ERROR] PMACLI-005 : Error the new master is behind the slave"); } $tab = new Table(1); $tab->addHeader(array("name", "IP", "port", "Connection_name", "Master_Log_File", "Master_Log_Pos")); $param['master'] = $master->getParams(); $param['slave'] = $slave->getParams(); $tab->addLine(array($servers[0], $param['slave']['hostname'], empty($param['slave']['port']) ? 3306 : $param['slave']['port'], $master_master_host[$ip_of_master], $SLAVE_MASTER_LOG_FILE, $SLAVE_MASTER_LOG_POS)); $tab->addLine(array($servers[1], $param['master']['hostname'], empty($param['master']['port']) ? 3306 : $param['master']['port'], $slave_master_host[$ip_of_master], $MASTER_MASTER_LOG_FILE, $MASTER_MASTER_LOG_POS)); $msg = $tab->display(); echo $msg; $sql = "SHOW MASTER STATUS"; echo $servers[1] . "> " . $sql . "\n"; $MS->setInstance($master); $master_status = $MS->isMaster(); $tab2 = new Table(1); $header = array(); $var = array(); foreach ($master_status as $key => $value) { $header[] = $key; $var[] = $value; } $master_file = $var[0]; $master_position = $var[1]; $tab2->addHeader($header); $tab2->addLine($var); echo $tab2->display(); $sql = "START SLAVE" . $slave_master_host[$ip_of_master] . " UNTIL MASTER_LOG_FILE='" . $MASTER_MASTER_LOG_FILE . "', MASTER_LOG_POS=" . $MASTER_MASTER_LOG_POS . ";"; echo $servers[0] . "> " . $sql . "\n"; $slave->sql_query($sql); $MS->setInstance($slave); do { $thread_slave = $MS->isSlave(); foreach ($thread_slave as $thread) { if ($thread['Master_Host'] == $ip_of_master) { $SLAVE_MASTER_LOG_FILE = $thread['Master_Log_File']; $SLAVE_MASTER_LOG_POS = $thread['Exec_Master_Log_Pos']; } } $sql = "SHOW SLAVE" . $slave_master_host[$ip_of_master] . " STATUS;"; echo $servers[0] . "> " . $sql . "\n"; $tab3 = new Table(1); $tab3->addHeader(array("Master_Log_File", "Exec_Master_Log_Pos")); $tab3->addLine(array($SLAVE_MASTER_LOG_FILE, $SLAVE_MASTER_LOG_POS)); echo $tab3->display(); sleep(1); } while ($MASTER_MASTER_LOG_POS != $SLAVE_MASTER_LOG_POS); $sql = "STOP SLAVE" . $master_master_host[$ip_of_master] . ";"; echo $servers[0] . "> " . $sql . "\n"; $slave->sql_query($sql); $sql = "CHANGE MASTER" . $slave_master_host[$ip_of_master] . " TO MASTER_HOST = '" . $param['master']['hostname'] . "', MASTER_LOG_FILE='" . $master_file . "', MASTER_LOG_POS=" . $master_position . ";"; echo $servers[0] . "> " . $sql . "\n"; $slave->sql_query($sql); $sql = "START SLAVE" . $slave_master_host[$ip_of_master] . ";"; echo $servers[1] . "> " . $sql . "\n"; $master->sql_query($sql); $sql = "START SLAVE" . $master_master_host[$ip_of_master] . ";"; echo $servers[0] . "> " . $sql . "\n"; $slave->sql_query($sql); //$slave->sql_query($sql); }
/** * This method return a string, printing a table with all connection configured and said which one is connected * @author Aurélien LEQUOY <*****@*****.**> * @license GNU/GPL * @license http://opensource.org/licenses/GPL-3.0 GNU Public License * @param void * @return string return a table with all main's information and tell us with one is connected * @description give a status o all databases * @access public * @example echo $this->di['db']->sql(DB_DEFAULT); * @package Sgbd * @See Also Glial\Cli\Table * @since 3.0 First time this was introduced. * @version 3.0 */ public function __toString() { $tab = new Table(1); $tab->addHeader(array("Id", "Name", "Is connected ?", "Driver", "IP", "Port", "User", "Password")); $i = 1; foreach ($this->config as $name => $param) { $port = empty($param['port']) ? "3306" : $param['port']; $isconnected = empty($this->db[$name]) ? "" : "■"; $tab->addLine(array((string) $i, $name, $isconnected, $param['driver'], $param['hostname'], $port, $param['user'], str_repeat("*", strlen($param['password'])))); $i++; } return $tab->display(); }
public function stats_for_log($data) { $table = new Table(0); $table->addHeader(array("Table", "Rows deleted")); foreach ($data as $table_name => $row_deleted) { $table->addLine(array($table_name, $row_deleted)); } echo $table->display(); }
public function waitPosition($db, $file, $position) { $MS = new MasterSlave(); $MS->setInstance($db); do { $thread_slave = $MS->isSlave(); foreach ($thread_slave as $thread) { $Relay_Master_Log_File = $thread['Relay_Master_Log_File']; $Exec_Master_Log_Pos = $thread['Exec_Master_Log_Pos']; } $sql = "SHOW SLAVE STATUS;"; $this->log($sql); if (!empty($thread['Last_Errno'])) { debug($thread); throw new \Exception('PMACLI-037 Error : Impossible to load data !'); } $tab = new Table(1); $tab->addHeader(array("Relay_Master_Log_File", "Exec_Master_Log_Pos")); $tab->addLine(array($Relay_Master_Log_File, $Exec_Master_Log_Pos)); echo $tab->display(); sleep(1); } while ($file != $Relay_Master_Log_File || $position != $Exec_Master_Log_Pos); }