#!/usr/local/bin/php
<?php
	header( 'content-type: text/html; charset=utf-8' );

	error_reporting(0);
	date_default_timezone_set('Europe/Paris');

	$site = $_SERVER["SERVER_NAME"];
	if( ($site == "localhost") || ($site == "127.0.0.1") ) 
	{

		$db_host    = "localhost";
		$db_user   = "root";
		$db_pass   = ""; 
		$db_name   = "test";
	}
	else
	{

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

	
		$db_host    = "analogdepat.mysql.db";
		$db_user   = "analogdepat";
		$db_pass   = "Un92pac007";
		$db_name   = "analogdepat";
		
		
		$jour = date('d-m-Y');
		$heure = date("H_i_s");
		$infos = $jour.'-'.$heure;
		$fichier = 'db-backup-'.$infos.'.sql';
		
		// Chemin absolu de l'emplacement du site
		$chemin_absolu_site = 'www' ;
		// Emplacement sur le FTP du répertoire des sauvegardes
		$repertoire_sauvegardes = $chemin_absolu_site.'/Massage/Backup/' ; 
		
		
	//system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");

	//$fichier = '/www/Massage/Backup/zzdb-backup-'.$infos.'.sql';
		
	//system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");

	//$fichier = $_SERVER["DOCUMENT_ROOT"] ."Massage/Backup"
	
	//system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");

	//$fichier = $_SERVER["DOCUMENT_ROOT"].'/Massage/Backup/xxdb-backup-'.$infos.'.sql';
	
	//system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");


	$fichier = '/homez.65/analogde/www/Massage/Backup/auto_db-backup-'.$infos.'.sql';
	
	system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");

	//$fichier = '../www/Massage/Backup/aadb-backup-'.$infos.'.sql';
	
	//system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");


	//system("gzip dump.sql");
	
	$repertoire = '/homez.65/analogde/www/Massage/Backup';
	listing_fichier_repertoire($repertoire); 
	
function listing_fichier_repertoire($repertoire) 
{
    $results = array();
    $handler = opendir($repertoire);

	$cpt = 0;
		
	$ts_actuel = strtotime(date("d-m-Y H:i:s"));
		
    while ($file = readdir($handler)) 
	{
		if($file != "." && $file != ".." && $file != "listing_backup.txt") 
		{
			$file_name = explode(".",$file);
			$date_pieces = explode("-",$file_name[0]);
			$date_tab[0] = $date_pieces[2];
			$date_tab[1] = $date_pieces[3];
			$date_tab[2] = $date_pieces[4];
			$heure_tab = explode("_",$date_pieces[5]);
			$date = $date_tab[0]."-".$date_tab[1]."-".$date_tab[2]; // d-m-Y
			$heure = $heure_tab[0].":".$heure_tab[1].":".$heure_tab[2]; 
			$format = $date ." " .$heure;

			$ts = strtotime($format);
			$ts_difference = $ts_actuel - $ts;
			$ts_7jours = 5 * 24 * 3600;
			if($ts_difference >= $ts_7jours)
			{
				//echo $file ." ----  " .$format ."    " .$ts ."   trop ancien" ;
				//chdir($repertoire);
				$chemin = '/homez.65/analogde/www/Massage/Backup/' .$file;
				unlink($chemin);
			}
			/*else
			{
				echo $format ."    " .$ts ;
			}*/
			//echo '<br>';
			
			/*http://www.timestamp.fr/?*/
			
			$results[$cpt][0] = $file;
			$results[$cpt][1]= $ts;
			$results[$cpt][2]= filesize($file);
			$cpt = $cpt + 1;
		}
    }
	
	
    closedir($handler);
  
}
	
/*	
	$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';

$backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass ".
           "test_db | gzip > $backup_file";

system($command);

/*	
	$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$table_name = "employee";
$backup_file  = "/tmp/employee.sql";
$sql = "LOAD DATA INFILE '$backup_file' INTO TABLE $table_name";

mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not load data : ' . mysql_error());
}
echo "Loaded  data successfully\n";
mysql_close($conn);

/*$table = 'reservation2015 configuration client  gestion'; //Nom des tables à sauvegarder - Optionnel
$rep = '/Backup'; //Répertoire où sauvegarder le dump de la base de données

 system("mysqldump --host=".$db_host." --user=".$db_user." --password=".$db_pass." 
 ".$db_name." ".$table."  > ".$rep.$db."-".date("d-m-Y-H\hi").".sql");
 
 */
 
 
/* echo "Votre base est en cours d'exportation.......<br>";
 system("mysqldump --host=localhost --user=root7 --password= --no-create-db --default-character-set=utf8 --lock-tables=FALSE --tables test >dump.sql");
 print_r(file("dump.sql")); // affiche le dump ou l'erreur de mysqldump
 echo "C'est fini. Votre base est exporté vers le fichier dump.sql";
 */
 
/*
echo "Votre base est en cours de restauration.......
<br>";
system("cat nom_de_la_base.sql | mysql --host=serveur_sql --user=nom_de_la_base --password=mot_de_passe nom_de_la_base");
echo "C'est fini. Votre base est en place sur cet hébergement.";


<?php
echo "Décompression du fichier.....
<br>";
system("gunzip testbackup.sql.gz");
echo "Votre base est en cours de restauration......
<br>";
system("cat testbackup.sql | mysql --host=mysql5-21.pro --user=testimport --password=RtPgDsmLE testimport");
echo "C'est fini. Votre base est en place sur cet hébergement.";
?>
?>
*/
?>