Skip to main content
adicionou 24 caracteres ao conteúdo
Fonte Link

$HOST         = "localhost";       # HOSTING
$USER_NAME    = "root";            # USUARIO DA BASE DE DADOS
$USER_PWRD    = "123456";          # PASSWORD DA BASE DE DADOS
$DB_PRIMARY   = "data base";       # NOME DA BASE DE DADOS PRIMARIA

// TABELA SELECIONADA PARA O BACKUP
$DB_SELECT = "$DB_PRIMARY";

// CONEXÃO BACKUP
$CONNECT_BACKUP = new mysqli("$HOST", "$USER_NAME", "$USER_PWRD", "$DB_SELECT");

// SETA O CHARSET DE COMUNICAÇÃO
$CONNECT_BACKUP->set_charset('utf8');

if ($CONNECT_BACKUP->connect_errno) {
    echo "Failed to connect to MySQL: (" . $CONNECT_BACKUP->connect_errno . ") " . $CONNECT_BACKUP->connect_error;
}

// INI FUÇÕES PARA A GERAÇÃO

// SQL QUERY ARRAY
function sqlQuery( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    return $ROW_COMAND;

}

// SQL QUERY
function sqlQueryView( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    foreach ($ROW_COMAND as $KEY => $VALUE) {

        foreach ($VALUE as $KEY2 => $VALUE2) {

            $COLS[] = $VALUE2;

        }

    }

    return $COLS;

}

// SQL INSERT
function sqlInsert( $DB_TABLE, $ROW_BACKUP ) {
    
    if ( $ROW_BACKUP != null ) {

    $SQL_INSERT = 'INSERT INTO `'.$DB_TABLE.'`'."\n";
    $SQL_INSERT .= '(';

    foreach ($ROW_BACKUP[0] as $KEY => $VALUE) {

    $COLS[] = "`$KEY`";
    }

    $COLS_IMP = implode( ', ', $COLS );
    $SQL_INSERT .= $COLS_IMP;

    $SQL_INSERT .= ")"."\n";
    $SQL_INSERT .= "VALUES"."\n";

    foreach ($ROW_BACKUP as $KEY => $VALUE) {
        
    $SQL_INSERT .= "(";

        foreach ($VALUE as $KEY2 => $VALUE2) {
            $ROWS[] = "'".$VALUE2."'";
        }

    $ROWS_IMP = implode( ', ', $ROWS );unset ($ROWS);
    $SQL_INSERT .= $ROWS_IMP;

    $SQL_INSERT .= "), "."\n";

    }

    $SQL_INSERT = trim($SQL_INSERT, '), '."\n");

    $SQL_INSERT .= "); "."\n"."\n";

    }

    return $SQL_INSERT;
}

// END FUÇÕES PARA A GERAÇÃO

// INI GERA O SCRIPT SQL
$SQL_TABLES = 'SHOW TABLES FROM `'.$DB_SELECT.'`;';

$ARRAY_ROWS_TABLES = sqlQuery( $SQL_TABLES, $CONNECT_BACKUP );

foreach ($ARRAY_ROWS_TABLES as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $EMPRESAS[]$SELECT_IN_TABLES[] = 'SELECT * FROM `'.$VALUE2.'`;'."\n";
        
        $SQL_SELECTS[] = $EMPRESAS;$SELECT_IN_TABLES; unset($EMPRESAS$SELECT_IN_TABLES);

    }

}

foreach ($SQL_SELECTS as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $DB_NAME_X = explode ('`',$VALUE2);

        $DB_NAME_Q = $DB_NAME_X[1];
        
        //SQL CREATE TABLE
        $SQL_CREATE_TABLES = 'SHOW CREATE TABLE `'.$DB_NAME_Q.'`;';

        $ARRAY_ROWS_CREATE_TABLES = sqlQueryView( $SQL_CREATE_TABLES, $CONNECT_BACKUP );
        
        $SQL_CREATE .= $ARRAY_ROWS_CREATE_TABLES[1].";"."\n"."\n";  

        $SQL_QUERY = sqlQuery( $VALUE2, $CONNECT_BACKUP );
        
        $SQL_INSERTS .= sqlInsert( $DB_NAME_Q, $SQL_QUERY );

    }

}
// END GERA O SCRIPT SQL

// INI MOSTRA SCRIPT SQL PRONTO

//DEBUG
print_r ( $SQL_CREATE );

//DEBUG
print_r ( $SQL_INSERTS );

// END MOSTRA SCRIPT SQL PRONTO


