<?php
ini_set("session.cookie_lifetime",60*60*10);
ini_set("session.gc_maxlifetime", 60*60*10);
session_start();
//-------------------------	
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
//	
backup_tables('localhost','inreva_usersis','12345abc','inreva_inreva');


/* backup the db OR just a table */
//En la variable $talbes puedes agregar las tablas especificas separadas por comas:
//profesor,estudiante,clase
//O déjalo con el asterisco '*' para que se respalde toda la base de datos

function backup_tables($host,$user,$pass,$name,$tables = '*')
{
   
   $link = mysql_connect($host,$user,$pass);
   mysql_select_db($name,$link);
   $return="";
   //get all of the tables
   if($tables == '*')
   {
      $tables = array();
      $result = grs("show full tables where Table_Type = 'BASE TABLE'");
      while($row = $result->fetch_row())
      {
         $tables[] = $row[0];
      }
   }
   else
   {
      $tables = is_array($tables) ? $tables : explode(',',$tables);
   }
   
   //cycle through
   foreach($tables as $table)
   {
      $result = grs('SELECT * FROM '.$table);
      $num_fields = mysql_num_fields($result);
      
      
      $row2 = mysql_fetch_row(grs('SHOW CREATE TABLE '.$table));
      $return.= "\n\n".$row2[1].";\n\n";
      
    for ($i = 0; $i < $num_fields; $i++)
      {
         while($row = $result->fetch_row())
         {
            $return.= 'INSERT INTO '.$table.' VALUES(';
            for($j=0; $j<$num_fields; $j++) 
            {
               $row[$j] = addslashes($row[$j]);
               //$row[$j] = ereg_replace("\n","\\n",$row[$j]);
			   $row[$j] = preg_replace("/\n/","\\n",$row[$j]);
               if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
               if ($j<($num_fields-1)) { $return.= ','; }
            }
            $return.= ");\n";
         }
      }
      $return.="\n\n\n";
   }
   //VISTAS
   $vistas = array();
   $result = grs("show full tables where Table_Type = 'VIEW'");
   while($row = $result->fetch_row())
   {
	 $vistas[] = $row[0];
   }
   foreach($vistas as $table)
   {
      $result = grs('SELECT * FROM '.$table);
      $num_fields = mysql_num_fields($result);
      
      
      $row2 = mysql_fetch_row(grs('SHOW CREATE TABLE '.$table));
	  $StrVIEW=$row2[1];
	  $StrVIEW=str_replace("ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER"," ",$StrVIEW);
	  $StrVIEW=str_replace("ALGORITHM=UNDEFINED DEFINER=`inreva`@`localhost` SQL SECURITY DEFINER"," ",$StrVIEW);
      $return.= "\n\n".$StrVIEW.";\n\n";
   }
   //save file
   $StrFile='InrevaBackup'.date("d-m-y").'.bak';
   $handle = fopen($StrFile,'w+');
   fwrite($handle,$return);
   fclose($handle);
   //
   $filename = 'InrevaBackup.zip';	 
   if (file_exists($filename)) {
	   unlink($filename);
   }
   //
   $zip = new ZipArchive(); 
   if($zip->open($filename,ZIPARCHIVE::CREATE)===true) {
			$zip->addFile($StrFile);			
			$zip->close();
			$StrFile=$filename;
	}
	else {
			
	}
   //
   echo "El backup ha sido generado, descargelo";
   echo "<p><a href=\"$StrFile\">Backup</a>";
   echo "</p>";
   echo "<p><a href=\"menu.php\">Volver al menu</a></p>";   
}


?>