Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Short bash script to extract a table from a MySQL dump, designed to perform well on large files! :)
#!/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
You can’t perform that action at this time.