Quickly Restore a table named mysqldump from the mysqldump file
BitsCN.com
Quickly Restore a table from the mysqldump file
In many cases, we need to restore a table from the mysqldump backup file. the common practice is to restore the SQL file to a test database and then use mysqldump to export a table, restore to the production environment. in this way, if the data volume is small, this method is feasible, but you still need to have a test machine or create a database temporarily, which is not very convenient, the method described below avoids the above problems:
1. use awk
You can first use 'Show tables; 'in the original database to view the list of database tables. Note that the list has been sorted alphabetically, for example,
Table1
Table 2
Table 3
Then use awk to filter SQL statements. if you want to restore table 2, you can use the following statement.
]# awk ‘/^-- Table structure for table .table2./,/^-- Table structure for table .table3./{print}’ mydumpfile.sql > /tmp/recovered_table.sql
2. use sed
Same method as above
Suppose you want to restore table 2, you can use the following statement
cat mydumpfile.sql | sed -n -e '/Table structure for table .test1./,/Table structure for table .test2./p' > /tmp/extracted_table.sql
Then, the filtered. SQL file is imported into the database and restored.
BitsCN.com