Mysqldump only tables with certain prefix / Mysqldump wildcards?

后端 未结 9 2539
一个人的身影
一个人的身影 2020-12-07 07:37

I have this huge, messy database I am cleaning up. It houses 500+ tables, which is the result of combining Magento Enterprise with Joomla in one single DB.

To make t

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 08:16

    Another oneliner to extract list of tables' name with mysql -sN … and then use each item in a "for … in … " shell loop to drop them:

    for f in `mysql dbname -sN -e "SHOW TABLES LIKE 'bak\_%' "` ; do mysql dbname -rsN -e "DROP TABLE $f"; done
    

    or (expanded version)

    for f in `mysql dbname -sN -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME LIKE 'bak\_%' "` ; do mysql dbname -rsN -e "DROP TABLE $f"; done
    

    Or use "group_concat" to concatenate* names of tables, if they are short enough:

    tables=`mysql dbname -srN -e "SELECT GROUP_CONCAT(TABLE_NAME SEPARATOR ',') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname'  AND TABLE_NAME LIKE 'bak\_%' "`; mysql dbname -rsN -e "DROP TABLE $tables"
    

    *some limits like the value of "group_concat_max_len" (typically equals to 1024, cf your 70 tables) may interfere.


    Same principle, but for dumping all tables except the ones starting with "bak_":

    for f in `mysql dbname -sN -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname' AND NOT(TABLE_NAME LIKE 'bak\_%') "` ; do mysqldump -u [u] -p dbname "$f" >> dump_dbname.sql; done
    

提交回复
热议问题