Restoring a full MySQL dump just to recover one table is slow and risky on a live database. This guide shows how to extract a single table’s data from a large .sql dump file and restore only that table — without touching the rest of the database.
⚠ Before You Start
Take a fresh backup of the target table before overwriting it:
mysqldump -u root -p mydb tablename > /tmp/tablename_current_backup.sql
Method 1: Extract Table Using sed (Works for Most Dumps)
# Extract CREATE TABLE + INSERT statements for a specific table
# Replace TABLENAME and FULL-DUMP.sql with your values
sed -n '/^-- Table structure for table `TABLENAME`/,/^-- Table structure for table/p' FULL-DUMP.sql
| head -n -1 > /tmp/TABLENAME_extract.sql
# If your dump doesn't have those comments, use this pattern instead:
sed -n '/^CREATE TABLE `TABLENAME`/,/^CREATE TABLE/p' FULL-DUMP.sql
| head -n -1 > /tmp/TABLENAME_extract.sql
Method 2: Use grep with Context (Simpler for Small Dumps)
# Works when INSERT statements are on separate lines from CREATE TABLE
grep -A 10000 "CREATE TABLE `TABLENAME`" FULL-DUMP.sql
| grep -B 10000 -m1 "CREATE TABLE `NEXTTABLE`"
| head -n -1 > /tmp/TABLENAME_extract.sql
Method 3: mysqldump –tables (Best if Database is Accessible)
If you have access to the source database (e.g., from a backup server), dump just the table you need:
mysqldump -u root -p source_db tablename > /tmp/tablename_only.sql
Restore the Extracted Table
# Drop the current (corrupt) table and restore from extraction
mysql -u root -p target_db << 'EOF'
DROP TABLE IF EXISTS `TABLENAME`;
EOF
mysql -u root -p target_db < /tmp/TABLENAME_extract.sql
# Verify the restore
mysql -u root -p -e "SELECT COUNT(*) FROM target_db.TABLENAME;"
Using MyDumper/MyLoader (Best for Large Databases)
For databases over 10 GB, myloader can restore individual tables from mydumper-format backups much faster than mysqldump:
myloader -u root -p password -d /backup/dir/ -B mydb -T TABLENAME --overwrite-tables
