<?php
session_start();


	function backup()
	{
	//	include '../db_connect.php';
  $suffix = date("d-m-Y-hms");
  #Execute the command to create backup sql file
   exec("/usr/bin/mysqldump --user={$username} --password={$password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --create-options {$db} > backup.sql");
  //exec("D:\\xampp\\mysql\\bin\\mysqldump --user={$username} --password={$password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --create-options {$db} > backup.sql");
  
  #Now zip that file
  $zip = new ZipArchive();
  $filename = "$_SESSION[SESS_CMPC]_backup-$suffix.zip";
  if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
   exit("cannot open <$filename>\n");
  }
  $zip->addFile("backup.sql" , "backup.sql");
  $zip->close();
  
if(file_exists($filename))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($filename);
// remove zip file is exists in temp path
unlink($filename);
}
  
  
  #Now delete the .sql file without any warning
  @unlink("backup.sql");
  #Return the path to the zip backup file
  return "backup-$suffix.zip";
  
	}
	
function delete_log($con)
{
	
	$sqlQry = "DELETE FROM `logtable` WHERE `logtable`.`entrydate` <=  NOW() - INTERVAL 3 MONTH";
	if(!$sqlRes=mysqli_query($con,$sqlQry))
	{
		echo "error in log deletion";
	}
}
	include '../db_connect.php';
	delete_log($con);
	backup($con);
 // header("location: logout.php");
  ?>