在Windows上运行.sh文件以从Mysql .sql文件中恢复单个表

时间:2013-11-01 16:10:35

标签: mysql mysqldump sh

我有一个mysql数据库的备份,我急需一张表。

它的4gb和香港专业教育学院尝试用像VIM这样的程序打开它并且它没有顺利,猜测它太大了。即使这样尝试从如此多的文本中提取一个表也很困难。

所以我遇到了这个: http://kedar.nitty-witty.com/blog/mydumpsplitter-extract-tables-from-mysql-dump-shell-script

其中介绍了如何使用shell脚本执行此操作。我发现http://cygwin.com你可以在windows中运行shell脚本,我正在运行Windows 8.1。

我不太清楚步骤是什么:

所以我运行cwygin并进入shell脚本窗口 我把我的数据库文件和mysqldumpsplitter.sh放在我创建的C:\ cygwin64 \ usr \ mysql文件夹中。

然后我转到/ usr / mysql并运行它:

sh mysqldumpsplitter.sh mydatabase.sql tbl_activity

tbl_activity是我试图访问的表。和mydatabase.sql是sql备份

但是当我跑步时我得到了

mysqldumpsplitter.sh:第5行:找不到tput:命令 mysqldumpsplitter.sh:第6行:tput:找不到命令 mysqldumpsplitter.sh:第7行:找不到tput:命令 mysqldumpsplitter.sh:第8行:找不到tput:命令 mysqldumpsplitter.sh:第9行:找不到tput:命令 mysqldumpsplitter.sh:第10行:tput:找不到命令 mysqldumpsplitter.sh:第11行:tput:找不到命令 mysqldumpsplitter.sh:第12行:tput:找不到命令 mysqldumpsplitter.sh:第13行:找不到tput:命令 mysqldumpsplitter.sh:第14行:tput:找不到命令 0表格从mydatabase.sql中提取。

第5行= 14低于

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

虽然我可能可以访问ubuntu机器并运行它(我假设这将更好地工作)我将等待数小时4gb .sql转储上传,我希望快速做到这一点。它只是在Windows上运行这个hack而我应该切换到ubuntu来运行它吗?

完整的.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}"

2 个答案:

答案 0 :(得分:0)

尝试UltraEdit程序:它打开文件而不缓冲整个内容。你相信,你可以使用评估版30天。

奇怪的是,这是我所知道的唯一一个不缓冲整个文件的程序(Windows / Linux)。它曾多次帮助过我。

答案 1 :(得分:0)

我不会那么久。我会用我手边的东西。我想,你知道表结构,只需要数据。所以我会在cmd中使用以下内容:

C:\tmp>findstr "^INSERT INTO your_table" < mydatabase.sql > filtered.sql

可以肯定的是,INSERT语句在文件中的外观如下所示:

C:\tmp>findstr "INSERT INTO" < mydatabase.sql | more

然后退出Ctrl+C