Skip to content

Instantly share code, notes, and snippets.

@ajbonner
Created June 9, 2011 15:08
Show Gist options
  • Save ajbonner/1016920 to your computer and use it in GitHub Desktop.
Save ajbonner/1016920 to your computer and use it in GitHub Desktop.
Mysqldump data from database tables matching a pattern
#!/bin/bash
if [ $# -lt 5 ]; then
echo "Exports data from mysql database in tables matching a like pattern e.g. 'table_%'"
echo "Usage: $0 dbname dbuser dbpass pattern outputfile"
exit 1
fi
DBNAME=$1
DBUSER=$2
DBPASS=$3
PATTERN=$4
OUTPUTFILE=$5
TABLES=( `mysql -u$DBUSER -p$DBPASS $DBNAME --silent -e "show tables like '$PATTERN'"` )
for TABLE in "${TABLES[@]}"; do
mysqldump -u$DBUSER -p$DBPASS $DBNAME $TABLE >> $OUTPUTFILE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment