2012-03-27 58 views
6

如何删除mysqldump中有大量表格的大表格的输出?如何从mysqldump中删除表格

我有一个6 GB大的数据库转储,但其中90%是只有一个日志表“cache_entries”,我不需要在备份中。

如何轻松删除转储中的那个位,它描述了大型日志记录表?

我发现这一点: http://gtowey.blogspot.de/2009/11/restore-single-table-from-mysqldump.html

例子:

grep -n 'Table structure' dump.sql 

,然后例如:

sed -n '40,61 p' dump.sql > t2.sql 

但我怎么可以改变我的需求?

+0

为什么在初始转储中包含该表?似乎mysqldump的API应该允许你只转储你关心的表 http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html – ControlAltDel 2012-03-27 19:12:36

+0

我没有做出疯狂的备份;) – rubo77 2013-05-30 15:52:09

回答

9

我发现这个bash脚本,使用csplit(将文件分割成由上下文行确定的部分)将一个数据库的转储分割为每个表的单独字段:

#!/bin/bash 

#### 
# Split MySQL dump SQL file into one file per table 
# based on http://blog.tty.nl/2011/12/28/splitting-a-database-dump 
#### 

if [ $# -ne 1 ] ; then 
    echo "USAGE $0 DUMP_FILE" 
fi 

csplit -s -ftable $1 "/-- Table structure for table/" {*} 
mv table00 head 

for FILE in `ls -1 table*`; do 
     NAME=`head -n1 $FILE | cut -d$'\x60' -f2` 
     cat head $FILE > "$NAME.sql" 
done 

rm head table* 

来源:gist.github.com/1608062

和位增强: How do I split the output from mysqldump into smaller files?

一次,你有单独的文件中的每个表,您可以删除不需要的表,如果需要用

cat table* >glued_sqldump.sql

粘合在一起他们
0

你需要找到你的表的create table语句,并找到下一个create table语句。说他们是n1和n2。

然后你可以像上面那样用sed删除它们。 sed'n1,n2d'dump.sql> new.sql

你可以用grep创建表并记下你的作业行号。

这里是一个演示。

[email protected]:~$ grep -n [34] a.txt 
3:3 
4:4 
[email protected]:~$ cat a.txt 
1 
2 
3 
4 
5 
6 
[email protected]:~$ grep [34] a.txt 
3 
4 
[email protected]:~$ sed '3,4d' a.txt > b.txt 
[email protected]:~$ cat b.txt 
1 
2 
5 
6 
[email protected]:~$ 
10

您可以使用'n,n d'来删除某些行。 我想你的情况你确实想要有问题的表,但不想要数据?

更改grep命令包括 “为表转储数据”:

 
grep -n 'Table structure\|Dumping data for table' dump.sql 
19:-- Table structure for table `t1` 
37:-- Dumping data for table `t1` 
47:-- Table structure for table `t2` 
66:-- Dumping data for table `t2` 
76:-- Table structure for table `t3` 
96:-- Dumping data for table `t3` 

现在,如果你不想T2的数据,你可以使用:

sed '66,75 d' dump.sql > cleandump.sql