我在 mysql.sql 文件中有一个产品表。我希望提取该表并将其放入自己的文件中。你会怎样做呢?
我找到了这个不错的解决方案,你必须在你的服务器上下载这个脚本(脚本复制如下)并输入
$> ./MyDumpSplitter.sh yourfile.sql your_table_name
这会将您的表提取到 your_table_name.sql 中
可选
现在您可以使用此类命令重命名它
$> sed -i 's/`your_table_name`/`your_table_name2`/g' your_table_name.sql
然后重新注入
mysql> source your_table_name.sql;
MyDumpSplitter.sh
#!/bin/sh
# http://kedar.nitty-witty.com
#SPLIT DUMP FILE INTO INDIVIDUAL TABLE DUMPS
# Text color variables
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
txtred=$(tput setaf 1) # Red
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtpur=$(tput setaf 5) # Purple
txtcyn=$(tput setaf 6) # Cyan
txtwht=$(tput setaf 7) # White
txtrst=$(tput sgr0) # Text reset
TARGET_DIR="."
DUMP_FILE=$1
TABLE_COUNT=0
if [ $# = 0 ]; then
echo "${txtbld}${txtred}Usage: sh MyDumpSplitter.sh DUMP-FILE-NAME${txtrst} -- Extract all tables as a separate file from dump."
echo "${txtbld}${txtred} sh MyDumpSplitter.sh DUMP-FILE-NAME TABLE-NAME ${txtrst} -- Extract single table from dump."
echo "${txtbld}${txtred} sh MyDumpSplitter.sh DUMP-FILE-NAME -S TABLE-NAME-REGEXP ${txtrst} -- Extract tables from dump for specified regular expression."
exit;
elif [ $# = 1 ]; then
#Loop for each tablename found in provided dumpfile
for tablename in $(grep "Table structure for table " $1 | awk -F"\`" {'print $2'})
do
#Extract table specific dump to tablename.sql
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 > $TARGET_DIR/$tablename.sql
TABLE_COUNT=$((TABLE_COUNT+1))
done;
elif [ $# = 2 ]; then
for tablename in $(grep -E "Table structure for table \`$2\`" $1| awk -F"\`" {'print $2'})
do
echo "Extracting $tablename..."
#Extract table specific dump to tablename.sql
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 > $TARGET_DIR/$tablename.sql
TABLE_COUNT=$((TABLE_COUNT+1))
done;
elif [ $# = 3 ]; then
if [ $2 = "-S" ]; then
for tablename in $(grep -E "Table structure for table \`$3" $1| awk -F"\`" {'print $2'})
do
echo "Extracting $tablename..."
#Extract table specific dump to tablename.sql
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 > $TARGET_DIR/$tablename.sql
TABLE_COUNT=$((TABLE_COUNT+1))
done;
else
echo "${txtbld}${txtred} Please provide proper parameters. ${txtrst}";
fi
fi
#Summary
echo "${txtbld}$TABLE_COUNT Table extracted from $DUMP_FILE at $TARGET_DIR${txtrst}"
我不久前遇到了这个问题并编写了一个 Perl 脚本。它运行良好,但它是旧版本的 MySQL。称呼它为:
extract.pl -table=TABLENAME mysqldumpfile.sql > recovered_table.sql
#!/usr/bin/perl -s -wnl #从 mysql 转储中提取单个表 开始 { $表或警告 “用法:$0 -table=TABLE_TO_EXTRACT mysqldumpfile.sql” 并退出 1; } /^DROP TABLE IF EXISTS `$table`/ .. /^UNLOCK TABLES;$/ 并打印;
您只需运行流编辑器来过滤和转换文本命令,如下所示:
$ sed -n -e '/CREATE TABLE.*products/,/CREATE TABLE/p' mysql.sql > products.sql
据我所知,没有任何工具可以通过这种方式解析原始 mySQL 转储文件。
复制+粘贴它(可能很麻烦)或将其导入临时数据库,删除其他所有内容,然后将表转储回文件中。
如果使用扩展插入语法完成转储,则实际表数据将作为转储文件中的单行完成:
INSERT INTO tablename (field, ...) VALUES (data, ...), (data, ...), (etc..)
你可以简单地 grep 出来。提取实际的表定义会更困难,尽管它应该紧接在数据行上方。目前无法访问正确的 shell,但我突然想到,像这样的东西可能会成功(以伪代码形式):
# retrieve line # that data insertion for the wanted table occurs on
DATALINE=`grep -l 'INSERT INTO tablename' dumpfile.sql`
# limiting ourselves to the part of the file up to the data line, find the line number
# of the last CREATE TABLE, which SHOULD be the one creating the table for the data
CREATELINE=`head -$DATALINE|grep -l 'CREATE TABLE'|tail -1|awk '{"print $1"}'`
您可以使用明智的头/尾和我们刚刚检索到的行号来提取创建DDL:
CREATE=`head -$CREATELINE dumpfile.sql|tail -$($CREATELINE - $DATALINE - 1)`
请记住,我对此非常疯狂,所以它几乎肯定不起作用,但应该足以让你开始。
如果你想提取数据库,你可以使用:
sed -n '/^-- Current Database: `dbname`/,/^-- Current Database: `/p' dumpfile > dbname.sql 2>error
例如:
sed -n '/^-- Current Database: `blogs`/,/^-- Current Database: `/p' dump.sql > blogs.sql 2>error