Skip to content

Instantly share code, notes, and snippets.

@cmattoon
Last active August 29, 2015 14:16
Show Gist options
  • Save cmattoon/b8f870eddc6efa1e137d to your computer and use it in GitHub Desktop.
Save cmattoon/b8f870eddc6efa1e137d to your computer and use it in GitHub Desktop.
Extract a table from mysql dump
# The basic command that gets run
sed -n '/CREATE TABLE.*table/,/UNLOCK TABLES/p' full_database_backup.sql > table.sql
#!/bin/bash
INFILE=$(READLINK -f $1);
TABLE_NAME=$2
if [ $# -eq 2 ]; then
if [ ! -f "$INFILE" ]; then
echo "First parameter must be a valid file";
exit 1;
fi;
if [ ! -r "$INFILE" ]; then
echo "Cannot read $INFILE. Do you have permission?";
exit 1;
fi;
sed -n "/CREATE TABLE.*${TABLE_NAME}/,/UNLOCK TABLES/p" $INFILE;
exit 0;
else
echo "Usage: dbextract [file] [table_name]";
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment