#!/usr/local/bin/php
<?php
/*#!/usr/local/bin/php*/

				include('database.php');
				DB_connexion(); 
	
				$tables= "BLITZ_affectation,BLITZ_classement,BLITZ_evolution,BLITZ_parties";
		
				$data = "\n/*---------------------------------------------------------------".
					"\n  SQL DB BACKUP ".date("d.m.Y H:i")." ".
					"\n  HOST: {$host}".
					"\n  DATABASE: {$name}".
					"\n  TABLES: {$tables}".
					"\n  ---------------------------------------------------------------*/\n";

					
					//mysql_query( "SET NAMES `utf8` COLLATE `utf8_general_ci`" ); // 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);
					}

					
  
					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}`", $link);
						$row = mysql_fetch_row($res);
						$data.= $row[1].";\n";

						$result = mysql_query("SELECT * FROM `{$table}`", $link);
						$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], $link )."'"; 
									} 
									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 $data;
					
					$chemin = '/homez.65/analogde/www/WORKAREA/WEBSITE/BLITZ/backup_blitz';
	
					chdir($chemin);
 
					$backup_file = 'blitz_db-backup-'.date("d.m.Y_H:i").'.sql';
					$handle = fopen($backup_file,'a+');
					fwrite($handle,$data);
					fclose($handle);
			
	
?>