$HOST         = "localhost";       # HOSTING
$USER_NAME    = "root";            # USUARIO DA BASE DE DADOS
$USER_PWRD    = "123456";          # PASSWORD DA BASE DE DADOS
$DB_PRIMARY   = "data base";       # NOME DA BASE DE DADOS PRIMARIA

// TABELA SELECIONADA PARA O BACKUP
$DB_SELECT = "$DB_PRIMARY";

// CONEXÃO BACKUP
$CONNECT_BACKUP = new mysqli("$HOST", "$USER_NAME", "$USER_PWRD", "$DB_SELECT");

// SETA O CHARSET DE COMUNICAÇÃO
$CONNECT_BACKUP->set_charset('utf8');

if ($CONNECT_BACKUP->connect_errno) {
    echo "Failed to connect to MySQL: (" . $CONNECT_BACKUP->connect_errno . ") " . $CONNECT_BACKUP->connect_error;
}

// INI FUÇÕES PARA A GERAÇÃO

// SQL QUERY ARRAY
function sqlQuery( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    return $ROW_COMAND;

}

// SQL QUERY
function sqlQueryView( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    foreach ($ROW_COMAND as $KEY => $VALUE) {

        foreach ($VALUE as $KEY2 => $VALUE2) {

            $COLS[] = $VALUE2;

        }

    }

    return $COLS;

}

// SQL INSERT
function sqlInsert( $DB_TABLE, $ROW_BACKUP ) {
    
    if ( $ROW_BACKUP != null ) {

    $SQL_INSERT = 'INSERT INTO `'.$DB_TABLE.'`'."\n";
    $SQL_INSERT .= '(';

    foreach ($ROW_BACKUP[0] as $KEY => $VALUE) {

    $COLS[] = "`$KEY`";
    }

    $COLS_IMP = implode( ', ', $COLS );
    $SQL_INSERT .= $COLS_IMP;

    $SQL_INSERT .= ")"."\n";
    $SQL_INSERT .= "VALUES"."\n";

    foreach ($ROW_BACKUP as $KEY => $VALUE) {
        
    $SQL_INSERT .= "(";

        foreach ($VALUE as $KEY2 => $VALUE2) {
            $ROWS[] = "'".$VALUE2."'";
        }

    $ROWS_IMP = implode( ', ', $ROWS );unset ($ROWS);
    $SQL_INSERT .= $ROWS_IMP;

    $SQL_INSERT .= "), "."\n";

    }

    $SQL_INSERT = trim($SQL_INSERT, '), '."\n");

    $SQL_INSERT .= "); "."\n"."\n";

    }

    return $SQL_INSERT;
}

// END FUÇÕES PARA A GERAÇÃO

// INI GERA O SCRIPT SQL
$SQL_TABLES = 'SHOW TABLES FROM `'.$DB_SELECT.'`;';

$ARRAY_ROWS_TABLES = sqlQuery( $SQL_TABLES, $CONNECT_BACKUP );

foreach ($ARRAY_ROWS_TABLES as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $EMPRESAS[] = 'SELECT * FROM `'.$VALUE2.'`;'."\n";
        
        $SQL_SELECTS[] = $EMPRESAS; unset($EMPRESAS);

    }

}

foreach ($SQL_SELECTS as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $DB_NAME_X = explode ('`',$VALUE2);

        $DB_NAME_Q = $DB_NAME_X[1];
        
        //SQL CREATE TABLE
        $SQL_CREATE_TABLES = 'SHOW CREATE TABLE `'.$DB_NAME_Q.'`;';

        $ARRAY_ROWS_CREATE_TABLES = sqlQueryView( $SQL_CREATE_TABLES, $CONNECT_BACKUP );
        
        $SQL_CREATE .= $ARRAY_ROWS_CREATE_TABLES[1].";"."\n"."\n";  

        $SQL_QUERY = sqlQuery( $VALUE2, $CONNECT_BACKUP );
        
        $SQL_INSERTS .= sqlInsert( $DB_NAME_Q, $SQL_QUERY );

    }

}
// END GERA O SCRIPT SQL

// INI MOSTRA SCRIPT SQL PRONTO

//DEBUG
print_r ( $SQL_CREATE );

//DEBUG
print_r ( $SQL_INSERTS );

// END MOSTRA SCRIPT SQL PRONTO


