Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@othyn
Created May 14, 2019 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save othyn/811536fd460cea3ffcbe579469ec1631 to your computer and use it in GitHub Desktop.
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! :)
#!/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