<?php
/*  To restrore table use "mysql -u root -p scanner < backup_scanner.sql"  commnad*/

require_once 'web_constant.php';
include("header.php");
include 'backup_function.php';
?>
<div class="content">
    <h2>Database Backup</h2>
	<div id="backup_timer_div" style=" padding: 20px; background-color: #2196F3; color: white;margin-bottom: 15px;">🕒 Please wait... Taking database backup</div>
    <?php
		//$tables=['ticl_detail'];
		$tables=['ticl_detail','scanner','ems','sidmotor'];
		
		$backup_path=web_constant::$databse_backup_path;
		$table_array_cnt=1;
		foreach($tables as $table){
			backup_tables(web_constant::$host, web_constant::$user, web_constant::$password, $table,$backup_path);
			$table_array_cnt++;
			if($table_array_cnt==sizeof($tables)){
				echo '<script>const backup_timer_div = document.getElementById("backup_timer_div");backup_timer_div.remove();</script>';
			}
		}
	?>
</div>
<?php
// Multiple databases
//backup_tables('localhost', 'root', 'root', ['scanner', 'ticl_detail']);
/*
backup_tables('localhost','root','','scanner');

function backup_tables($host,$user,$pass,$name,$tables = '*')
{	
	include "connect.php";
	if($tables == '*')
	{
		$tables = array();
		$result=mysqli_query($con,'SHOW TABLES');
		while($row = mysqli_fetch_row($result))
		{
			$tables[] = $row[0];
		}
	}
	else
	{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	$return="";
	foreach($tables as $table)
	{
		$result = mysqli_query($con,'SELECT * FROM '.$table);
		$num_fields=mysqli_num_fields($result);
		$return.= 'DROP TABLE IF EXISTS '.$table.';';
		$row2=mysqli_fetch_row(mysqli_query($con,'SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysqli_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j < $num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j]=preg_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";
	}
	$handle = fopen('\\\192.168.0.3\e$\EMS Scanner Backup\Scanner\scanner-db-backup-'.date("d-F-Y").'-'.$name.'.sql','w+');
	//$handle = fopen('\\\192.168.0.3\scanbackup\scanner-db-backup-'.date("d-F-Y").'-'.$name.'.sql','w+');
	//$handle = fopen('e:\xampp\backup\scanner-db-backup-'.date("d-F-Y").'-'.$name.'.sql','w+');
	if(fwrite($handle,$return)>0)
	{
		?>
		<div class="container">
		  <div class="alert alert-success">
			<strong>Success:</strong><?php echo 'Data backup with name = '.'db-backup-'.date("d-F-Y").'-'.$name.'.sql'; ?>
		  </div>
		</div>
		<?php
	}
	else
	{
		?>
		<div class="container">
			<div class="alert alert-danger">
				<strong>Erro!</strong> <?php echo "while backup data"; ?>.
			</div>
		</div>
		<?php
	}
	fclose($handle);
}
*/
include 'footer.php';
?>