Backup & Restore MySQL Tables & Databases

Submitted by:Jhon Jhon

Date added:14 September, 2013

Category:MySQL

In order for this to work, you will need to copy mysqldump.exe and mysql.exe from the MySQL bin Program Files folder into the same directory as your executing PHP script. If you know the direct path to these for your server, you can use those instead.

Tags: backup , restore

Code Snippet:

    // Config
$db_user = "db_username";
$db_pass = "db_pass";

// Dump an entire database
exec("mysqldump --opt -u$db_user -p$db_pass my_database > backup.sql");

// Dump a table
exec("mysqldump --opt -u$db_user -p$db_pass my_database my_table > backup.sql");

// Dump just table rows without structure
exec("mysqldump --opt -u$db_user -p$db_pass --no-create-info my_database my_table > backup.sql");

// Dump just table rows with WHERE clause without structure
exec("mysqldump --opt -u$db_user -p$db_pass --no-create-info --where=id='1' my_database my_table > backup.sql");

// Restoring from .sql file
exec("mysql -u$db_user -p$db_pass my_database < backup.sql");
 
 

Comments