$HOST         = "localhost";       # HOSTING
$USER_NAME    = "root";            # USUARIO DA BASE DE DADOS
$USER_PWRD    = "123456";          # PASSWORD DA BASE DE DADOS
$DB_PRIMARY   = "data base";       # NOME DA BASE DE DADOS PRIMARIA

// TABELA SELECIONADA PARA O BACKUP
$DB_SELECT = "$DB_PRIMARY";

// CONEXÃO BACKUP
$CONNECT_BACKUP = new mysqli("$HOST", "$USER_NAME", "$USER_PWRD", "$DB_SELECT");

// SETA O CHARSET DE COMUNICAÇÃO
$CONNECT_BACKUP->set_charset('utf8');

if ($CONNECT_BACKUP->connect_errno) {
    echo "Failed to connect to MySQL: (" . $CONNECT_BACKUP->connect_errno . ") " . $CONNECT_BACKUP->connect_error;
}

// INI FUÇÕES PARA A GERAÇÃO

// SQL QUERY ARRAY
function sqlQuery( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    return $ROW_COMAND;

}

// SQL QUERY
function sqlQueryView( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    foreach ($ROW_COMAND as $KEY => $VALUE) {

        foreach ($VALUE as $KEY2 => $VALUE2) {

            $COLS[] = $VALUE2;

        }

    }

    return $COLS;

}

// SQL INSERT
function sqlInsert( $DB_TABLE, $ROW_BACKUP ) {
    
    if ( $ROW_BACKUP != null ) {

    $SQL_INSERT = 'INSERT INTO `'.$DB_TABLE.'`'."\n";
    $SQL_INSERT .= '(';

    foreach ($ROW_BACKUP[0] as $KEY => $VALUE) {

    $COLS[] = "`$KEY`";
    }

    $COLS_IMP = implode( ', ', $COLS );
    $SQL_INSERT .= $COLS_IMP;

    $SQL_INSERT .= ")"."\n";
    $SQL_INSERT .= "VALUES"."\n";

    foreach ($ROW_BACKUP as $KEY => $VALUE) {
        
    $SQL_INSERT .= "(";

        foreach ($VALUE as $KEY2 => $VALUE2) {
            $ROWS[] = "'".$VALUE2."'";
        }

    $ROWS_IMP = implode( ', ', $ROWS );unset ($ROWS);
    $SQL_INSERT .= $ROWS_IMP;

    $SQL_INSERT .= "), "."\n";

    }

    $SQL_INSERT = trim($SQL_INSERT, '), '."\n");

    $SQL_INSERT .= "); "."\n"."\n";

    }

    return $SQL_INSERT;
}

// END FUÇÕES PARA A GERAÇÃO

// INI GERA O SCRIPT SQL
$SQL_TABLES = 'SHOW TABLES FROM `'.$DB_SELECT.'`;';

$ARRAY_ROWS_TABLES = sqlQuery( $SQL_TABLES, $CONNECT_BACKUP );

foreach ($ARRAY_ROWS_TABLES as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $SELECT_IN_TABLES[] = 'SELECT * FROM `'.$VALUE2.'`;'."\n";
        
        $SQL_SELECTS[] = $SELECT_IN_TABLES; unset($SELECT_IN_TABLES);

    }

}

foreach ($SQL_SELECTS as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $DB_NAME_X = explode ('`',$VALUE2);

        $DB_NAME_Q = $DB_NAME_X[1];
        
        //SQL CREATE TABLE
        $SQL_CREATE_TABLES = 'SHOW CREATE TABLE `'.$DB_NAME_Q.'`;';

        $ARRAY_ROWS_CREATE_TABLES = sqlQueryView( $SQL_CREATE_TABLES, $CONNECT_BACKUP );
        
        $SQL_CREATE .= $ARRAY_ROWS_CREATE_TABLES[1].";"."\n"."\n";  

        $SQL_QUERY = sqlQuery( $VALUE2, $CONNECT_BACKUP );
        
        $SQL_INSERTS .= sqlInsert( $DB_NAME_Q, $SQL_QUERY );

    }

}
// END GERA O SCRIPT SQL

// INI MOSTRA SCRIPT SQL PRONTO

//DEBUG
print_r ( $SQL_CREATE );

//DEBUG
print_r ( $SQL_INSERTS );

// END MOSTRA SCRIPT SQL PRONTO

Fonte Link

Esse eu criei pra usar sem o mysqldump:


$HOST         = "localhost";       # HOSTING
$USER_NAME    = "root";            # USUARIO DA BASE DE DADOS
$USER_PWRD    = "123456";          # PASSWORD DA BASE DE DADOS
$DB_PRIMARY   = "data base";       # NOME DA BASE DE DADOS PRIMARIA

