mirror of
https://github.com/kedarvj/mysqldumpsplitter.git
synced 2025-08-14 04:14:03 +02:00
Version 6.0
# ... Bug fixing in REGEXP extraction functionlity # ... Bug fixing in describe functionality # ... Preserving time_zone & charset env settings in extracted sqls.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Current Version: 5.0
|
||||
# Current Version: 6.0
|
||||
# Extracts database, table, all databases, all tables or tables matching on regular expression from the mysqldump.
|
||||
# Includes output compression options.
|
||||
# By: Kedar Vaijanapurkar
|
||||
@@ -25,6 +25,11 @@
|
||||
# Ver. 5.0: Apr, 2015
|
||||
# ... Describing the dump, listing all databases and tables
|
||||
# ... Extracting one or more tables from single database
|
||||
# Ver. 6.0: Oct, 2015
|
||||
# ... Bug fixing in REGEXP extraction functionlity
|
||||
# ... Bug fixing in describe functionality
|
||||
# ... Preserving time_zone & charset env settings in extracted sqls.
|
||||
# Credit: @PeterTheDBA helped understanding the possible issues with environment variable settings included in first 17 lines of mysqldump.
|
||||
##
|
||||
|
||||
# ToDo: Work with straming input
|
||||
@@ -51,7 +56,7 @@ TABLE_NAME='';
|
||||
DB_NAME='';
|
||||
COMPRESSION='gzip';
|
||||
DECOMPRESSION='cat';
|
||||
VERSION=5.0
|
||||
VERSION=6.0
|
||||
|
||||
## Usage Description
|
||||
usage()
|
||||
@@ -96,7 +101,7 @@ parse_result()
|
||||
|
||||
## Parse Extract Operation
|
||||
case $EXTRACT in
|
||||
ALLDBS|ALLTABLES )
|
||||
ALLDBS|ALLTABLES|DESCRIBE )
|
||||
if [ "$MATCH_STR" != '' ]; then
|
||||
echo "${txtylw}Ignoring option --match_string.${txtrst}"
|
||||
fi;
|
||||
@@ -202,30 +207,55 @@ echo "${txtylw}Processing: Extract $EXTRACT $MATCH_STR from $SOURCE with compres
|
||||
|
||||
}
|
||||
|
||||
# Include first 17 lines of full mysqldump - preserve time_zone/charset/environment variables.
|
||||
include_dump_info()
|
||||
{
|
||||
if [ $1 = "" ]; then
|
||||
echo "${txtred}Couldn't find out-put file while preserving time_zone/charset settings!${txtrst}"
|
||||
exit;
|
||||
fi;
|
||||
OUTPUT_FILE=$1
|
||||
|
||||
echo "Including environment settings from mysqldump."
|
||||
$DECOMPRESSION $SOURCE | head -17 | $COMPRESSION > $OUTPUT_DIR/$OUTPUT_FILE.$EXT
|
||||
echo "" | $COMPRESSION >> $OUTPUT_DIR/$MATCH_STR.$EXT
|
||||
echo "/* -- Splitted with mysqldumpsplitter (http://goo.gl/WIWj6d) -- */" | $COMPRESSION >> $OUTPUT_DIR/$OUTPUT_FILE.$EXT
|
||||
echo "" | $COMPRESSION >> $OUTPUT_DIR/$MATCH_STR.$EXT
|
||||
}
|
||||
|
||||
## Actual dump splitting
|
||||
dump_splitter()
|
||||
{
|
||||
case $EXTRACT in
|
||||
DB)
|
||||
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||
include_dump_info $MATCH_STR
|
||||
|
||||
echo "Extracting Database: $MATCH_STR";
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_STR\`/,/^-- Current Database: /p" | $COMPRESSION > $OUTPUT_DIR/$MATCH_STR.$EXT
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_STR\`/,/^-- Current Database: /p" | $COMPRESSION >> $OUTPUT_DIR/$MATCH_STR.$EXT
|
||||
echo "${txtbld} Database $MATCH_STR extracted from $SOURCE at $OUTPUT_DIR${txtrst}"
|
||||
;;
|
||||
|
||||
TABLE)
|
||||
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||
include_dump_info $MATCH_STR
|
||||
|
||||
#Loop for each tablename found in provided dumpfile
|
||||
echo "Extracting $MATCH_STR."
|
||||
#Extract table specific dump to tablename.sql
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Table structure for table \`$MATCH_STR\`/,/^-- Table structure for table/p" | $COMPRESSION > $OUTPUT_DIR/$MATCH_STR.$EXT
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Table structure for table \`$MATCH_STR\`/,/^-- Table structure for table/p" | $COMPRESSION >> $OUTPUT_DIR/$MATCH_STR.$EXT
|
||||
echo "${txtbld} Table $MATCH_STR extracted from $SOURCE at $OUTPUT_DIR${txtrst}"
|
||||
;;
|
||||
|
||||
ALLDBS)
|
||||
for dbname in $($DECOMPRESSION $SOURCE | grep -E "^-- Current Database: " | awk -F"\`" {'print $2'})
|
||||
do
|
||||
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||
include_dump_info dbname
|
||||
|
||||
echo "Extracting Database $dbname..."
|
||||
#Extract database specific dump to database.sql.gz
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$dbname\`/,/^-- Current Database: /p" | $COMPRESSION > $OUTPUT_DIR/$dbname.$EXT
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$dbname\`/,/^-- Current Database: /p" | $COMPRESSION >> $OUTPUT_DIR/$dbname.$EXT
|
||||
DB_COUNT=$((DB_COUNT+1))
|
||||
echo "${txtbld}Database $dbname extracted from $SOURCE at $OUTPUT_DIR/$dbname.$EXT${txtrst}"
|
||||
done;
|
||||
@@ -233,22 +263,30 @@ dump_splitter()
|
||||
;;
|
||||
|
||||
ALLTABLES)
|
||||
|
||||
for tablename in $($DECOMPRESSION $SOURCE | grep "Table structure for table " | awk -F"\`" {'print $2'})
|
||||
do
|
||||
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||
include_dump_info tablename
|
||||
|
||||
#Extract table specific dump to tablename.sql
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" | $COMPRESSION > $OUTPUT_DIR/$tablename.$EXT
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" | $COMPRESSION >> $OUTPUT_DIR/$tablename.$EXT
|
||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||
echo "${txtbld}Table $tablename extracted from $DUMP_FILE at $OUTPUT_DIR/$tablename.$EXT${txtrst}"
|
||||
done;
|
||||
echo "${txtbld}Total $TABLE_COUNT tables extracted.${txtrst}"
|
||||
;;
|
||||
REGEXP)
|
||||
|
||||
TABLE_COUNT=0;
|
||||
for tablename in $(grep -E "Table structure for table \`$MATCH_STR" $SOURCE| awk -F"\`" {'print $2'})
|
||||
for tablename in $($DECOMPRESSION $SOURCE | grep -E "Table structure for table \`$MATCH_STR" | awk -F"\`" {'print $2'})
|
||||
do
|
||||
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||
include_dump_info $tablename
|
||||
|
||||
echo "Extracting $tablename..."
|
||||
#Extract table specific dump to tablename.sql
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" | $COMPRESSION > $OUTPUT_DIR/$tablename.$EXT
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" | $COMPRESSION >> $OUTPUT_DIR/$tablename.$EXT
|
||||
echo "${txtbld}Table $tablename extracted from $DUMP_FILE at $OUTPUT_DIR/$tablename.$EXT${txtrst}"
|
||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||
done;
|
||||
@@ -256,6 +294,7 @@ dump_splitter()
|
||||
;;
|
||||
|
||||
DBTABLE)
|
||||
|
||||
MATCH_DB=`echo $MATCH_STR | awk -F "." {'print $1'}`
|
||||
MATCH_TBLS=`echo $MATCH_STR | awk -F "." {'print $2'}`
|
||||
if [ "$MATCH_TBLS" = "*" ]; then
|
||||
@@ -267,8 +306,10 @@ dump_splitter()
|
||||
do
|
||||
echo "Extracting $tablename..."
|
||||
#Extract table specific dump to tablename.sql
|
||||
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||
include_dump_info $tablename
|
||||
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_DB\`/,/^-- Current Database: /p" | sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" | $COMPRESSION > $OUTPUT_DIR/$tablename.$EXT
|
||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_DB\`/,/^-- Current Database: /p" | sed -n "/^-- Table structure for table \`$tablename\`/,/^-- Table structure for table/p" | $COMPRESSION >> $OUTPUT_DIR/$tablename.$EXT
|
||||
echo "${txtbld}Table $tablename extracted from $DUMP_FILE at $OUTPUT_DIR/$tablename.$EXT${txtrst}"
|
||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||
done;
|
||||
@@ -300,15 +341,6 @@ while [ "$1" != "" ]; do
|
||||
missing_arg --source
|
||||
fi;
|
||||
SOURCE=$1 ;;
|
||||
--desc )
|
||||
EXTRACT="none"
|
||||
echo "-------------------------------";
|
||||
echo "Database\t\tTables";
|
||||
echo "-------------------------------";
|
||||
$DECOMPRESSION $SOURCE | grep -E "(^-- Current Database:|^-- Table structure for table)" | sed 's/-- Current Database: /-------------------------------\n/' | sed 's/-- Table structure for table /\t\t/'| sed 's/`//g' ;
|
||||
echo "-------------------------------";
|
||||
exit 0;
|
||||
;;
|
||||
|
||||
--extract|-E ) shift
|
||||
if [ -z $1 ]; then
|
||||
@@ -335,6 +367,17 @@ while [ "$1" != "" ]; do
|
||||
missing_arg --match_str
|
||||
fi;
|
||||
MATCH_STR=$1 ;;
|
||||
--desc )
|
||||
EXTRACT="DESCRIBE"
|
||||
parse_result
|
||||
echo "-------------------------------";
|
||||
echo "Database\t\tTables";
|
||||
echo "-------------------------------";
|
||||
$DECOMPRESSION $SOURCE | grep -E "(^-- Current Database:|^-- Table structure for table)" | sed 's/-- Current Database: /-------------------------------\n/' | sed 's/-- Table structure for table /\t\t/'| sed 's/`//g' ;
|
||||
echo "-------------------------------";
|
||||
exit 0;
|
||||
;;
|
||||
|
||||
--config ) shift;
|
||||
if [ -z $1 ]; then
|
||||
missing_arg --config
|
||||
|
Reference in New Issue
Block a user