File "automatic_backup.php"

Full Path: /home/analogde/www/Massage_debug_15_11_2017/automatic_backup.php
File size: 3.73 KB
MIME-type: text/x-php
Charset: utf-8

<?php

	include('database.php');

	date_default_timezone_set('Europe/Paris');

function backup_tables($host,$user,$pass,$name)
{
	$annee = date('Y');
	$table_reservation  = "reservation" .$annee;
	$link = mysql_connect($host,$user,$pass);
	mysql_select_db($name,$link);


	$tables = array();
	$tables[] = "configuration";
	$tables[] = "clients";
	$tables[] = "gestion";
	$tables[] = $table_reservation;

	foreach($tables as $table)
	{
		$result = mysql_query('SELECT * FROM '.$table);
		$num_fields = mysql_num_fields($result);
		$return.= 'DROP TABLE '.$table.';';
		$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysql_fetch_row($result))
			{
				$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]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j<($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}

		$return.="\n\n\n";
	}

	$jour = date('d-m-Y');
	$heure = date("H_i_s");
	//$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
	$infos = $jour.'-'.$heure;
	chdir('Backup');
	$handle = fopen('db-backup-'.$infos.'.sql','w+');
	//$handle = fopen($infos.'.sql','w+');
	fwrite($handle,$return);
	fclose($handle);

}

	//echo filemtime("automatic_backup.php");

	$ts_actuel = strtotime(date("d-m-Y H:i:s"));
	echo $ts_actuel;	
	//chdir('Backup');
	$dir    = './Backup';
	$files = scandir($dir);
	
	echo '<pre>';
	print_r($files);
	
	$tab_ts = array();
	
	//echo filemtime("db-backup.sql");
	
	for( $i=0; $i < count($files) ; $i++)
	{
		if($i>= 2)
		{ 
			$file_name = $files[$i];
			$file_name = explode(".",$file_name);
			$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);
			echo $format ."    " .$ts;
			echo '<br>';
			//http://www.timestamp.fr/?
			
			$tab_ts[] = $ts;
		}
	}	

	echo '<pre>';
	print_r($tab_ts);
	
	sort($tab_ts);

	$arrlength = count($tab_ts);
	for($x = 0; $x <  $arrlength; $x++)
	{
		echo $tab_ts[$x];
		echo "<br>";
	}
	
	/*$file = "db-backup-13-03-2015-09_38_59.sql";
	
	$file = explode(".",$file);

	$date_pieces = explode("-",$file[0]);
	print_r($date_pieces);
	
	$date_tab[0] = $date_pieces[2];
	$date_tab[1] = $date_pieces[3];
	$date_tab[2] = $date_pieces[4];
	
	print_r($date_tab);
	
	$heure_tab = explode("_",$date_pieces[5]);
	print_r($heure_tab);
	
	//echo date("d/m/Y H:i:s",timestamp);     // 10/03/2007 15:25:37
	
	$date = $date_tab[0]."-".$date_tab[1]."-".$date_tab[2]; // d-m-Y
	$ts = strtotime($date);
	echo $ts;
			*/
			
	$seconde_par_jour = 60*60*24;	

	/*for($i = 0; $i <  count($tab_ts); $i++)
	{
		$res = ($ts_actuel - $tab_ts[$i]) / $seconde_par_jour ;
		echo $res;
		echo "<br>";
	}	*/
	
	$res = ($ts_actuel - $tab_ts[count($tab_ts)-1]) / $seconde_par_jour ;
	echo $res;
	echo "<br>";
	
	if($res > 1.0)
	{
		error_reporting(0);
		

		$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";
		}
		//$connexion = DB(); 
		echo "Detection";
		backup_tables($db_host,$db_user,$db_pass,$db_name);
		
	}
	
?>