mirror of
https://github.com/kedarvj/mysqldumpsplitter.git
synced 2025-08-22 16:13:46 +02:00
Added database extraction and compression.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# http://kedar.nitty-witty.com
|
# http://kedar.nitty-witty.com
|
||||||
# Source: http://kedar.nitty-witty.com/blog/mydumpsplitter-extract-tables-from-mysql-dump-shell-script
|
# Ver 2 (added dump database & compression)
|
||||||
#SPLIT DUMP FILE INTO INDIVIDUAL TABLE DUMPS
|
#SPLIT DUMP FILE INTO INDIVIDUAL TABLE DUMPS
|
||||||
# Text color variables
|
# Text color variables
|
||||||
txtund=$(tput sgr 0 1) # Underline
|
txtund=$(tput sgr 0 1) # Underline
|
||||||
@@ -17,18 +17,21 @@ txtrst=$(tput sgr0) # Text reset
|
|||||||
TARGET_DIR="."
|
TARGET_DIR="."
|
||||||
DUMP_FILE=$1
|
DUMP_FILE=$1
|
||||||
TABLE_COUNT=0
|
TABLE_COUNT=0
|
||||||
|
COMPRESSION=gzip
|
||||||
|
|
||||||
if [ $# = 0 ]; then
|
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}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 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."
|
echo "${txtbld}${txtred} sh MyDumpSplitter.sh DUMP-FILE-NAME -S TABLE-NAME-REGEXP ${txtrst} -- Extract tables from dump for specified regular expression."
|
||||||
|
echo "${txtbld}${txtred} sh MyDumpSplitter.sh DUMP-FILE-NAME -d DATABASE-NAME ${txtrst} -- Extract complete database from dump."
|
||||||
|
|
||||||
exit;
|
exit;
|
||||||
elif [ $# = 1 ]; then
|
elif [ $# = 1 ]; then
|
||||||
#Loop for each tablename found in provided dumpfile
|
#Loop for each tablename found in provided dumpfile
|
||||||
for tablename in $(grep "Table structure for table " $1 | awk -F"\`" {'print $2'})
|
for tablename in $(grep "Table structure for table " $1 | awk -F"\`" {'print $2'})
|
||||||
do
|
do
|
||||||
#Extract table specific dump to tablename.sql
|
#Extract table specific dump to tablename.sql
|
||||||
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 > $TARGET_DIR/$tablename.sql
|
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 | $COMPRESSION > $TARGET_DIR/$tablename.sql.gz
|
||||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||||
done;
|
done;
|
||||||
elif [ $# = 2 ]; then
|
elif [ $# = 2 ]; then
|
||||||
@@ -36,7 +39,7 @@ elif [ $# = 2 ]; then
|
|||||||
do
|
do
|
||||||
echo "Extracting $tablename..."
|
echo "Extracting $tablename..."
|
||||||
#Extract table specific dump to tablename.sql
|
#Extract table specific dump to tablename.sql
|
||||||
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 > $TARGET_DIR/$tablename.sql
|
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 | $COMPRESSION > $TARGET_DIR/$tablename.sql.gz
|
||||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||||
done;
|
done;
|
||||||
elif [ $# = 3 ]; then
|
elif [ $# = 3 ]; then
|
||||||
@@ -46,9 +49,14 @@ elif [ $# = 3 ]; then
|
|||||||
do
|
do
|
||||||
echo "Extracting $tablename..."
|
echo "Extracting $tablename..."
|
||||||
#Extract table specific dump to tablename.sql
|
#Extract table specific dump to tablename.sql
|
||||||
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 > $TARGET_DIR/$tablename.sql
|
sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" $1 | $COMPRESSION > $TARGET_DIR/$tablename.sql.gz
|
||||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||||
done;
|
done;
|
||||||
|
elif [ $2 = "-d" ]; then
|
||||||
|
echo "Extracting Database: $3...";
|
||||||
|
sed -n "/^-- Current Database: \`$3\`/,/^-- Current Database: /p" $1 | $COMPRESSION > $TARGET_DIR/$3.sql.gz
|
||||||
|
echo "${txtbld} Database $3 extracted from $DUMP_FILE at $TARGET_DIR${txtrst}"
|
||||||
|
exit;
|
||||||
else
|
else
|
||||||
echo "${txtbld}${txtred} Please provide proper parameters. ${txtrst}";
|
echo "${txtbld}${txtred} Please provide proper parameters. ${txtrst}";
|
||||||
fi
|
fi
|
||||||
@@ -56,4 +64,3 @@ fi
|
|||||||
|
|
||||||
#Summary
|
#Summary
|
||||||
echo "${txtbld}$TABLE_COUNT Table extracted from $DUMP_FILE at $TARGET_DIR${txtrst}"
|
echo "${txtbld}$TABLE_COUNT Table extracted from $DUMP_FILE at $TARGET_DIR${txtrst}"
|
||||||
|
|
Reference in New Issue
Block a user