Created
October 4, 2014 19:33
-
-
Save maxlawton/14f37a43345a30cd3a68 to your computer and use it in GitHub Desktop.
Dump MySQL tables matching prefix
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
#!/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