Mysql Database Backup Using PHP / Batch File in Windows System.

In this section, I will show how to backup Mysql database using php script and batch file(.bat).
I had created this script for our client as they require automatic mysql database backup for their project.

So You can schedule below script in task schedular of windows system to backup database on daily /monthly basis.

Mysql Database Backup Using PHP Script :


Example: backup.php

<?php
ini_set("display_errors",1);

$dbhost = 'localhost'; // Datatbase host name.
$dbuser = 'root'; // Database user name
$dbpass = '123'; // database password
$dbname="testdb"; // database name;

system("cd c:\\wamp\\bin\\mysql\\mysql5.5.24\\bin &  mysqldump --opt -h $dbhost -u$dbuser -p$dbpass $dbname>c:\\wamp\\backup\\backup.sql");
?>



"cd c:\\wamp\\bin\\mysql\\mysql5.5.24\\bin" is the path of Mysql folder.

"mysqldump --opt -h $dbhost -u$dbuser -p$dbpass $dbname>c:\\wamp\\backup\\backup.sql" is the Mysql command used to take backup of mysql database.


Mysql Database Backup Using Batch File :


It is very easy to backup mysql database using batch file as you have to just write commands in batch file and schedule batch file as per your requirment in Task Shedular.

Example: backup.bat


cd c:\wamp\bin\mysql\mysql5.5.24\bin

@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set dt=%%c-%%a-%%b)
mysqldump --opt -h localhost -uroot -p123 testdb>c:\wamp\backup\testdb_%dt%.sql




I hope above examples will help you to take backup of mysql database.