File per Table

From Hashmysql
Jump to: navigation, search
#!/bin/bash

if [ "$1" = "" ]; then
   PASSWD=
else
   PASSWD="-p$1"
fi

#
# DB_IGNORE_LIST is for those schemas which we do not want dump the data
#
DB_IGNORE_LIST="information_schema|mysql"
DB_IGNORE_LIST="information_schema"
USER=root
SOCKET="/tmp/mysql.sock"

#
# Really shouldn't need to do much editing below here.
#
DB_LIST=$( mysql -u $USER $PASSWD -S $SOCKET --skip-column-names -Be "SHOW DATABASES" | egrep -v "$DB_IGNORE_LIST" )
for db in $DB_LIST
do
   if [ !-e $db ]; then mkdir -p $db; fi
   TABLE_LIST=$( mysql -u $USER -S $SOCKET --skip-column-names -Be "SHOW TABLES" $db )
   for table in $TABLE_LIST
   do
      mysqldump -S $SOCKET -u $USER $PASSWD $db $table> $db/$table.sql
   done
done