From d556724b0b73448dc121fc5f226aae966170cff4 Mon Sep 17 00:00:00 2001 From: kedarvj Date: Fri, 13 Jun 2014 00:27:29 +0530 Subject: [PATCH] mysqldumpsplitter A shellscript to split the mysqldump. [http://kedar.nitty-witty.com/blog/mydumpsplitter-extract-tables-from-mysql-dump-shell-script] --- .gitattributes | 22 +++++++++++++++++ .gitignore | 36 +++++++++++++++++++++++++++ mysqldumpsplitter.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 mysqldumpsplitter.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6dae747 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# ========================= +# Operating System Files +# ========================= + +# OSX +# ========================= + +.DS_Store +.AppleDouble +.LSOverride + +# Icon must ends with two \r. +Icon + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes diff --git a/mysqldumpsplitter.sh b/mysqldumpsplitter.sh new file mode 100644 index 0000000..ab38012 --- /dev/null +++ b/mysqldumpsplitter.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# http://kedar.nitty-witty.com +# Source: http://kedar.nitty-witty.com/blog/mydumpsplitter-extract-tables-from-mysql-dump-shell-script +#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}" + \ No newline at end of file