Esempio n. 1
0
function gs_db_setup_replication($master_host, $slave_host, $user, $pass)
{
    if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) {
        return new GsError('Not allowed on single server systems.');
    }
    # are we root? do we have to sudo?
    $uid = @posix_geteuid();
    $uinfo = @posix_getPwUid($uid);
    $uname = @$uinfo['name'];
    $sudo = $uname === 'root' ? '' : 'sudo ';
    # get binlog position
    #
    $master = null;
    $ok = gs_db_connect($master, 'master', $master_host, $user, $pass, GS_DB_MASTER_DB, 1);
    if (!$ok) {
        return new GsError('Failed to connect to master database.');
    }
    $rs = $master->execute('SHOW MASTER STATUS');
    if (!$rs) {
        return new GsError('DB error.');
    }
    $master_status = $rs->fetchRow();
    if (!$master_status) {
        return new GsError('DB error.');
    }
    # Stop Slave
    #
    $slave = null;
    $ok = gs_db_connect($slave, 'slave', $slave_host, $user, $pass, GS_DB_SLAVE_DB, 1);
    if (!$ok) {
        return new GsError('Failed to connect to slave database.');
    }
    $ok = $slave->execute('STOP SLAVE');
    if (!$ok) {
        return new GsError('Failed to stop database slave replication');
    }
    $dump_filename = '/tmp/gs-db-resync-dump-' . rand() . '.sql';
    # dump Master database
    #
    $cmd = 'mysqldump --opt --lock-tables --databases asterisk';
    $cmd = 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa('root@' . $master_host) . ' ' . qsa($cmd) . ' > ' . qsa($dump_filename) . ' 2>>/dev/null';
    $err = 0;
    $out = array();
    @exec($sudo . $cmd, $out, $err);
    if ($err != 0) {
        return new GsError('Failed to save dump of master database!');
    }
    # restore dump on Slave
    #
    $cmd = 'cat ' . qsa($dump_filename) . ' | ssh -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa('root@' . $slave_host) . ' ' . qsa('mysql asterisk');
    //FIXME - instead of cat, copy the dump to the slave first
    $err = 0;
    $out = array();
    @exec($sudo . $cmd, $out, $err);
    if ($err != 0) {
        return new GsError('Failed to restore database dump on slave!');
    }
    # start replication on Slave
    #
    $query = 'CHANGE MASTER TO ' . 'MASTER_HOST=\'' . $master->escape($master_host) . '\', ' . 'MASTER_USER=\'' . $master->escape($user) . '\', ' . 'MASTER_PASSWORD=\'' . $master->escape($pass) . '\', ' . 'MASTER_LOG_FILE=\'' . $master->escape($master_status['File']) . '\', ' . 'MASTER_LOG_POS=' . (int) $master_status['Position'];
    $ok = $slave->execute($query);
    if (!$ok) {
        return new GsError('Failed to Change Master on Slave!');
    }
    $ok = $slave->execute('START SLAVE');
    if (!$ok) {
        return new GsError('Failed to Start Slave Replication Process');
    }
    return true;
}
Esempio n. 2
0
function &gs_db_cdr_master_connect($_backtrace_level = 0)
{
    global $gs_db_conn_cdr_master, $gs_db_conn_master, $gs_db_conn_slave;
    if (gs_get_conf('GS_DB_CDR_MASTER_HOST') != '') {
        $ret = gs_db_connect($gs_db_conn_cdr_master, 'cdr-master', GS_DB_CDR_MASTER_HOST, GS_DB_CDR_MASTER_USER, GS_DB_CDR_MASTER_PWD, GS_DB_CDR_MASTER_DB, ++$_backtrace_level);
    } else {
        $ret = gs_db_connect($gs_db_conn_master, 'cdr-master', GS_DB_MASTER_HOST, GS_DB_MASTER_USER, GS_DB_MASTER_PWD, GS_DB_MASTER_DB, ++$_backtrace_level);
        if ($ret) {
            $gs_db_conn_cdr_master =& $gs_db_conn_master;
            if (gs_db_slave_is_master() && !gs_db_is_connected($gs_db_conn_slave)) {
                $gs_db_conn_slave =& $gs_db_conn_master;
            }
        }
    }
    if (!$ret) {
        gs_log(GS_LOG_DEBUG, 'Failed to connect to CDR master database');
        $null = null;
        return $null;
    }
    return $gs_db_conn_cdr_master;
}