Mysqldump only tables with certain prefix / Mysqldump wildcards?

后端 未结 9 2493
一个人的身影
一个人的身影 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
    
    0 讨论(0)
  • 2020-12-07 08:24

    There are already a lot of good answers, but I came here with such variation:

    mysql MY_DATABASE -N -u MY_MYSQLUSER -p -e 'show tables like "%MY_LIKE_CODE%";' |
    xargs mysqldump MY_DATABASE -u MY_MYSQLUSER -p |
    gzip > ~/backup/`date +%Y%m%d:::%H:%M:%S-MY_DAMP.sql.gz`
    

    By this action I made a table dump by the mask like %mask% from the database to a single file. Hopefully someone will find it useful.

    0 讨论(0)
  • 2020-12-07 08:28

    You can specify table names on the command line one after the other, but without wildcards. mysqldump databasename table1 table2 table3

    You can also use --ignore-table if that would be shorter.

    Another idea is to get the tables into a file with something like

    mysql -N information_schema -e "select table_name from tables where table_schema = 'databasename' and table_name like 'bak_%'" > tables.txt 
    

    Edit the file and get all the databases onto one line. Then do

    mysqldump dbname `cat tables.txt` > dump_file.sql
    

    To drop tables in one line (not recommended) you can do the following

    mysql -NB  information_schema -e "select table_name from tables where table_name like 'bak_%'" | xargs -I"{}" mysql dbname -e "DROP TABLE {}"
    
    0 讨论(0)
提交回复
热议问题