Home > Mysql > BackUp and Restore Mysql Database

BackUp and Restore Mysql Database

In this article we will learn on how we can perform following operations using MySQL.
* Backup Database using MySQL
* Backup specific tables using MySQL
* Backup Multiple Database using MySQL
* Backup Database Structure using MySQL
* Restoring Backup using MySQL

MySQL comes with an powerful console command called “mysqldump”. With this command we can achieve all the operation listed above. To check whether you have mysqldump command just type mysqldump in your command prompt you will get following output.

	[root@Hitesh ~]# mysqldump
	Usage: mysqldump [OPTIONS] database [tables]
	OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
	OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
	For more options, use mysqldump --help

If you dont see this prompt check for where you have installed the mysql on your system.

Backup Database using MySQL:
The syntax for taking backup of mysql database is:

    mysqldump -u [username] -p [password] [databasename] > [backupfile.sql]

o [username] – this is your database username
o [password] – this is the password for your database
o [databasename] – the name of your database
o [backupfile.sql] – the file to which the backup should be written.

Example:

 	mysqldump -u root -p password employee > /tmp/dbback.sql

Here we have taken the full backup for all the tables inside employee database and all the sql statements are being written inside /tmp/dbback.sql. Full backup means it will have both the insert statements and create statements required for creating and populating the tables.

Regular Expression in MySQL

Backup specific tables using MySQL:
The syntax for taking backup of specific mysql table is:

    mysqldump -u [username] -p [password] [databasename] [tablename1, tablename2 ...] > [backupfile.sql]

o [username] – this is your database username
o [password] – this is the password for your database
o [databasename] – the name of your database
o [tablename] – the name of your table existing inside the database selected
o [backupfile.sql] – the file to which the backup should be written.

Example:

 	mysqldump -uroot -p password employee salary> /tmp/dbback.sql

Here we have taken the backup for salary table inside employee database.

Backup Multiple Database using MySQL:
To take backup of multiple databases you will have to use “–databases” options provided with mysqldump, every name specified after “–databases” will be considered as the database name you wish to take the backup. Note that if you taking the backup for multiple database than you cannot take the backup for individual tables.
The syntax for taking backup of multiple mysql table is:

    mysqldump -u [username] -p [password] -- databases [databasename1] [databasesname2] > [backupfile.sql]

o [username] – this is your database username
o [password] – this is the password for your database
o [databasename1] – the name of your database
o [databasename2] – the name of your database
o [backupfile.sql] – the file to which the backup should be written.

Example:

 	mysqldump -uroot -p password --databases employee products> /tmp/dbback.sql

Backup Database Structure using MySQL:
In all the above cases we have discussed will add all the Data Manipulation Statements, if we only want Data Definition Statements then we will have to use –no-data options with mysqldump.
The syntax for taking backup of database structure is:

    mysqldump --no-data -u [username] -p [password] [databasename1] > [backupfile.sql]

o [username] – this is your database username
o [password] – this is the password for your database
o [databasename1] – the name of your database
o [backupfile.sql] – the file to which the backup should be written.

Example:

 	mysqldump --no-data -uroot -p password employee > /tmp/dbback.sql

Restoring Backup using MySQL:
For restoring the data we will be using mysql command. The syntax for the same would be

mysql -uroot -p password employee < /tmp/dbback.sql

Here we are restoring the dbdack.sql to employee database


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: Mysql Tags:
  1. Sachin Warke
    October 8th, 2009 at 01:01 | #1

    Hi Hitesh,

    It’s nice. Usually we need these commands in every project. Would you Please mention, What about to import database in MySQL on UNIX ?

    Thank you !
    Kind Regards
    - Sachin.

  2. October 13th, 2009 at 23:14 | #2

    I really like your blog and i respect your work. I’ll be a frequent visitor.

  3. January 19th, 2010 at 10:06 | #3

    really nice share..thanks a lot mate !

  4. February 19th, 2010 at 04:22 | #4

    I personally think, that it is very user friendly for starters/newbies to use the graphical method rather than writing Linux/UNIX queries. Anyways, this is a really good tutorial for people who are used to PhpMyAdmin.

  5. meth0s
    April 25th, 2010 at 06:34 | #5

    Really nice tutorial. I am going to bookmark your blog address for further reading.

  6. Thamotharan R
    July 5th, 2010 at 11:20 | #6

    Hi Hitesh,

    Its really very useful for us to prepare for our interview. Can u plz give more topics in php which are commonly asked in php interviews. And also can u plz list out the procedure to prepare for the php interview!!

  1. November 2nd, 2009 at 15:00 | #1
  2. March 4th, 2010 at 09:11 | #2