Mysqldump only tables with certain prefix / Mysqldump wildcards?

后端 未结 9 2492
一个人的身影
一个人的身影 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:01

    Here is an easy way:

    mysql [dbname] -u [username] -p[password] -N -e 'show tables like "bak\_%"' | xargs mysqldump [dbname] -u [username] -p[password] > [dump_file]
    
    0 讨论(0)
  • 2020-12-07 08:03

    As of MySQL 5.7, the mysqlpump tool supports table name filtering with patterns.

    Note that it's a half-baked tool, so you need to make sure it supports the required functionalities, and that it performs them correctly (eg. as of MySQL 5.7.12, the triggers export is broken).

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

    Building on some of the other nice answers here, I created shell script to make this even easier. This script generates 3 files in the output - one with the structure for all tables, one with the data for all non-excluded tables, and one with the data for all "excluded" tables (you could comment this out if you really don't need it). Then you can use which one(s) you need.

    #!/bin/bash
    
    echo -n "DB Password: "
    read -s PASSWORD
    
    HOST=yourhostname.com
    USER=youruser
    DATABASE=yourdatabase
    
    MAIN_TABLES=$(mysql -h $HOST -u $USER -p$PASSWORD -D $DATABASE -Bse "SHOW TABLES WHERE Tables_in_dashboard NOT LIKE 'bigtable_%';")
    STATS_TABLES=$(mysql -h $HOST -u $USER -p$PASSWORD -D $DATABASE -Bse "SHOW TABLES LIKE 'bigtable_%';")
    
    echo "Dumping structure..."
    mysqldump -h $HOST -u $USER -p$PASSWORD $DATABASE --no-data | gzip > structure.sql.gz
    
    echo "Dumping main data..."
    mysqldump -h $HOST -u $USER -p$PASSWORD $DATABASE --no-create-info $MAIN_TABLES | gzip > data.sql.gz
    
    echo "Dumping big table data..."
    mysqldump -h $HOST -u $USER -p$PASSWORD $DATABASE --no-create-info $STATS_TABLES | gzip > big_table_data.sql.gz
    
    0 讨论(0)
  • 2020-12-07 08:10

    My solution:

    mysqldump -u username -p mydatabase `mysql -B --disable-column-names -u username -p mydatabase -e "SHOW TABLES LIKE 'bak\_%'" | sed ':a;N;$!ba;s/\n/ /g'`
    
    0 讨论(0)
  • 2020-12-07 08:12

    mysql DATABASE -u USERNAME -p -e 'show tables like "PREFIX%"' | grep -v Tables_in | xargs mysqldump DATABASE -u USERNAME -p > DUMP.sql

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

    My favorite:

    mysqldump DBNAME $(mysql -D DBNAME -Bse "show tables like 'wp\_%'") > FILENAME.sql
    

    All the answers take nearly the same approach, but this is the most concise syntax.

    0 讨论(0)
提交回复
热议问题