您可以从mysql获取所需的表名,然后使用它们来构建mysql dump参数。
在下面的示例中,只需将“ someprefix”替换为您的前缀(例如“ exam_”)即可。
该SHOW TABLES
查询可以改变,以寻找其他的套表。或者,您可以对INFORMATION_SCHEMA
表使用查询以使用更多条件。
#/bin/bash
#this could be improved but it works
read -p "Mysql username and password" user pass
#specify your database, e.g. "mydb"
DB="mydb"
SQL_STRING='SHOW TABLES LIKE "someprefix%";'
DBS=$(echo $SQL_STRING | mysql -u $user -p$pass -Bs --database=$DB )
#next two lines untested, but intended to add a second excluded table prefix
#ANOTHER_SQL_STRING='SHOW TABLES LIKE "otherprefix%";'
#DBS="$DBS""\n"$(echo $ANOTHER_SQL_STRING | mysql -u $user -p$pass -Bs --database=$DB )
#-B is for batch - tab-separated columns, newlines between rows
#-s is for silent - produce less output
#both result in escaping special characters
#but the following might not work if you have special characters in your table names
IFS=$'\n' read -r -a TABLES <<< $DBS
IGNORE="--ignore_table="$DB"."
IGNORE_TABLES=""
for table in $TABLES; do
IGNORE_TABLES=$IGNORE_TABLES" --ignore_table="$DB"."$table
done
#Now you have a string in $IGNORE_TABLES like this: "--ignore_table=someprefix1 --ignore_table=someprefix2 ..."
mysqldump $DB --routines -u $user -p$pass $IGNORE_TABLES > specialdump.sql
这是在有关获取“ bash中所有表除外”的答案的帮助下构建的:https : //stackoverflow.com/a/9232076/631764
以及有关跳过使用一些bash的表的答案:https : //stackoverflow.com/a/425172/631764