Tuesday, September 6, 2016

BACK UP AND RESTORE DATABASE USING COMMAND LINE IN PHP SCRIPT

Filled under:

1. BACK UP DATABASE
This line bellow is used to back up database existing in MySQL database engine. This standart coding is using XAMPP Server and running in Windows Operating System. Before executing this line in browser, you have to try it in the Windows Command Prompt with your own system privileges.
a. Go to cmd.exe as an Administrator and run this command line:
C:\xampp\mysql\bin>mysqldump –h[hostname] –u[username] –p[password] [databasename] > [database back up file].[extension]
This is the example of the real code:
C:\ xampp\mysql\bin\mysqldump -u root spisy_kpe > spisy_kpe.sql
Never use –p if you don’t use password to encrypt the database. The above example is not using password, so there is no –p command.
b. Executing the command line with PHP Script:
Try to make program with php extension saved in the folder  in the htdocs and type this code bellow:
<?php
$command = "c:/xampp/mysql/bin/mysqldump -u root spisy_kpe > spisy_kpe3.sql";
exec($command);
echo "finished to backup database";
?>

Run the script using browser and find the backup file in the folder used to save this script program file.
2. CREATE DATABASE
a. Go to cmd.exe as an Administrator and run this command line:
C:\xampp\mysql\bin>mysql –h[hostname] –u[username] –p[password] –e “create database spisy;”

This is the example of the real code:
C:\ xampp\mysql\bin\mysql –h localhost -u root –e “create database spisy;”
Never use –p if you don’t use password to encrypt the database. The above example is not using password, so there is no –p command.
b. Executing the command line with PHP Script:
Try to make program with php extension saved in the folder  in the htdocs and type this code bellow:
<?php
$command = "c:/xampp/mysql/bin/mysql –h localhost -u root -e "create database spisy;";
exec($command);
echo "finished to create database";
?>

Try to open phpmyadmin through the browser and look at the database collection. The database you created will exist there.
3. RESTORE DATABASE
a. Go to cmd.exe as an Administrator and run this command line:
C:\xampp\mysql\bin>mysql –h [hostname] -u root –p [password] [restored database name] < [database file name].[extension]”

This is the example of the real code:
C:\xampp\mysql\bin>mysql –h localhost -u root spisy < spisy-kpe3.sql”
Never use –p if you don’t use password to encrypt the database. The above example is not using password, so there is no –p command.
b. Executing the command line with PHP Script:
Try to make program with php extension saved in the folder  in the htdocs and type this code bellow:
<?php
$command = "c:/xampp/mysql/bin/mysql –h localhost -u root spisy < spisy-kpe3.sql";
exec($command);
echo "finished to restore database";
?>

4. MERGING THE PROGRAM
Now, we can use all of program to back up, create and restore database in same time. The program look like bellow:
<?php
$command = "c:/xampp/mysql/bin/mysqldump -u root spisy-kpe > spisy-kpe3.sql";

exec($command);

echo "finished to backup database";

exec('c:/xampp/mysql/bin/mysql -u root -e "create database spisy;"');

exec('c:/xampp/mysql/bin/mysql -u root spisy < spisy-kpe3.sql');

echo "finished to restore database";
exec(exit);
?>

1 comments:

  1. Pragmatic Play adds Slots to its portfolio in India - JTM Hub
    Pragmatic 군포 출장마사지 Play has added Slots to its portfolio 시흥 출장안마 in India, 전주 출장안마 making it one of the 화성 출장샵 top online 안성 출장안마 casinos in the world.

    ReplyDelete