DrXCheng
DrXCheng

Reputation: 4132

Can I restore one table from MySQL database backup?

I accidentally empty a table. I have backup, but the backup is the whole database, about 10GB. How can I just restore one table from the backup? Is it possible?

Upvotes: 2

Views: 1748

Answers (2)

Steve Robbins
Steve Robbins

Reputation: 13812

From http://blog.tsheets.com/2008/tips-tricks/mysql-restoring-a-single-table-from-a-huge-mysqldump-file.html

$ awk '/Table structure for table .test1./,/Table structure for table .test2./{print}' mydumpfile.sql > /tmp/extracted_table.sql

Upvotes: 1

spencer7593
spencer7593

Reputation: 108380

You'd need to extract just the one table from the backup SQL.

You can do this using sed, e.g.

sed -n -e '/CREATE TABLE `mytable`/,/CREATE TABLE/p' backup.sql > mytable.sql

(This works as long as its not the last table in the dump (I think). The output file will include an extra line, a partial CREATE TABLE statement.)

Upvotes: 0

Related Questions