This small example will show you in a nutshell how to take the backup of MySQL database and also how to restore a database.
Create a file called MySQLbackup.php in you server or you can give it to any other name you wish.
Now add the following html code to that file.
MySQL database backup
Next I have used the following javascript code to validate the input fields.
Next add the following php code.
< ?php
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['databasename'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$databasename = trim($_POST['databasename']);
$backupRestore = $_POST['backupRestore'];
if ($backupRestore == 'backup') {
$data = $_POST['data'];
$now = str_replace(":", "", date("Y-m-d H:i:s"));
$outputfilename = $databasename . '-' . $now . '.db';
$outputfilename = str_replace(" ", "-", $outputfilename);
//Dump the MySQL database
if ($data == "wd") {
//With data
exec('mysqldump -u ' . $username . ' -p' . $password . ' ' . $databasename . ' > ' . $outputfilename);
} else {
//Without data
exec('mysqldump --no-data -u ' . $username . ' -p' . $password . ' ' . $databasename . ' > ' . $outputfilename);
}
//Download the database file
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($outputfilename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($outputfilename));
ob_clean();
flush();
readfile($outputfilename);
//After download remove the file from server
exec('rm ' . $outputfilename);
} else { //Restore the database
$target_path = getcwd();
$databasefilename = $_FILES["databasefile"]["name"];
//Upload the database file to current working directory
move_uploaded_file($_FILES["databasefile"]["tmp_name"], $target_path . '/' . $databasefilename);
//Restore the database
exec('mysql -u ' . $username . ' -p' . $password . ' ' . $databasename . ' < ' . $databasefilename);
//Remove the file from server
exec('rm ' . $databasefilename);
}
}
?>
And finally the file will look like following.
< ?php
if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['databasename'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$databasename = trim($_POST['databasename']);
$backupRestore = $_POST['backupRestore'];
if ($backupRestore == 'backup'){
$data = $_POST['data'];
$now = str_replace(":", "", date("Y-m-d H:i:s"));
$outputfilename = $databasename . '-' . $now . '.db';
$outputfilename = str_replace(" ", "-", $outputfilename);
//Dump the MySQL database
if ($data == "wd"){
//With data
exec('mysqldump -u '. $username .' -p'. $password .' '. $databasename .' > '. $outputfilename);
}
else{
//Without data
exec('mysqldump --no-data -u '. $username .' -p'. $password .' '. $databasename .' > '. $outputfilename);
}
//Download the database file
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($outputfilename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($outputfilename));
ob_clean();
flush();
readfile($outputfilename);
//After download remove the file from server
exec('rm ' . $outputfilename);
}
else{//Restore the database
$target_path = getcwd();
$databasefilename = $_FILES["databasefile"]["name"];
//Upload the database file to current working directory
move_uploaded_file($_FILES["databasefile"]["tmp_name"], $target_path . '/' . $databasefilename);
//Restore the database
exec('mysql -u '. $username .' -p'. $password .' '. $databasename .' < '. $databasefilename);
//Remove the file from server
exec('rm ' . $databasefilename);
}
}
?>
MySQL database backup
Now run the file from your browser and you will get the following output.
Create a file called MySQLbackup.php in you server or you can give it to any other name you wish.
Now add the following html code to that file.
Next I have used the following javascript code to validate the input fields.
Next add the following php code.
And finally the file will look like following.
Now run the file from your browser and you will get the following output.
No comments:
Post a Comment