top button
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Backup & Restoring in MySql

+2 votes

Back Up From Command Line

1.mysqldump -u [uname] -p[pass] [dbname] > [backupfile.sql]

For example, to backup a database named 'IMS' with the username 'root' and with no password to a file IMS_Bkp.sql, you should accomplish this command:

Mysqldump -uroot -p ims>IMS_Bkp.sql

The back up file will save in location 'MySQL Server 5.5\bin'

With mysqldump command you can specify certain tables of your database you want to backup.

For example, to back up only assetmaster and assetdetails tables from the 'IMS' database accomplish the command below. Each table name has to be separated by space.

Mysqldump -uroot -p ims assetmaster assetdetails >IMS_Bkp.sql

Sometimes it is necessary to back up more that one database at once. In this case you can use the --database option followed by the list of databases you would like to backup. Each database name has to be separated by space.

Mysqldump -uroot -p --databases ims ims1>IMS_Bkp.sql

If you want to back up all the databases in the server at one time you should use the --all-databases option. It tells MySQL to dump all the databases it has in storage.

mysqldump -u root -p --all-databases > alldb_backup.sql

The mysqldump command has also some other useful options:

--add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.

--no-data: Dumps only the database structure, not the contents.
--ignore-table : Ignore selective Tables

--add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.

Ex:- mysqldump -h -u -p --ignore-table=schema.table1 --ignore-table=schema.table2 > db-data.sql

for multiple databases: mysqldump -u user -p --ignore-table=db1.tbl1 --ignore-table=db2.tbl1 --databases db1 db2 ..

The mysqldump command has advantages and disadvantages. The advantages of using mysqldump are that it is simple to use and it takes care of table locking issues for you. The disadvantage is that the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.

Back up your MySQL Database with Compress

Linux & Unix

If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip, then you will get the output as gzip file.

mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

Ex:- mysqldump -uroot -p ims | gzip -c >ims_bkp.sql.gz

If you want to extract the .gz file, use the command below:

gunzip [backupfile.sql.gz]


Restoring your MySQL Database

Above we backup the Tutorials database into IMS_Bkp.sql file. To re-create the Tutorials database you should follow two steps:

Create an appropriately named database on the target machine

Load the file using the mysql command:

mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

Have a look how you can restore your IMS_Bkp.sql file to the Tutorials database.

mysql -u root -p IMS< IMS_Bkp.sql

To restore compressed backup files you can do the following:

gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

If you need to restore a database that already exists, you'll need to use mysqlimport command. The syntax for mysqlimport is as follows:

mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

posted Feb 27, 2015 by Dharmananda Mohanta

  Promote This Article
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button