Yesterday on Twitter someone asked if there was a way to export data from mysql, but only from tables matching a like pattern. E.g. something like
mysqldump -uuser -p mydb mytables_*
There isn't an inbuilt mechanism to do this, my reply was to use a shell script with an array containing a list of tables you wanted to export. Another reply had a better way, using a call to mysql to get a list of tables matching a glob pattern, putting them in an array and then iterating over that array with successive calls to mysqldump.
I put this suggestion together with mine, made it more generic and popped it on github for reference.
Personally I've placed this in my user's .bash_functions (included by .bashrc).
#!/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