代码改变世界

从mysqldump备份中恢复一个表

2022-02-27 20:05  abce  阅读(102)  评论(0编辑  收藏  举报

假设要恢复的表是 abce.abce

 

#提取abce库的所有数据

sed -n '/^-- Current Database: `abce`/,/^-- Current Database:/p' backup.sql > backup_abce.sql

#从库备份文件中提取建表语句

sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `abce`/!d;q' backup_abce.sql > abce_table_create.sql

#从库备份文件中提取插入数据语句

grep -i 'INSERT INTO `abce`' backup_abce.sql > abce_table_insert.sql

#恢复表结构到 abce 库

mysql -u<user> -p abce < abce_table_create.sql

#恢复表数据到 abce.abce 表

mysql -u<user> -p abce <  abce_table_insert.sql