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
|
#!/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.
|
# Extracts database, table, all databases, all tables or tables matching on regular expression from the mysqldump.
|
||||||
# Includes output compression options.
|
# Includes output compression options.
|
||||||
# By: Kedar Vaijanapurkar
|
# By: Kedar Vaijanapurkar
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
# Ver. 2.0: Feb, 2015
|
# Ver. 2.0: Feb, 2015
|
||||||
# ... Added database extract and compression
|
# ... Added database extract and compression
|
||||||
# Ver. 3.0: March, 2015
|
# Ver. 3.0: March, 2015
|
||||||
# ... Complete rewrite.
|
# ... Complete rewrite.
|
||||||
# ... Extract all databases.
|
# ... Extract all databases.
|
||||||
# Ver. 4.0: March, 2015
|
# Ver. 4.0: March, 2015
|
||||||
# ... More validations and bug fixes.
|
# ... More validations and bug fixes.
|
||||||
@@ -25,6 +25,11 @@
|
|||||||
# Ver. 5.0: Apr, 2015
|
# Ver. 5.0: Apr, 2015
|
||||||
# ... Describing the dump, listing all databases and tables
|
# ... Describing the dump, listing all databases and tables
|
||||||
# ... Extracting one or more tables from single database
|
# ... 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
|
# ToDo: Work with straming input
|
||||||
@@ -51,34 +56,34 @@ TABLE_NAME='';
|
|||||||
DB_NAME='';
|
DB_NAME='';
|
||||||
COMPRESSION='gzip';
|
COMPRESSION='gzip';
|
||||||
DECOMPRESSION='cat';
|
DECOMPRESSION='cat';
|
||||||
VERSION=5.0
|
VERSION=6.0
|
||||||
|
|
||||||
## Usage Description
|
## Usage Description
|
||||||
usage()
|
usage()
|
||||||
{
|
{
|
||||||
echo "\n\t\t\t\t\t\t\t${txtgrn}${txtund}************ Usage ************ \n"${txtrst};
|
echo "\n\t\t\t\t\t\t\t${txtgrn}${txtund}************ Usage ************ \n"${txtrst};
|
||||||
echo "${txtgrn}sh mysqldumpsplitter.sh --source filename --extract [DB|TABLE|DBTABLES|ALLDBS|ALLTABLES|REGEXP] --match_str string --compression [gzip|pigz|bzip2|none] --decompression [gzip|pigz|bzip2|none] --output_dir [path to output dir] [--config /path/to/config] ${txtrst}"
|
echo "${txtgrn}sh mysqldumpsplitter.sh --source filename --extract [DB|TABLE|DBTABLES|ALLDBS|ALLTABLES|REGEXP] --match_str string --compression [gzip|pigz|bzip2|none] --decompression [gzip|pigz|bzip2|none] --output_dir [path to output dir] [--config /path/to/config] ${txtrst}"
|
||||||
echo "${txtund} ${txtrst}"
|
echo "${txtund} ${txtrst}"
|
||||||
echo "OPTIONS:"
|
echo "OPTIONS:"
|
||||||
echo "${txtund} ${txtrst}"
|
echo "${txtund} ${txtrst}"
|
||||||
echo " --source: mysqldump filename to process. It could be a compressed or regular file."
|
echo " --source: mysqldump filename to process. It could be a compressed or regular file."
|
||||||
echo " --desc: This option will list out all databases and tables."
|
echo " --desc: This option will list out all databases and tables."
|
||||||
echo " --extract: Specify what to extract. Possible values DB, TABLE, ALLDBS, ALLTABLES, REGEXP"
|
echo " --extract: Specify what to extract. Possible values DB, TABLE, ALLDBS, ALLTABLES, REGEXP"
|
||||||
echo " --match_str: Specify match string for extract command option."
|
echo " --match_str: Specify match string for extract command option."
|
||||||
echo " --compression: gzip/pigz/bzip2/none (default: gzip). Extracted file will be of this compression."
|
echo " --compression: gzip/pigz/bzip2/none (default: gzip). Extracted file will be of this compression."
|
||||||
echo " --decompression: gzip/pigz/bzip2/none (default: gzip). This will be used against input file."
|
echo " --decompression: gzip/pigz/bzip2/none (default: gzip). This will be used against input file."
|
||||||
echo " --output_dir: path to output dir. (default: ./out/)"
|
echo " --output_dir: path to output dir. (default: ./out/)"
|
||||||
echo " --config: path to config file. You may use --config option to specify the config file that includes following variables."
|
echo " --config: path to config file. You may use --config option to specify the config file that includes following variables."
|
||||||
echo "\t\tSOURCE=
|
echo "\t\tSOURCE=
|
||||||
\t\tEXTRACT=
|
\t\tEXTRACT=
|
||||||
\t\tCOMPRESSION=
|
\t\tCOMPRESSION=
|
||||||
\t\tDECOMPRESSION=
|
\t\tDECOMPRESSION=
|
||||||
\t\tOUTPUT_DIR=
|
\t\tOUTPUT_DIR=
|
||||||
\t\tMATCH_STR=
|
\t\tMATCH_STR=
|
||||||
"
|
"
|
||||||
echo "${txtund} ${txtrst}"
|
echo "${txtund} ${txtrst}"
|
||||||
echo "Ver. $VERSION"
|
echo "Ver. $VERSION"
|
||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
## Parsing and processing input
|
## Parsing and processing input
|
||||||
@@ -86,45 +91,45 @@ parse_result()
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
## Validate SOURCE is provided and exists
|
## Validate SOURCE is provided and exists
|
||||||
if [ -z $SOURCE ]; then
|
if [ -z $SOURCE ]; then
|
||||||
echo "${txtred}ERROR: Source file not specified or does not exist. (Entered: $SOURCE)${txtrst}"
|
echo "${txtred}ERROR: Source file not specified or does not exist. (Entered: $SOURCE)${txtrst}"
|
||||||
elif [ ! -f $SOURCE ]; then
|
elif [ ! -f $SOURCE ]; then
|
||||||
echo "${txtred}ERROR: Source file does not exist. (Entered: $SOURCE)${txtrst}"
|
echo "${txtred}ERROR: Source file does not exist. (Entered: $SOURCE)${txtrst}"
|
||||||
exit 2;
|
exit 2;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## Parse Extract Operation
|
## Parse Extract Operation
|
||||||
case $EXTRACT in
|
case $EXTRACT in
|
||||||
ALLDBS|ALLTABLES )
|
ALLDBS|ALLTABLES|DESCRIBE )
|
||||||
if [ "$MATCH_STR" != '' ]; then
|
if [ "$MATCH_STR" != '' ]; then
|
||||||
echo "${txtylw}Ignoring option --match_string.${txtrst}"
|
echo "${txtylw}Ignoring option --match_string.${txtrst}"
|
||||||
fi;
|
fi;
|
||||||
;;
|
;;
|
||||||
DB|TABLE|REGEXP|DBTABLE)
|
DB|TABLE|REGEXP|DBTABLE)
|
||||||
if [ "$MATCH_STR" = '' ]; then
|
if [ "$MATCH_STR" = '' ]; then
|
||||||
echo "${txtred}ERROR: Expecting input for option --match_string.${txtrst}"
|
echo "${txtred}ERROR: Expecting input for option --match_string.${txtrst}"
|
||||||
exit 1;
|
exit 1;
|
||||||
fi;
|
fi;
|
||||||
;;
|
;;
|
||||||
* ) echo "${txtred}ERROR: Please specify correct option for --extract.${txtrst}"
|
* ) echo "${txtred}ERROR: Please specify correct option for --extract.${txtrst}"
|
||||||
usage;
|
usage;
|
||||||
esac;
|
esac;
|
||||||
|
|
||||||
## Parse compression
|
## Parse compression
|
||||||
if [ "$COMPRESSION" = 'none' ]; then
|
if [ "$COMPRESSION" = 'none' ]; then
|
||||||
COMPRESSION='cat';
|
COMPRESSION='cat';
|
||||||
EXT="sql"
|
EXT="sql"
|
||||||
echo "${txtgrn}Setting no compression.${txtrst}";
|
echo "${txtgrn}Setting no compression.${txtrst}";
|
||||||
elif [ "$COMPRESSION" = 'pigz' ]; then
|
elif [ "$COMPRESSION" = 'pigz' ]; then
|
||||||
which $COMPRESSION &>/dev/null
|
which $COMPRESSION &>/dev/null
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "${txtred}WARNING:$COMPRESSION appears having issues, using default gzip.${txtrst}";
|
echo "${txtred}WARNING:$COMPRESSION appears having issues, using default gzip.${txtrst}";
|
||||||
COMPRESSION="gzip";
|
COMPRESSION="gzip";
|
||||||
fi;
|
fi;
|
||||||
echo "${txtgrn}Setting compression as $COMPRESSION.${txtrst}";
|
echo "${txtgrn}Setting compression as $COMPRESSION.${txtrst}";
|
||||||
EXT="sql.gz"
|
EXT="sql.gz"
|
||||||
elif [ "$COMPRESSION" = 'bzip2' ]; then
|
elif [ "$COMPRESSION" = 'bzip2' ]; then
|
||||||
which $COMPRESSION &>/dev/null
|
which $COMPRESSION &>/dev/null
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "${txtred}WARNING:$COMPRESSION appears having issues, using default gzip.${txtrst}";
|
echo "${txtred}WARNING:$COMPRESSION appears having issues, using default gzip.${txtrst}";
|
||||||
@@ -132,14 +137,14 @@ parse_result()
|
|||||||
fi;
|
fi;
|
||||||
echo "${txtgrn}Setting compression as $COMPRESSION.${txtrst}";
|
echo "${txtgrn}Setting compression as $COMPRESSION.${txtrst}";
|
||||||
EXT="sql.bz2";
|
EXT="sql.bz2";
|
||||||
else
|
else
|
||||||
COMPRESSION='gzip';
|
COMPRESSION='gzip';
|
||||||
echo "${txtgrn}Setting compression $COMPRESSION (default).${txtrst}";
|
echo "${txtgrn}Setting compression $COMPRESSION (default).${txtrst}";
|
||||||
EXT="sql.gz"
|
EXT="sql.gz"
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
|
|
||||||
## Parse decompression
|
## Parse decompression
|
||||||
if [ "$DECOMPRESSION" = 'none' ]; then
|
if [ "$DECOMPRESSION" = 'none' ]; then
|
||||||
DECOMPRESSION='cat';
|
DECOMPRESSION='cat';
|
||||||
echo "${txtgrn}Setting no decompression.${txtrst}";
|
echo "${txtgrn}Setting no decompression.${txtrst}";
|
||||||
@@ -167,187 +172,225 @@ parse_result()
|
|||||||
fi;
|
fi;
|
||||||
|
|
||||||
|
|
||||||
## Verify file type:
|
## Verify file type:
|
||||||
filecommand=`file $SOURCE`
|
filecommand=`file $SOURCE`
|
||||||
echo $filecommand | grep "compressed" 1>/dev/null
|
echo $filecommand | grep "compressed" 1>/dev/null
|
||||||
if [ `echo $?` -eq 0 ]
|
if [ `echo $?` -eq 0 ]
|
||||||
then
|
then
|
||||||
echo "${txtylw}File $SOURCE is a compressed dump.${txtrst}"
|
echo "${txtylw}File $SOURCE is a compressed dump.${txtrst}"
|
||||||
if [ "$DECOMPRESSION" = 'cat' ]; then
|
if [ "$DECOMPRESSION" = 'cat' ]; then
|
||||||
echo "${txtred} The input file $SOURCE appears to be a compressed dump. \n While the decompression is set to none.\n Please specify ${txtund}--decompression [gzip|bzip2|pigz]${txtrst}${txtred} argument.${txtrst}";
|
echo "${txtred} The input file $SOURCE appears to be a compressed dump. \n While the decompression is set to none.\n Please specify ${txtund}--decompression [gzip|bzip2|pigz]${txtrst}${txtred} argument.${txtrst}";
|
||||||
exit 1;
|
exit 1;
|
||||||
fi;
|
fi;
|
||||||
else
|
else
|
||||||
echo "${txtylw}File $SOURCE is a regular dump.${txtrst}"
|
echo "${txtylw}File $SOURCE is a regular dump.${txtrst}"
|
||||||
if [ "$DECOMPRESSION" != 'cat' ]; then
|
if [ "$DECOMPRESSION" != 'cat' ]; then
|
||||||
echo "${txtred} Default decompression method for source is gzip. \n The input file $SOURCE does not appear a compressed dump. \n ${txtylw}We will try using no decompression. Please consider specifying ${txtund}--decompression none${txtrst}${txtylw} argument.${txtrst}";
|
echo "${txtred} Default decompression method for source is gzip. \n The input file $SOURCE does not appear a compressed dump. \n ${txtylw}We will try using no decompression. Please consider specifying ${txtund}--decompression none${txtrst}${txtylw} argument.${txtrst}";
|
||||||
DECOMPRESSION='cat'; ## Auto correct decompression to none for regular files.
|
DECOMPRESSION='cat'; ## Auto correct decompression to none for regular files.
|
||||||
fi;
|
fi;
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
|
|
||||||
# Output directory
|
# Output directory
|
||||||
if [ "$OUTPUT_DIR" = "" ]; then
|
if [ "$OUTPUT_DIR" = "" ]; then
|
||||||
OUTPUT_DIR="out";
|
OUTPUT_DIR="out";
|
||||||
fi;
|
fi;
|
||||||
mkdir -p $OUTPUT_DIR
|
mkdir -p $OUTPUT_DIR
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo "${txtgrn}Setting output directory: $OUTPUT_DIR.${txtrst}";
|
echo "${txtgrn}Setting output directory: $OUTPUT_DIR.${txtrst}";
|
||||||
else
|
else
|
||||||
echo "${txtred}ERROR:Issue while checking output directory: $OUTPUT_DIR.${txtrst}";
|
echo "${txtred}ERROR:Issue while checking output directory: $OUTPUT_DIR.${txtrst}";
|
||||||
exit 2;
|
exit 2;
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
echo "${txtylw}Processing: Extract $EXTRACT $MATCH_STR from $SOURCE with compression option as $COMPRESSION and output location as $OUTPUT_DIR${txtrst}";
|
echo "${txtylw}Processing: Extract $EXTRACT $MATCH_STR from $SOURCE with compression option as $COMPRESSION and output location as $OUTPUT_DIR${txtrst}";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 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
|
## Actual dump splitting
|
||||||
dump_splitter()
|
dump_splitter()
|
||||||
{
|
{
|
||||||
case $EXTRACT in
|
case $EXTRACT in
|
||||||
DB)
|
DB)
|
||||||
echo "Extracting Database: $MATCH_STR";
|
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||||
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_STR\`/,/^-- Current Database: /p" | $COMPRESSION > $OUTPUT_DIR/$MATCH_STR.$EXT
|
include_dump_info $MATCH_STR
|
||||||
echo "${txtbld} Database $MATCH_STR extracted from $SOURCE at $OUTPUT_DIR${txtrst}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
TABLE)
|
echo "Extracting Database: $MATCH_STR";
|
||||||
#Loop for each tablename found in provided dumpfile
|
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_STR\`/,/^-- Current Database: /p" | $COMPRESSION >> $OUTPUT_DIR/$MATCH_STR.$EXT
|
||||||
echo "Extracting $MATCH_STR."
|
echo "${txtbld} Database $MATCH_STR extracted from $SOURCE at $OUTPUT_DIR${txtrst}"
|
||||||
#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
|
|
||||||
echo "${txtbld} Table $MATCH_STR extracted from $SOURCE at $OUTPUT_DIR${txtrst}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
ALLDBS)
|
TABLE)
|
||||||
for dbname in $($DECOMPRESSION $SOURCE | grep -E "^-- Current Database: " | awk -F"\`" {'print $2'})
|
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||||
do
|
include_dump_info $MATCH_STR
|
||||||
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
|
|
||||||
DB_COUNT=$((DB_COUNT+1))
|
|
||||||
echo "${txtbld}Database $dbname extracted from $SOURCE at $OUTPUT_DIR/$dbname.$EXT${txtrst}"
|
|
||||||
done;
|
|
||||||
echo "${txtbld}Total $DB_COUNT databases extracted.${txtrst}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
ALLTABLES)
|
#Loop for each tablename found in provided dumpfile
|
||||||
for tablename in $($DECOMPRESSION $SOURCE | grep "Table structure for table " | awk -F"\`" {'print $2'})
|
echo "Extracting $MATCH_STR."
|
||||||
do
|
#Extract table specific dump to tablename.sql
|
||||||
#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 \`$tablename\`/,/^-- Table structure for table/p" | $COMPRESSION > $OUTPUT_DIR/$tablename.$EXT
|
echo "${txtbld} Table $MATCH_STR extracted from $SOURCE at $OUTPUT_DIR${txtrst}"
|
||||||
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'})
|
|
||||||
do
|
|
||||||
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
|
|
||||||
echo "${txtbld}Table $tablename extracted from $DUMP_FILE at $OUTPUT_DIR/$tablename.$EXT${txtrst}"
|
|
||||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
|
||||||
done;
|
|
||||||
echo "${txtbld}Total $TABLE_COUNT tables extracted.${txtrst}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
DBTABLE)
|
ALLDBS)
|
||||||
MATCH_DB=`echo $MATCH_STR | awk -F "." {'print $1'}`
|
for dbname in $($DECOMPRESSION $SOURCE | grep -E "^-- Current Database: " | awk -F"\`" {'print $2'})
|
||||||
MATCH_TBLS=`echo $MATCH_STR | awk -F "." {'print $2'}`
|
do
|
||||||
if [ "$MATCH_TBLS" = "*" ]; then
|
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||||
MATCH_TBLS='';
|
include_dump_info dbname
|
||||||
fi;
|
|
||||||
TABLE_COUNT=0;
|
|
||||||
|
|
||||||
for tablename in $( $DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_DB\`/,/^-- Current Database: /p" | grep -E "^-- Table structure for table \`$MATCH_TBLS" | awk -F '\`' {'print $2'} )
|
echo "Extracting Database $dbname..."
|
||||||
do
|
#Extract database specific dump to database.sql.gz
|
||||||
echo "Extracting $tablename..."
|
$DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$dbname\`/,/^-- Current Database: /p" | $COMPRESSION >> $OUTPUT_DIR/$dbname.$EXT
|
||||||
#Extract table specific dump to tablename.sql
|
DB_COUNT=$((DB_COUNT+1))
|
||||||
|
echo "${txtbld}Database $dbname extracted from $SOURCE at $OUTPUT_DIR/$dbname.$EXT${txtrst}"
|
||||||
|
done;
|
||||||
|
echo "${txtbld}Total $DB_COUNT databases extracted.${txtrst}"
|
||||||
|
;;
|
||||||
|
|
||||||
$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
|
ALLTABLES)
|
||||||
echo "${txtbld}Table $tablename extracted from $DUMP_FILE at $OUTPUT_DIR/$tablename.$EXT${txtrst}"
|
|
||||||
TABLE_COUNT=$((TABLE_COUNT+1))
|
|
||||||
done;
|
|
||||||
echo "${txtbld}Total $TABLE_COUNT tables extracted from $MATCH_DB.${txtrst}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
*) echo "Wrong option, exiting.";
|
for tablename in $($DECOMPRESSION $SOURCE | grep "Table structure for table " | awk -F"\`" {'print $2'})
|
||||||
usage;
|
do
|
||||||
exit 1;;
|
# Include first 17 lines of standard mysqldump to preserve time_zone and charset.
|
||||||
esac
|
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
|
||||||
|
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 $($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
|
||||||
|
echo "${txtbld}Table $tablename extracted from $DUMP_FILE at $OUTPUT_DIR/$tablename.$EXT${txtrst}"
|
||||||
|
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||||
|
done;
|
||||||
|
echo "${txtbld}Total $TABLE_COUNT tables extracted.${txtrst}"
|
||||||
|
;;
|
||||||
|
|
||||||
|
DBTABLE)
|
||||||
|
|
||||||
|
MATCH_DB=`echo $MATCH_STR | awk -F "." {'print $1'}`
|
||||||
|
MATCH_TBLS=`echo $MATCH_STR | awk -F "." {'print $2'}`
|
||||||
|
if [ "$MATCH_TBLS" = "*" ]; then
|
||||||
|
MATCH_TBLS='';
|
||||||
|
fi;
|
||||||
|
TABLE_COUNT=0;
|
||||||
|
|
||||||
|
for tablename in $( $DECOMPRESSION $SOURCE | sed -n "/^-- Current Database: \`$MATCH_DB\`/,/^-- Current Database: /p" | grep -E "^-- Table structure for table \`$MATCH_TBLS" | awk -F '\`' {'print $2'} )
|
||||||
|
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
|
||||||
|
echo "${txtbld}Table $tablename extracted from $DUMP_FILE at $OUTPUT_DIR/$tablename.$EXT${txtrst}"
|
||||||
|
TABLE_COUNT=$((TABLE_COUNT+1))
|
||||||
|
done;
|
||||||
|
echo "${txtbld}Total $TABLE_COUNT tables extracted from $MATCH_DB.${txtrst}"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*) echo "Wrong option, exiting.";
|
||||||
|
usage;
|
||||||
|
exit 1;;
|
||||||
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
missing_arg()
|
missing_arg()
|
||||||
{
|
{
|
||||||
echo "${txtred}ERROR:Missing argument $1.${txtrst}"
|
echo "${txtred}ERROR:Missing argument $1.${txtrst}"
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ "$#" -eq 0 ]; then
|
if [ "$#" -eq 0 ]; then
|
||||||
usage;
|
usage;
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Accepts Parameters
|
# Accepts Parameters
|
||||||
while [ "$1" != "" ]; do
|
while [ "$1" != "" ]; do
|
||||||
case $1 in
|
case $1 in
|
||||||
--source|-S ) shift
|
--source|-S ) shift
|
||||||
if [ -z $1 ]; then
|
if [ -z $1 ]; then
|
||||||
missing_arg --source
|
missing_arg --source
|
||||||
fi;
|
fi;
|
||||||
SOURCE=$1 ;;
|
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
|
--extract|-E ) shift
|
||||||
if [ -z $1 ]; then
|
if [ -z $1 ]; then
|
||||||
missing_arg --extract
|
missing_arg --extract
|
||||||
fi;
|
fi;
|
||||||
EXTRACT=$1 ;;
|
EXTRACT=$1 ;;
|
||||||
--compression|-C ) shift
|
--compression|-C ) shift
|
||||||
if [ -z $1 ]; then
|
if [ -z $1 ]; then
|
||||||
missing_arg --compression
|
missing_arg --compression
|
||||||
fi;
|
fi;
|
||||||
COMPRESSION=$1 ;;
|
COMPRESSION=$1 ;;
|
||||||
--decompression|-D) shift
|
--decompression|-D) shift
|
||||||
if [ -z $1 ]; then
|
if [ -z $1 ]; then
|
||||||
missing_arg --decompression
|
missing_arg --decompression
|
||||||
fi;
|
fi;
|
||||||
DECOMPRESSION=$1 ;;
|
DECOMPRESSION=$1 ;;
|
||||||
--output_dir|-O ) shift
|
--output_dir|-O ) shift
|
||||||
if [ -z $1 ]; then
|
if [ -z $1 ]; then
|
||||||
missing_arg --output_dir
|
missing_arg --output_dir
|
||||||
fi;
|
fi;
|
||||||
OUTPUT_DIR=$1 ;;
|
OUTPUT_DIR=$1 ;;
|
||||||
--match_str|-M ) shift
|
--match_str|-M ) shift
|
||||||
if [ -z $1 ]; then
|
if [ -z $1 ]; then
|
||||||
missing_arg --match_str
|
missing_arg --match_str
|
||||||
fi;
|
fi;
|
||||||
MATCH_STR=$1 ;;
|
MATCH_STR=$1 ;;
|
||||||
--config ) shift;
|
--desc )
|
||||||
if [ -z $1 ]; then
|
EXTRACT="DESCRIBE"
|
||||||
missing_arg --config
|
parse_result
|
||||||
fi;
|
echo "-------------------------------";
|
||||||
if [ ! -f $1 ]; then
|
echo "Database\t\tTables";
|
||||||
echo "${txtred}ERROR: Config file $1 does not exist.${txtrst}"
|
echo "-------------------------------";
|
||||||
exit 2;
|
$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' ;
|
||||||
fi;
|
echo "-------------------------------";
|
||||||
|
exit 0;
|
||||||
|
;;
|
||||||
|
|
||||||
|
--config ) shift;
|
||||||
|
if [ -z $1 ]; then
|
||||||
|
missing_arg --config
|
||||||
|
fi;
|
||||||
|
if [ ! -f $1 ]; then
|
||||||
|
echo "${txtred}ERROR: Config file $1 does not exist.${txtrst}"
|
||||||
|
exit 2;
|
||||||
|
fi;
|
||||||
. ./$1 ;;
|
. ./$1 ;;
|
||||||
-h ) usage
|
-h ) usage
|
||||||
exit ;;
|
exit ;;
|
||||||
* ) echo "";
|
* ) echo "";
|
||||||
usage
|
usage
|
||||||
exit 1
|
exit 1
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
|
Reference in New Issue
Block a user