En el foro ya han sugerido métodos para hacer un backup o dumpear la base de datos. Este es el que yo uso para hacerlo con un cron job.
Código PHP:
Ver original<?php
# % -> esta línea la pueden quitar es para evitar un error con el highlight
define('DB_HOST', 'localhost'); define('DB_PASS', 'contraseña');
function setQuery($setSelectQueryStr, $fetchType = NULL){
try{
$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdoQuery = $db->query($setSelectQueryStr);
$fetchType = empty($fetchType) ? PDO
::FETCH_ASSOC : $fetchType; $arr = ($pdoQuery->rowCount() > 0) ?
$pdoQuery->fetchAll($fetchType) : array();
}catch(PDOException $e){
echo $e->getMessage();
}
return $arr;
}
$drop = true;
$f = fopen(DB_NAME
. '.sql', 'w');
$query = setQuery('SHOW TABLES FROM `' . DB_NAME . '`', PDO::FETCH_NUM);
foreach($query as $row){
$tables[] = $row[0];
}
$extra['dumpVersion'] = "1.0";
$extra['dtTm'] = date("Y-m-d H:i:s"); $extra['serverVersion'] = $db->getAttribute(PDO::ATTR_SERVER_VERSION);
$extra['dbName'] = DB_NAME;
foreach($tables as $k => $v){
$t[] = "[$k] => $v;";
}
$extra['tables'] = implode(NL
. '-- ', $t);
$text = <<<HEADERTEXT
-- dumpFDW
-- version {$extra['dumpVersion']}
-- http://www.forosdelweb.com/miembros/abimaelrc/
--
-- Host: {$_SERVER['HTTP_HOST']}
-- Generation Time: {$extra['dtTm']}
-- Server version: {$extra['serverVersion']}
-- PHP Version: {$extra['phpVersion']}
-- Database: '{$extra['dbName']}'
-- Tables: {$extra['tables']}
HEADERTEXT;
foreach ($tables as $table){
fwrite($f, NL
. NL
.($drop ?
"DROP TABLE IF EXISTS `$table`;" : "-- No especificado.") . NL
);
$query = setQuery("SHOW CREATE TABLE `$table`", PDO::FETCH_NUM);
foreach($query as $row){
foreach($arr as $key => $value){
if(stripos($value, "CONSTRAINT") !== false){ $tmpArr[] = ' ADD ' . trim($value); }
}
}
$constraints[] = 'ALTER TABLE ' . $row[0] . NL
. implode(NL
, $tmpArr) . ';'; }
}
$query = setQuery("SELECT * FROM `$table`");
$n = 0;
foreach($query as $qry){
foreach($columnas as $columna){
$keys[] = "`".$columna."`";
$values[] = $qry[$columna];
} else{
}
}
/* $sC = special Char, saber cual cáracter especial colocar al final del código */
$sC = ($n%2000 == 1999 || $n == $nR ? ";" : ",");
if($n%2000 == 0){
fwrite($f, "INSERT INTO `$table`(".implode(", ", $keys).") VALUES " . NL
. "(" . implode(", ", $values) . ")" . $sC . NL
); }else{
}
$n++;
}
}
if(!empty($constraints)){ }
Otros temas para dumpear son
http://www.forosdelweb.com/f18/backu...o-1-0b-166532/
y
http://www.forosdelweb.com/f54/tutor...igdump-376908/
Nota:
12-29-2010 Se quito la clase singleton, no es útil para base de datos.