// TABELA SELECIONADA PARA O BACKUP
$DB_SELECT = "$DB_PRIMARY";

// CONEXÃO BACKUP
$CONNECT_BACKUP = new mysqli("$HOST", "$USER_NAME", "$USER_PWRD", "$DB_SELECT");

// SETA O CHARSET DE COMUNICAÇÃO
$CONNECT_BACKUP->set_charset('utf8');

if ($CONNECT_BACKUP->connect_errno) {
    echo "Failed to connect to MySQL: (" . $CONNECT_BACKUP->connect_errno . ") " . $CONNECT_BACKUP->connect_error;
}

// INI FUÇÕES PARA A GERAÇÃO

// SQL QUERY ARRAY
function sqlQuery( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    return $ROW_COMAND;

}

// SQL QUERY
function sqlQueryView( $SQL_COMAND, $CONNECT ) {

    $RESULT_COMAND  = mysqli_query($CONNECT, $SQL_COMAND);

    while ($ROW_COMAND[]  = mysqli_fetch_assoc($RESULT_COMAND )) { } array_pop($ROW_COMAND);

    foreach ($ROW_COMAND as $KEY => $VALUE) {

        foreach ($VALUE as $KEY2 => $VALUE2) {

            $COLS[] = $VALUE2;

        }

    }

    return $COLS;

}

// SQL INSERT
function sqlInsert( $DB_TABLE, $ROW_BACKUP ) {
    
    if ( $ROW_BACKUP != null ) {

    $SQL_INSERT = 'INSERT INTO `'.$DB_TABLE.'`'."\n";
    $SQL_INSERT .= '(';

    foreach ($ROW_BACKUP[0] as $KEY => $VALUE) {

    $COLS[] = "`$KEY`";
    }

    $COLS_IMP = implode( ', ', $COLS );
    $SQL_INSERT .= $COLS_IMP;

    $SQL_INSERT .= ")"."\n";
    $SQL_INSERT .= "VALUES"."\n";

    foreach ($ROW_BACKUP as $KEY => $VALUE) {
        
    $SQL_INSERT .= "(";

        foreach ($VALUE as $KEY2 => $VALUE2) {
            $ROWS[] = "'".$VALUE2."'";
        }

    $ROWS_IMP = implode( ', ', $ROWS );unset ($ROWS);
    $SQL_INSERT .= $ROWS_IMP;

    $SQL_INSERT .= "), "."\n";

    }

    $SQL_INSERT = trim($SQL_INSERT, '), '."\n");

    $SQL_INSERT .= "); "."\n"."\n";

    }

    return $SQL_INSERT;
}

// END FUÇÕES PARA A GERAÇÃO

// INI GERA O SCRIPT SQL
$SQL_TABLES = 'SHOW TABLES FROM `'.$DB_SELECT.'`;';

$ARRAY_ROWS_TABLES = sqlQuery( $SQL_TABLES, $CONNECT_BACKUP );

foreach ($ARRAY_ROWS_TABLES as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $EMPRESAS[] = 'SELECT * FROM `'.$VALUE2.'`;'."\n";
        
        $SQL_SELECTS[] = $EMPRESAS; unset($EMPRESAS);

    }

}

foreach ($SQL_SELECTS as $KEY => $VALUE) {

    foreach ($VALUE as $KEY2 => $VALUE2) {

        $DB_NAME_X = explode ('`',$VALUE2);

        $DB_NAME_Q = $DB_NAME_X[1];
        
        //SQL CREATE TABLE
        $SQL_CREATE_TABLES = 'SHOW CREATE TABLE `'.$DB_NAME_Q.'`;';

        $ARRAY_ROWS_CREATE_TABLES = sqlQueryView( $SQL_CREATE_TABLES, $CONNECT_BACKUP );
        
        $SQL_CREATE .= $ARRAY_ROWS_CREATE_TABLES[1].";"."\n"."\n";  

        $SQL_QUERY = sqlQuery( $VALUE2, $CONNECT_BACKUP );
        
        $SQL_INSERTS .= sqlInsert( $DB_NAME_Q, $SQL_QUERY );

    }

}
// END GERA O SCRIPT SQL

// INI MOSTRA SCRIPT SQL PRONTO

//DEBUG
print_r ( $SQL_CREATE );

//DEBUG
print_r ( $SQL_INSERTS );

// END MOSTRA SCRIPT SQL PRONTO