grep -n "Table Structure" mydump.sql
# identify the first and last line numbers (n1 and n2) of desired table
sed -n n1,n2p mydump.sql > mytable.sql # (e.g. sed -n 48,112p)
一个使用 awk
awk '/Table Structure for table .table1./, /Table structure for table .cultivars./{print}' mydump.sql > mytable.sql
#!/bin/bash
# Where to restore
db_host='localhost'
db_name='adhoctuts'
db_user='root'
db_pass='Adhoctuts2018#'
dump_file='/root/scripts/dump_ignore.sql'
# Associative table list array as source_table=>destination_table pairs
declare -A tbl_list=( ["tbl1"]="restored_tbl1" ["tbl2"]="restored_tbl2")
for tbl in "${!tbl_list[@]}"
do
echo "Restore $tbl to ${tbl_list[$tbl]}"
# extract the content between drop table and Table structure for, also replace the table name
sed -n -e '/DROP TABLE IF EXISTS `'$tbl'`/,/\/*!40000 ALTER TABLE `'$tbl'` ENABLE KEYS \*\/;/p' $dump_file > tbl.sql
sed -i 's/`'$tbl'`/`'${tbl_list[$tbl]}'`/g' tbl.sql
mysql -h $db_host -u $db_user -p"$db_pass" $db_name < tbl.sql
rm -f tbl.sql
done
windows脚本(蝙蝠):
%= Define the database and root authorization details =%
@ECHO OFF
SETLOCAL EnableDelayedExpansion
set db_host=192.168.70.138
set db_name=adhoctuts
set db_user=adhoctuts
set db_pass=Adhoctuts2018#
set dump_file=dump_ignore.sql
set tbl_cnt=2
set source_table[1]=tbl1
set destination_table[1]=restored_tbl1
set source_table[2]=tbl2
set destination_table[2]=restored_tbl2
set i=1
:loop
set src=!source_table[%i%]!
set dest=!destination_table[%i%]!
for /f "tokens=1 delims=[]" %%a in ('find /n "DROP TABLE IF EXISTS `%src%`"^<"%dump_file%"') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "ALTER TABLE `%src%` ENABLE KEYS"^<"%dump_file%"') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%dump_file%"') do (
set "line=%%b "
IF %%a geq %start% IF %%a leq %end% ECHO( !line:%src%=%dest%!
)
)>"tbl.sql"
mysql -h %db_host% -u %db_user% -p"%db_pass%" %db_name% < "tbl.sql"
del /f "tbl.sql"
if %i% equ %tbl_cnt% goto :eof
set /a i=%i%+1
goto loop
我找到了两种解决方案,一种使用
一个使用 awk
这种方法无法解决必须导入大型 mysqldumpfile 的问题,但在 Mysql 中,您可以执行以下操作:
恢复整个转储
删除当前表的内容
从备份表插入
我已经创建了 linux 和 windows 脚本来从转储文件中恢复特定的表:
linux(bash脚本):
windows脚本(蝙蝠):
在这里,您可以定义需要恢复的表以及要恢复的名称。这是更通用的解决方案。
我还为 MySQL 选择性/异常任务创建了单独的教程。如果需要,您可以检查:
https://youtu.be/8fWQbtIISdc
https://adhoctuts.com/mysql-selective-exceptional-permissions-and-backup-restore/