Created
May 14, 2019 10:37
-
-
Save othyn/811536fd460cea3ffcbe579469ec1631 to your computer and use it in GitHub Desktop.
Short bash script to extract a table from a MySQL dump, designed to perform well on large files! :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
USAGE="\n\x1b[31mUsage:\x1b[0m extract_table_from_mysql_dump.sh my_table /path/to/dump.sql\n\x1b[32mOptional 3rd parameter:\x1b[0m /path/to/export/to\nThe export filename will default to extracted_{my_table}.sql\n" | |
if [[ -z "$1" ]] ; then | |
printf "$USAGE" | |
exit 1 | |
fi | |
# Check the table name was provided | |
if [[ -z "$2" ]] ; then | |
printf "$USAGE" | |
exit 1 | |
fi | |
# Check the input path to the dump was provided | |
if [[ -z "$3" ]] ; then | |
OUTPUT="~/extracted_$1.sql" | |
else | |
OUTPUT="$3/extracted_$1.sql" | |
fi | |
# Check the output path to the dump was provided | |
# If not, use a default one | |
sed -n -e "/DROP TABLE.*\`$1\`/,/UNLOCK TABLES/p" $2 > $OUTPUT | |
# Run. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment