typo
[phpmyadmin/madhuracj.git] / server_replication.php
blob979b9f070aabfe6eb86d0f0c6046a56820d4f4c7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 /**
12 require_once './libraries/common.inc.php';
14 /**
15 * Does the common work
17 $GLOBALS['js_include'][] = 'server_privileges.js';
18 $GLOBALS['js_include'][] = 'functions.js';
19 $GLOBALS['js_include'][] = 'mootools.js';
20 $GLOBALS['js_include'][] = 'mootools_common.js';
22 require './libraries/server_common.inc.php';
23 require './libraries/replication.inc.php';
24 require './libraries/replication_gui.lib.php';
25 require_once './libraries/server_synchronize.lib.php';
27 /**
28 * Checks if the user is allowed to do what he tries to...
30 if (! $is_superuser) {
31 require './libraries/server_links.inc.php';
32 echo '<h2>' . "\n"
33 . PMA_getIcon('s_replication.png')
34 . $GLOBALS['strReplication'] . "\n"
35 . '</h2>' . "\n";
36 PMA_Message::error('strNoPrivileges')->display();
37 require_once './libraries/footer.inc.php';
40 /**
41 * Handling control requests
44 if (isset($GLOBALS['sr_take_action'])) {
45 $refresh = false;
46 if (isset($GLOBALS['slave_changemaster'])) {
47 $_SESSION['replication']['m_username'] = $sr['username'] = PMA_sqlAddslashes($GLOBALS['username']);
48 $_SESSION['replication']['m_password'] = $sr['pma_pw'] = PMA_sqlAddslashes($GLOBALS['pma_pw']);
49 $_SESSION['replication']['m_hostname'] = $sr['hostname'] = PMA_sqlAddslashes($GLOBALS['hostname']);
50 $_SESSION['replication']['m_port'] = $sr['port'] = PMA_sqlAddslashes($GLOBALS['port']);
51 $_SESSION['replication']['m_correct'] = '';
52 $_SESSION['replication']['sr_action_status'] = 'error';
53 $_SESSION['replication']['sr_action_info'] = $strReplicationUnknownError;
54 // Attempt to connect to the new master server
55 $link_to_master = PMA_replication_connect_to_master($sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']);
57 if (! $link_to_master) {
58 $_SESSION['replication']['sr_action_status'] = 'error';
59 $_SESSION['replication']['sr_action_info'] = sprintf($GLOBALS['strReplicationErrorMasterConnect'], $sr['hostname']);
60 } else {
61 // Read the current master position
62 $position = PMA_replication_slave_bin_log_master($link_to_master);
64 if (empty($position)) {
65 $_SESSION['replication']['sr_action_status'] = 'error';
66 $_SESSION['replication']['sr_action_info'] = $GLOBALS['strReplicationErrorGetPosition'];
67 } else {
68 $_SESSION['replication']['m_correct'] = true;
70 if (! PMA_replication_slave_change_master($sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port'], $position, true, false)) {
71 $_SESSION['replication']['sr_action_status'] = 'error';
72 $_SESSION['replication']['sr_action_info'] = $GLOBALS['strReplicationUnableToChange'];
73 } else {
74 $_SESSION['replication']['sr_action_status'] = 'success';
75 $_SESSION['replication']['sr_action_info'] = sprintf($GLOBALS['strReplicationChangedSuccesfully'], $sr['hostname']);
79 } elseif (isset($GLOBALS['sr_slave_server_control'])) {
80 if ($GLOBALS['sr_slave_action'] == 'reset') {
81 PMA_replication_slave_control("STOP");
82 PMA_DBI_try_query("RESET SLAVE;");
83 PMA_replication_slave_control("START");
84 } else {
85 PMA_replication_slave_control($GLOBALS['sr_slave_action'], $GLOBALS['sr_slave_control_parm']);
87 $refresh = true;
89 } elseif (isset($GLOBALS['sr_slave_skip_error'])) {
90 $count = 1;
91 if (isset($GLOBALS['sr_skip_errors_count'])) {
92 $count = $GLOBALS['sr_skip_errors_count'] * 1;
94 PMA_replication_slave_control("STOP");
95 PMA_DBI_try_query("SET GLOBAL SQL_SLAVE_SKIP_COUNTER = ".$count.";");
96 PMA_replication_slave_control("START");
98 } elseif (isset($GLOBALS['sl_sync'])) {
99 // TODO username, host and port could be read from 'show slave status',
100 // when asked for a password this might work in more situations the just after changing master (where the master password is stored in session)
101 $src_link = PMA_replication_connect_to_master($_SESSION['replication']['m_username'], $_SESSION['replication']['m_password'], $_SESSION['replication']['m_hostname'], $_SESSION['replication']['m_port']);
102 $trg_link = null; // using null to indicate the current PMA server
104 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $src_link); // let's find out, which databases are replicated
106 $do_db = array();
107 $ignore_db = array();
108 $dblist = array();
110 if (! empty($data[0]['Binlog_Do_DB'])) {
111 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
113 if (! empty($data[0]['Binlog_Ignore_DB'])) {
114 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
117 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $src_link);
118 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
119 if ($tmp_row[0] == 'information_schema') {
120 continue;
122 if (count($do_db) == 0) {
123 if (array_search($tmp_row[0], $ignore_db) !== false) {
124 continue;
126 $dblist[] = $tmp_row[0];
128 PMA_DBI_query('CREATE DATABASE IF NOT EXISTS '.$tmp_row[0], $trg_link);
129 } else {
130 if (array_search($tmp_row[0], $do_db) !== false) {
131 $dblist[] = $tmp_row[0];
132 PMA_DBI_query('CREATE DATABASE IF NOT EXISTS '.$tmp_row[0], $trg_link);
135 } // end while
137 unset($do_db, $ignore_db, $data);
139 if (isset($GLOBALS['repl_data'])) {
140 $include_data = true;
141 } else {
142 $include_data = false;
144 foreach ($dblist as $db) {
145 PMA_replication_synchronize_db($db, $src_link, $trg_link, $include_data);
147 // TODO some form of user feedback error/success would be nice
148 // What happens if $dblist is empty?
149 // or sync failed?
152 if ($refresh) {
153 Header("Location: ". PMA_generate_common_url($GLOBALS['url_params']));
155 unset($refresh);
158 * Displays the links
160 require './libraries/server_links.inc.php';
162 echo '<div id="replication">';
163 echo ' <h2>';
164 echo ' <img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 's_replication.png" width="16" height="16" alt="" />';
165 echo $GLOBALS['strReplication'];
166 echo ' </h2>';
168 // Display error messages
169 if (isset($_SESSION['replication']['sr_action_status']) && isset($_SESSION['replication']['sr_action_info'])) {
170 if ($_SESSION['replication']['sr_action_status'] == 'error') {
171 PMA_Message::error($_SESSION['replication']['sr_action_info'])->display();
172 $_SESSION['replication']['sr_action_status'] = 'unknown';
173 } elseif ($_SESSION['replication']['sr_action_status'] == 'success') {
174 PMA_Message::success($_SESSION['replication']['sr_action_info'])->display();
175 $_SESSION['replication']['sr_action_status'] = 'unknown';
179 if ($server_master_status) {
180 if (! isset($GLOBALS['repl_clear_scr'])) {
181 echo PMA_js_mootools_domready($jscode['master_replication']);
182 echo '<fieldset>';
183 echo '<legend>' . $GLOBALS['strReplicationMaster'] . '</legend>';
184 echo $GLOBALS['strReplicationConfiguredMaster'];
185 echo '<ul>';
186 echo ' <li><a href="#" id="master_status_href">' . $GLOBALS['strReplicationShowMasterStatus'] . '</a></li>';
187 PMA_replication_print_status_table('master', true, false);
189 echo ' <li><a href="#" id="master_slaves_href">' . $GLOBALS['strReplicationShowConnectedSlaves'] . '</a></li>';
190 PMA_replication_print_slaves_table(true);
192 $_url_params = $GLOBALS['url_params'];
193 $_url_params['mr_adduser'] = true;
194 $_url_params['repl_clear_scr'] = true;
196 echo ' <li><a href="' . PMA_generate_common_url($_url_params) . '" id="master_addslaveuser_href">' . $GLOBALS['strReplicationAddSlaveUser'] . '</a></li>';
199 // Display 'Add replication slave user' form
200 if (isset($GLOBALS['mr_adduser'])) {
201 PMA_replication_gui_master_addslaveuser();
202 } elseif (! isset($GLOBALS['repl_clear_scr'])) {
203 echo "</ul>";
204 echo "</fieldset>";
206 } elseif (! isset($GLOBALS['mr_configure']) && ! isset($GLOBALS['repl_clear_scr'])) {
207 $_url_params = $GLOBALS['url_params'];
208 $_url_params['mr_configure'] = true;
210 echo '<fieldset>';
211 echo '<legend>' . $GLOBALS['strReplicationMaster'] . '</legend>';
212 echo sprintf($GLOBALS['strReplicationServernConfiguredMaster'], PMA_generate_common_url($_url_params));
213 echo '</fieldset>';
216 if (isset($GLOBALS['mr_configure'])) {
217 // Render the 'Master configuration' section
218 echo PMA_js_mootools_domready($jscode['configure_master']);
219 echo '<fieldset>';
220 echo '<legend>' . $GLOBALS['strReplicationMasterConfiguration'] . '</legend>';
221 echo $GLOBALS['strReplicationMasterChooseMode'] . '<br /><br />';
223 echo '<select name="db_type" id="db_type">';
224 echo '<option value="all">' . $GLOBALS['strReplicationMasterChooseAll'] . '</option>';
225 echo '<option value="ign">' . $GLOBALS['strReplicationMasterChooseIgn'] . '</option>';
226 echo '</select>';
227 echo '<br /><br />';
228 echo $GLOBALS['strReplicationSelectDatabases'] . '<br />';
229 echo PMA_replication_db_multibox();
230 echo '<br /><br />';
231 echo $GLOBALS['strReplicationAddLines'] . '<br />';
232 echo '<pre id="rep">server-id='. $serverid . '<br />log-bin=mysql-bin<br />log-error=mysql-bin.err<br /></pre>';
233 echo $GLOBALS['strReplicationRestartServer'];
234 echo '</fieldset>';
235 echo '<fieldset class="tblFooters">';
236 echo ' <form method="post" action="server_replication.php" >';
237 echo PMA_generate_common_hidden_inputs('', '');
238 echo ' <input type="submit" value="' . $GLOBALS['strGo'] . '" id="goButton" />';
239 echo ' </form>';
240 echo '</fieldset>';
242 require_once './libraries/footer.inc.php';
243 exit;
246 echo '</div>';
248 if (! isset($GLOBALS['repl_clear_scr'])) {
249 // Render the 'Slave configuration' section
250 echo '<fieldset>';
251 echo '<legend>' . $GLOBALS['strReplicationSlave'] . '</legend>';
252 if ($server_slave_status) {
253 echo PMA_js_mootools_domready($jscode['slave_control']);
254 echo '<div id="slave_configuration_gui">';
256 $_url_params = $GLOBALS['url_params'];
257 $_url_params['sr_take_action'] = true;
258 $_url_params['sr_slave_server_control'] = true;
260 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
261 $_url_params['sr_slave_action'] = 'start';
262 } else {
263 $_url_params['sr_slave_action'] = 'stop';
266 $_url_params['sr_slave_control_parm'] = 'IO_THREAD';
267 $slave_control_io_link = PMA_generate_common_url($_url_params);
269 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
270 $_url_params['sr_slave_action'] = 'start';
271 } else {
272 $_url_params['sr_slave_action'] = 'stop';
275 $_url_params['sr_slave_control_parm'] = 'SQL_THREAD';
276 $slave_control_sql_link = PMA_generate_common_url($_url_params);
278 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No'
279 || $server_slave_replication[0]['Slave_SQL_Running'] == 'No'
281 $_url_params['sr_slave_action'] = 'start';
282 } else {
283 $_url_params['sr_slave_action'] = 'stop';
286 $_url_params['sr_slave_control_parm'] = null;
287 $slave_control_full_link = PMA_generate_common_url($_url_params);
289 $_url_params['sr_slave_action'] = 'reset';
290 $slave_control_reset_link = PMA_generate_common_url($_url_params);
292 $_url_params = $GLOBALS['url_params'];
293 $_url_params['sr_slave_skip_error'] = true;
294 $slave_skip_error_link = PMA_generate_common_url($_url_params);
296 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
297 PMA_Message::warning('strReplicationSlaveSQLThreadNotRunning')->display();
299 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
300 PMA_Message::warning('strReplicationSlaveIOThreadNotRunning')->display();
302 $_url_params = $GLOBALS['url_params'];
303 $_url_params['sl_configure'] = true;
304 $_url_params['repl_clear_scr'] = true;
306 $reconfiguremaster_link = PMA_generate_common_url($_url_params);
308 echo $GLOBALS['strReplicationSlaveConfigured'];
309 echo '<br />';
310 echo '<ul>';
311 echo ' <li><a href="#" id="slave_status_href">' . $GLOBALS['strReplicationSlaveSeeStatus'] . '</a></li>';
312 echo PMA_replication_print_status_table('slave', true, false);
313 if (isset($_SESSION['replication']['m_correct']) && $_SESSION['replication']['m_correct'] == true) {
314 echo PMA_js_mootools_domready($jscode['slave_control_sync']);
315 echo ' <li><a href="#" id="slave_synchronization_href">' . $GLOBALS['strReplicationSynchronize'] . '</a></li>';
316 echo ' <div id="slave_synchronization_gui" style="display: none">';
317 echo ' <form method="post" action="server_replication.php">';
318 echo PMA_generate_common_hidden_inputs('', '');
319 echo ' <input type="checkbox" name="repl_struc" value="1" checked="checked" disabled="disabled" /> ' . $GLOBALS['strStructure'] . '<br />'; // this is just for vizualization, it has no other purpose
320 echo ' <input type="checkbox" name="repl_data" value="1" checked="checked" /> ' . $GLOBALS['strData'] .' <br />';
321 echo ' <input type="hidden" name="sr_take_action" value="1" />';
322 echo ' <input type="submit" name="sl_sync" value="' . $GLOBALS['strGo'] . '" />';
323 echo ' </form>';
324 echo ' </div>';
326 echo ' <li><a href="#" id="slave_control_href">' . $GLOBALS['strReplicationControlSlave'] . '</a>';
327 echo ' <div id="slave_control_gui" style="display: none">';
328 echo ' <ul>';
329 echo ' <li><a href="'. $slave_control_full_link . '">' . (($server_slave_replication[0]['Slave_IO_Running'] == 'No' || $server_slave_replication[0]['Slave_SQL_Running'] == 'No') ? $GLOBALS['strFullStart'] : $GLOBALS['strFullStop']) . ' </a></li>';
330 echo ' <li><a href="'. $slave_control_reset_link . '">' . $GLOBALS['strReplicationSlaveReset'] . '</a></li>';
331 echo ' <li><a href="' . $slave_control_sql_link . '">' . sprintf($GLOBALS['strReplicationSlaveSQLThread'], ($server_slave_replication[0]['Slave_SQL_Running'] == 'No' ? $GLOBALS['strStart'] : $GLOBALS['strStop'])) . '</a></li>';
332 echo ' <li><a href="' . $slave_control_io_link . '">' . sprintf($GLOBALS['strReplicationSlaveIOThread'], ($server_slave_replication[0]['Slave_IO_Running'] == 'No' ? $GLOBALS['strStart'] : $GLOBALS['strStop'])) . '</a></li>';
333 echo ' </ul>';
334 echo ' </div>';
335 echo ' </li>';
336 echo ' <li><a href="#" id="slave_errormanagement_href">' . $GLOBALS['strReplicationSlaveErrorManagement'] . '</a>';
337 echo ' <div id="slave_errormanagement_gui" style="display: none">';
338 PMA_Message::warning($GLOBALS['strReplicationSkippingErrorWarn'])->display();
339 echo ' <ul>';
340 echo ' <li><a href="' . $slave_skip_error_link . '">' . $GLOBALS['strReplicationSlaveSkipCurrentError'] . '</a></li>';
341 echo ' <li>' . $GLOBALS['strReplicationSlaveSkipNext'];
342 echo ' <form method="post" action="server_replication.php">';
343 echo PMA_generate_common_hidden_inputs('', '');
344 echo ' <input type="text" name="sr_skip_errors_count" value="1" style="width: 30px" />' . $GLOBALS['strReplicationSlaveSkipNextErrors'];
345 echo ' <input type="submit" name="sr_slave_skip_error" value="' . $GLOBALS['strGo'] . '" />';
346 echo ' <input type="hidden" name="sr_take_action" value="1" />';
347 echo ' </form></li>';
348 echo ' </ul>';
349 echo ' </div>';
350 echo ' </li>';
351 echo ' <li><a href="' . $reconfiguremaster_link . '">' . $GLOBALS['strReplicationSlaveChangeMaster'] . '</a></li>';
352 echo '</ul>';
354 } elseif (! isset($GLOBALS['sl_configure'])) {
355 $_url_params = $GLOBALS['url_params'];
356 $_url_params['sl_configure'] = true;
357 $_url_params['repl_clear_scr'] = true;
359 echo sprintf($GLOBALS['strReplicationSlaveNotConfigured'], PMA_generate_common_url($_url_params));
361 echo '</div>';
362 echo '</fieldset>';
364 if (isset($GLOBALS['sl_configure'])) {
365 PMA_replication_gui_changemaster("slave_changemaster");
367 require_once './libraries/footer.inc.php';