<?php
	session_start();

	$connect = mysqli_connect("localhost","root","","thahaayaa");
	$today = date('Y-m-d');
	$session_user = $_SESSION['admin_login_id'];

	function convertToReadableSize($size){
	  $base = log($size) / log(1024);
	  $suffix = array("", "KB", "MB", "GB", "TB");
	  $f_base = floor($base);
	  return round(pow(1024, $base - floor($base)), 1) . $suffix[$f_base];
	}

	$backupFile = 'database_backup_'.date("YmdHis").'.sql';
	$export = exec('D:\xampp\mysql\bin\mysqldump -u root thahaayaa  -r "C:\Users\iniya\Downloads\database1_backup_'.date("YmdHis").'.sql"', $output, $return);
	$fpath  = 'C:\Users\iniya\Downloads\database1_backup_'.date("YmdHis").'.sql';

	if(!$return){
		$qurty = mysqli_query($connect,"INSERT INTO `backup_details`(`id`, `date`,`start_date`, `end_date`, `status`,`file_name`,`file_size`, `file_path`,`created_by`,`backup_type`) VALUES (NULL,now(), now(), now(), '1','".$backupFile."','".$fsize."','".$fpath."','".$session_user."','manual')");
	} else {
		$qurty = mysqli_query($connect,"INSERT INTO `backup_details`(`id`, `date`,`start_date`, `end_date`, `status`,`file_name`,`file_size`, `file_path`,`created_by`,`backup_type`) VALUES (NULL,now(), now(), now(), '0','".$backupFile."','".$fsize."','".$fpath."','".$session_user."','manual')");
	} 
	$fsize='';
		$insert_id = mysqli_insert_id($connect);
		$fsize = convertToReadableSize(filesize('C:\Users\iniya\Downloads\database1_backup_20240827073600.sql'));
		$qurty = mysqli_query($connect,"update `backup_details` set file_size='".$fsize."' where id='".$insert_id."'");

	if($qurty){
		echo "<script>alert('Db backup done successfully!!');</script>";
		echo "<script>window.location.href='backup_details.php';</script>";
	}

?>