Skip to content

Instantly share code, notes, and snippets.

@maxlawton
Created October 4, 2014 19:33
Show Gist options
  • Save maxlawton/14f37a43345a30cd3a68 to your computer and use it in GitHub Desktop.
Save maxlawton/14f37a43345a30cd3a68 to your computer and use it in GitHub Desktop.
Dump MySQL tables matching prefix
#!/usr/bin/env bash
if [ "$#" -ge 2 ]
then
DBNAME="$1"; shift
PREFIX="$1"; shift
else
echo "usage: mysql-dump-like.sh <database> <table_prefix> [<mysql_options...>]"
exit 1
fi
STAMP=$(date +%Y%m%d_%H%M)
FILE="${STAMP}-${DBNAME}-${PREFIX}.sql"
SCHEMA=$(cat<<EOF
SELECT \`TABLE_NAME\` FROM \`TABLES\`
WHERE \`TABLE_SCHEMA\` = '${DBNAME}'
AND \`TABLE_NAME\` LIKE '${PREFIX}%'
EOF
)
TABLES=$(mysql -N information_schema -e "$SCHEMA")
cat<<EOF
> Dumping \`${DBNAME}\`.\`${PREFIX}*\` into '${FILE}'
EOF
mysqldump --routines --skip-lock-tables $* --databases $DBNAME --tables $TABLES > $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment