<?php

	header( 'content-type: text/html; charset=utf-8' );
	
	include('fonctions.php');
	
	echo '<a href="backup.php">retour ...</a>  
	<br>
	<br>';

	
	$fichier_sql = $_GET['fichier'];
	echo "Fichier choisi: " .$fichier_sql;
	echo '<br>';
	echo '<br>';
	
	// hebergement OVH
	$db_host    = "analogdepat.mysql.db";
	$db_user   = "analogdepat";
	$db_pass   = "Un92pac007";
	$db_name   = "analogdepat";
	
	$annee = date('Y');
	
	$sql1 = "configuration";
	$sql2 = "client";
	$sql3 = "gestion";
	$sql4 = "reservation" .$annee;
	
	//vider_table($sql1);
	//vider_table($sql2);
	//vider_table($sql3);
	//vider_table($sql4);
		
	
	//Attention : La base de données doit être vide avant l'import !!!!
	
	echo "Votre base est en cours de restauration.......<br>";
	echo '<br>';
	//system("cat dump.sql | mysql --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --database=analogdepat");
	#Import du fichier /chemin/vers/le/fichier.sql dans la base nom_de_la_base
	//mysql -h localhost -u utilisateur -p"mot_de_passe" nom_de_la_base < /chemin/vers/le/fichier.sql
	//system("cat nom_de_la_base.sql | mysql --host=serveur_sql --user=nom_de_la_base --password=mot_de_passe nom_de_la_base");
	
	//$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]
	
	$sql = "mysql --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 analogdepat < /Backup/" .$fichier_sql;
	//system($sql);
	echo "Commande executée : " .$sql;
	echo '<br>';
	echo '<br>';
	echo "Traitement réussi.";
	

function restaurer($fichier)
{

	// Temporary variable, used to store current query
	$templine = '';
	// Read in entire file
	$lines = file($filename);
	// Loop through each line
	foreach ($lines as $line)
	{
		// Skip it if it's a comment
		if(substr($line, 0, 2) == '--' || $line == '')
			continue;
 
		// Add this line to the current segment
		$templine .= $line;
		// If it has a semicolon at the end, it's the end of the query
		if (substr(trim($line), -1, 1) == ';')
		{
			// Perform the query
			mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
			// Reset temp variable to empty
			$templine = '';
		}
	}	
}	

function vider_table($table_a_vider)
{
	$connexion = DB(); 
	
	$sql  = "TRUNCATE TABLE ".$table_a_vider; 
	echo $sql;
	echo '<br>'; 
	echo "La table ".$table_a_vider." a été vidée !"; 
	echo '<br>'; 
} 

?>