#!/usr/local/bin/php

<?php

	//define('_PATH', dirname(__FILE__));
	
	//include('database.php');
	//DB_connexion(); 
   
/*****************************************************************************/   
   
function backup_tables($host, $user, $pass, $db, $tables )  // $tables = '*'
{
	
			
	
			$data = "\n/*---------------------------------------------------------------".
					"\n  SQL DB BACKUP ".date("d.m.Y H:i")." ".
					"\n  HOST: {$host}".
					"\n  DATABASE: {$name}".
					"\n  TABLES: {$tables}".
					"\n  ---------------------------------------------------------------*/\n";


					$connexion = mysql_connect($host,$user,$pass); 
					if(!$connexion)
					{
						die('Could not connect: ' . mysql_error());
					}
 
					mysql_select_db($db, $connexion); 

					//$connexion = mysql_connect($host,$user,$pass);
					//mysql_select_db($name,$connexion);
					mysql_query( "SET NAMES `utf8` COLLATE `utf8_general_ci`" , $connexion ); // Unicode

					if($tables == '*')
					{ 	//get all of the tables
						$tables = array();
						$result = mysql_query("SHOW TABLES");
						while($row = mysql_fetch_row($result))
						{
							$tables[] = $row[0];
						}
					}
					else
					{
						$tables = is_array($tables) ? $tables : explode(',',$tables);
					}

					//echo " Nombres de table incluses dans le backup " .count($tables);
					//echo "<br/>";
					//echo "<br/>";

					//print_r($tables);
  
					foreach($tables as $table)
					{
						$data.= "\n/*---------------------------------------------------------------".
								"\n  TABLE: `{$table}`".
								"\n  ---------------------------------------------------------------*/\n";  
		
						$data.= "DROP TABLE IF EXISTS `{$table}`;\n";
						$res = mysql_query("SHOW CREATE TABLE `{$table}`", $connexion);
						$row = mysql_fetch_row($res);
						$data.= $row[1].";\n";

						$result = mysql_query("SELECT * FROM `{$table}`", $connexion);
						$num_rows = mysql_num_rows($result);    

						if($num_rows>0)
						{
		
							$vals = Array(); $z=0;
		  
							for($i=0; $i<$num_rows; $i++)
							{
								$items = mysql_fetch_row($result);
								$vals[$z]="(";
								
								for($j=0; $j<count($items); $j++)
								{
									if (isset($items[$j])) 
									{ 
										$vals[$z].= "'".mysql_real_escape_string( $items[$j], $connexion )."'"; 
									} 
									else 
									{ 
										$vals[$z].= "NULL";
										}
									if ($j<(count($items)-1))
									{ 
										$vals[$z].= ",";
									}
								}
								
								$vals[$z].= ")"; $z++;
							}
		  
							$data.= "INSERT INTO `{$table}` VALUES ";      
							$data .= "  ".implode(";\nINSERT INTO `{$table}` VALUES ", $vals).";\n";
						}
					}
					
					
					//echo "<br/>";
					//echo "<br/>";
					
					//echo "******** Data extraites ********";
					
					//echo "<br/>";
					//echo "<br/>";
					
					//echo "<pre>";
					//print_r($data);
					//echo "<pre/>";

					//echo "<br/>";
					//echo "<br/>";
  
					mysql_close( $connexion );
					
 
					//echo "******** Nom du fichier ********";
					//echo "<br/>";
					$backup_file = 'toto_blitz_db-backup-'.date("d.m.Y_H:i").'.sql';
					//echo $backup_file;
					
					//echo "<br/>";
					//echo "<br/>";

					//echo "******** Nom du l'archive aprés compression ********";
					//echo "<br/>";
					$backup_file_zipped = 'toto_blitz_db-backup-'.date("d.m.Y_H:i").'.zip';
					//echo $backup_file_zipped;
			
			
					$handle = fopen($backup_file,'w+');
					fwrite($handle,$data);
					fclose($handle);
			
					zip_file($backup_file, $backup_file_zipped);
  
}

/*****************************************************************************/   

function zip_file($backup_file, $backup_file_zipped)
{
	$zip = new ZipArchive;
	if ($zip->open($backup_file_zipped , ZipArchive::CREATE | ZipArchive::OVERWRITE ) === TRUE)
	{
		// Add file to the zip file
		
		
		
		//$chemin_zip = _PATH."/backup_blitz/";
		
		$chemin_zip = "/homez.528/analogde/www/WORKAREA/WEBSITE/BLITZ/backup_blitz/";
		
		//echo ">>>>> " $chemin_zip;
		
		$zip->addFile($backup_file);
		// All files are added, so close the zip file.
		$zip->close();
	}
	
	copy($backup_file_zipped, 'backup_blitz/'.$backup_file_zipped);
	
	// suppression des fichiers 
	unlink($backup_file);
	unlink($backup_file_zipped);
	
	
}


/*****************************************************************************/   

	$db_host    = "analogdepat.mysql.db";
   	$db_user   = "analogdepat";
 	$db_pass   = "Un92pac007";
	$db_name   = "analogdepat";

	$backup_file = 'db-backup-'.time().'.sql';
	$backup_file = 'db-backup-'.date("d.m.Y_H:i").'.sql';

	$tables= "BLITZ_affectation,BLITZ_classement,BLITZ_evolution,BLITZ_parties";

	//$mybackup = backup_tables( $db_host, $db_user, $db_pass , $db_name, $tables); // si on veut toutes les table "*"
	backup_tables( $db_host, $db_user, $db_pass , $db_name, $tables); // si on veut toutes les table "*"


?>