Monday, 5 June 2017

Restore Single Table in Mysql

Steps to Restore Single table from MYSQL Dump:


Suppose we have multiple table as below:

mysql> show tables;
+-----------------------+
 | Tables_in_test  |
+-----------------------+
 | mytable1          |
 | mytable2          |
 | mytable3          |
+-----------------------+
3 rows in set (0.00 sec)

To dump from table we can use below command
$ mysqldump -u username -p -h XXX.XXX.XXX.XXX test > mytestdump.sql

Search for the string pattern table structure as below
$ grep -n 'Table structure' mytestdump.sql

120:-- Table structure for table `mytable1`
304:-- Table structure for table `mytable2`
810:-- Table structure for table `mytable3`

Once we get the starting line of each table we can use sed command to create a file for individual table as below
$ sed -n '304,810 p' mytestdump.sql > mytable2.sql

Now with the below command we can restore individual table into database
$ mysql -u username -p -h XXX.XXX.XXX.XXX DATA-BASE-NAME < mytable2.sql