git-svn-id: https://192.168.0.254/svn/Proyectos.Incam_Intranet/trunk@1 e2b1556b-49f8-d141-9351-52d6861a72d9
198 lines
6.5 KiB
PHP
198 lines
6.5 KiB
PHP
<?php
|
||
/*
|
||
* Clase BD
|
||
*
|
||
* Sirve para conectarse a la base de datos y realizar consultas sobre esta
|
||
*/
|
||
|
||
include_once("ConexionBackup.php");
|
||
include_once("BDBackup.php");
|
||
include_once("BD.php");
|
||
|
||
class Backup{
|
||
|
||
private $nombre_bd;
|
||
|
||
private $usuario;
|
||
|
||
public function Backup($usuario){
|
||
$conexion = new ConexionBackup();
|
||
$this->nombre_bd = $conexion->getNombreBD();
|
||
$this->usuario = $usuario;
|
||
}
|
||
|
||
public function backupGlobal(){
|
||
if(!$this->usuario->tieneRol(1)){
|
||
$error = $this->locale['4042'];
|
||
throw new Exception($error);
|
||
return false;
|
||
exit;
|
||
}
|
||
$consulta = "SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema='".$this->nombre_bd."'";
|
||
$bdB = new BDBackup();
|
||
$bd = new BD();
|
||
|
||
$db_tables = $bdB->arrayQuery($consulta, "TABLE_NAME");
|
||
|
||
if(count($db_tables)>0){
|
||
$crlf = "\n";
|
||
//ob_start();
|
||
//@ob_implicit_flush(0);
|
||
$backup = "#----------------------------------------------------------".$crlf;
|
||
$backup .= "# Intranet de Selfor SQL Data Dump".$crlf;
|
||
$backup .= "# Fecha: `".date("d/m/Y H:i")."`".$crlf;
|
||
$backup .= "#----------------------------------------------------------".$crlf;
|
||
$backup .= $crlf."SET FOREIGN_KEY_CHECKS=0;".$crlf."#".$crlf;
|
||
mysql_query('SET SQL_QUOTE_SHOW_CREATE=1');
|
||
foreach($db_tables as $table){
|
||
@set_time_limit(1200);
|
||
mysql_query("OPTIMIZE TABLE $table");
|
||
$backup .= $crlf."#".$crlf."# Structure for Table `".$table."`".$crlf."#".$crlf;
|
||
$backup .= "DROP TABLE IF EXISTS `$table`;$crlf";
|
||
$row=mysql_fetch_array($bd->execQuery("SHOW CREATE TABLE $table"));
|
||
$backup .= $row[1].";".$crlf;
|
||
$result=mysql_query("SELECT * FROM $table");
|
||
if($result&&mysql_num_rows($result)){
|
||
$backup .= $crlf."#".$crlf."# Table Data for `".$table."`".$crlf."#".$crlf;
|
||
$column_list="";
|
||
$num_fields=mysql_num_fields($result);
|
||
for($i=0;$i<$num_fields;$i++){
|
||
$column_list.=(($column_list!="")?", ":"")."`".mysql_field_name($result,$i)."`";
|
||
}
|
||
}
|
||
while($row=mysql_fetch_array($result)){
|
||
$dump="INSERT INTO `$table` ($column_list) VALUES (";
|
||
for($i=0;$i<$num_fields;$i++){
|
||
$dump.=($i>0)?", ":"";
|
||
if(!isset($row[$i])){
|
||
$dump.="NULL";
|
||
}elseif($row[$i]=="0"||$row[$i]!=""){
|
||
$type=mysql_field_type($result,$i);
|
||
if($type=="tinyint"||$type=="smallint"||$type=="mediumint"||$type=="int"||$type=="bigint"||$type=="timestamp"){
|
||
$dump.=$row[$i];
|
||
}else{
|
||
$search_array=array('\\','\'',"\x00","\x0a","\x0d","\x1a");
|
||
$replace_array=array('\\\\','\\\'','\0','\n','\r','\Z');
|
||
$row[$i]=str_replace($search_array,$replace_array,$row[$i]);
|
||
$dump.="'$row[$i]'";
|
||
}
|
||
}else{
|
||
$dump.="''";
|
||
}
|
||
}
|
||
$dump.=');';
|
||
$backup .= $dump.$crlf;
|
||
}
|
||
}
|
||
$backup .= $crlf."SET FOREIGN_KEY_CHECKS=1;".$crlf."#".$crlf;
|
||
$ruta = $this->backupMysql($backup);
|
||
if($ruta != ""){
|
||
$aciertos = "Backup realizado";
|
||
return $ruta;
|
||
}else{
|
||
$errores = "Fallo al realizar el backup";
|
||
return "";
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/* Descargar la base de datos en gzip (al m<>ximo) y enviar por mail.*/
|
||
private function backupMysql($backup){
|
||
// Crear nombres de los ficheros backup
|
||
$fecha=time(); // Fecha actual
|
||
$archivo=gmstrftime("%y-%m-%d",$fecha); // Formato de la fecha para dar nombre al fichero
|
||
$asunto = 'Base de datos '.$archivo;
|
||
$ruta = constante("srcDocs")."/bdcopy/";
|
||
$ruta=$ruta.$archivo.'.sql'; // Archivo sql
|
||
$archivo_gz=$ruta.$archivo.'.gz';
|
||
$resultado = $archivo.'.sql'.$archivo.'.gz';
|
||
$fp = fopen($ruta, "a");
|
||
$write = fputs($fp, $backup);
|
||
fclose($fp);
|
||
$tamano_archivo = $fp['size'];
|
||
// Archivo sql.gz
|
||
|
||
// Descarga de la base de datos
|
||
if(file_exists($ruta)) // Si ha creado correctamente el backup
|
||
{
|
||
$comprimido=$this->comprimir($ruta,$archivo_gz); // Comprimirlo en gzip
|
||
if($comprimido=='TRUE'){
|
||
//$this->enviarAdjunto ($archivo_gz, $asunto); // Envio de mail comprimido
|
||
}else{
|
||
//$this->enviarAdjunto ($fp, $asunto); // Envio de mail sin comprimir
|
||
}
|
||
|
||
@unlink($ruta);
|
||
//TODO ruta segura // Borrar fichero sql
|
||
//@unlink($archivo_gz); // Borrar fichero sql.gz
|
||
|
||
//return TRUE; // Regresa confirmado el backup
|
||
return $resultado;
|
||
}
|
||
else // No ha podido crear el backup
|
||
return ""; // Regresa denegando el backup
|
||
}
|
||
|
||
// Comprimir en gzip un archivo
|
||
private function comprimir($archivo_original,$archivo_comprimido)
|
||
{
|
||
$fp=@gzopen($archivo_comprimido,'w9'); // Crear archivo comprimido
|
||
if($fp!='FALSE') // Comprobar que zLib esta activo
|
||
{
|
||
$fp2=@fopen($archivo_original,'r'); // Abrir archivo original
|
||
$buffer=@fread($fp2,filesize($archivo_original));// Leer archivo original
|
||
@fclose($fp2); // Cerrar archivo original
|
||
@gzwrite($fp,$buffer); // Escribir archivo comprimido
|
||
@gzclose($fp); // Cerrar archivo comprimido
|
||
return TRUE; // Regresa confirmado la compresi<73>n
|
||
}
|
||
else
|
||
return FALSE; // Regresa sin comprimirlo
|
||
}
|
||
|
||
// // ENVIO DE CORREO CON ADJUNTO COMPRIMIDO
|
||
// private function enviarAdjunto($archivo,$asunto)
|
||
// {
|
||
// global $mail_enviar;
|
||
//
|
||
// $buffer = implode("", file($archivo)); // Leer fichero
|
||
// $buffer=chunk_split(base64_encode($buffer)); // Codificaci<63>n en base64 y divido
|
||
//
|
||
// // Cabeceras
|
||
// $cabecera = "MIME-version: 1.0\n";
|
||
// $cabecera .= "Content-type: multipart/mixed; ";
|
||
// $cabecera .= "boundary=\"Message-Boundary\"\n";
|
||
// $cabecera .= "Content-transfer-encoding: 7BIT\n";
|
||
// $cabecera .= "X-attachments: $archivo";
|
||
//
|
||
// // Mensaje
|
||
// $mensaje = "--Message-Boundary\n";
|
||
// $mensaje .= "Content-type: text/plain; charset=ISO-8859-1\n";
|
||
// $mensaje .= "Content-transfer-encoding: 7BIT\n";
|
||
// $mensaje .= "Content-description: Mail message body\n\n";
|
||
//
|
||
// // Adjuntar el fichero
|
||
// $mensaje .= "\n\n--Message-Boundary\n";
|
||
// $mensaje .= "Content-type: Binary; name=\"$archivo\"\n";
|
||
// $mensaje .= "Content-Transfer-Encoding: BASE64\n";
|
||
// $mensaje .= "Content-disposition: attachment; filename=\"$archivo\"\n\n";
|
||
// $mensaje .= "$buffer\n";
|
||
// $mensaje .= "--Message-Boundary--\n";
|
||
//
|
||
// @mail($mail_enviar,$asunto,$mensaje,$cabecera); // Envio de mail
|
||
// }
|
||
|
||
function getLink(){
|
||
if(!$this->usuario->tieneRol(1)){
|
||
$error = $this->locale['4042'];
|
||
throw new Exception($error);
|
||
return false;
|
||
exit;
|
||
}
|
||
return $this->conexion->getlink();
|
||
}
|
||
|
||
}
|
||
?>
|