Tuesday 17 February 2015

MySQL Database Backup using PHP

backup.php

<?php
session_start();
$user ='root';
$pass ='test';
$db ='bg';
$backup_dir ='db_backups';
$filename ='db_backup_'.$_SESSION['username'].'_'.date('d-m-Y_H-i-s').'.sql';

$mysqldumppath='C:\\wamp\\bin\\mysql\\mysql5.5.8\\bin\\';
$result=exec($mysqldumppath.'mysqldump '.$db.' --password='.$pass.' --user='.$user.' --single-transaction >'.$backup_dir.'/'.$filename,$output);
$_SESSION['downfname']=$backup_dir.'/'.$filename;
?>
<center>
<br>
<h1 style="color:#FF3366">Backup Complete</h1>
<br>
<form method="post" >
<input type="submit" name="submit" value="Download Backup">
</form>
<br><br><br>
</center>
<br>
<?php
if(isset($_POST['submit'])){
header('Location:download_backup.php');
}
?>

download_backup.php

<?php
session_start();

$file = $_SESSION['downfname'];

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}


?>

No comments:

Post a Comment

Popular Posts