96

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 things worse, there is a set of 70+ Joomla tables that are not in use at all. These are all prefixed with bak_.

Just deleting these bak_ tables will be easy, but I want to 'bak' them up first (see what I did there?). In my mind I can picture a command like this:

mysqldump -u username -p mydatabase bak_*

But this doesn't work. What would be the best way to do this? Thanks!

EDIT: Yes, I could explicitly list the 70 tables to include, or the ~430 tables to exclude, but I am looking for a better way to do this, if possible.

3
  • answer is select that table by query and pass that query with mysqldump because mysqldump does not support the regex thanks good luck
    – Daric
    Mar 11, 2011 at 16:22
  • 1
    I think the answer to your question is here: stackoverflow.com/questions/2949330/…
    – raghu
    May 19, 2011 at 6:31
  • The answer given by @minaz is clearly better than the answer currently marked as best. Would help to mark that one as better, if you agree of course. Sep 5, 2012 at 10:53

10 Answers 10

130

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 {}"
8
  • 4
    But either way, I will have to list out 70 tables to ignore, or 430 tables to include. This is what I am trying to avoid doing. I should have been more clear in my question, but thanks for the answer. This WOULD work, yes ;)
    – thaddeusmt
    Mar 11, 2011 at 14:42
  • 1
    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_name like 'bak_%'"" > tables.txt Edit the file and get all the databases onto one line. Then do mysqldump dbname `cat tables.txt`
    – sreimer
    Mar 12, 2011 at 20:57
  • I ended up just dropping the bak_ tables, but I still used this to get the list of table. Then I gawked them together with commas to create a big DROP TABLE statement gawk '{print $1"," }' tables.txt > baktables.sql. Thanks!
    – thaddeusmt
    May 3, 2011 at 19:26
  • Any way to do this in a one-liner? ie: without using a temp file as an intermediary?
    – Tom Auger
    Sep 7, 2011 at 20:29
  • 4
    This would do it mysql -NB information_schema -e "select table_name from tables where table_name like 'bak_%'" | xargs -I"{}" mysql test -e "DROP TABLE {}"
    – sreimer
    Sep 8, 2011 at 14:43
72

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]
4
  • 4
    it should be show tables like "<prefix>\_%" because _ is also a wildcard and should be escaped... otherwise you could get a problem with tables that have the same prefix-prefix like bak_ and bak2_ would both match on bak_%
    – reox
    Mar 26, 2012 at 9:59
  • 5
    You can pass -N to mysql and you won't need to run the grep to filter out the Tables_in row. Sep 5, 2012 at 10:34
  • 1
    Worked for me but with mysql -NB.
    – wesamly
    Apr 14, 2015 at 13:09
  • If show tables like "bak\_%" returns nothing, all the database is dumped. Any way to prevent this?
    – Seb33300
    Apr 17, 2019 at 15:20
71

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.

5
  • 11
    for those who are curious, these flags are -B batch -e execute -s silent mode (less output) dev.mysql.com/doc/refman/5.6/en/mysql-command-options.html
    – jsh
    Mar 27, 2015 at 16:56
  • I love this. I have come back here many times to grab this snippet! Jun 7, 2016 at 21:52
  • 1
    Slight gotcha: You may have to fill in username and password parameters, for both the mysqldump AND the mysql commands. And if the mysql call fails, the mysqldump defaults to dumping the entire database, which could be huge, especially in cases where you're inclined to dump only a subset of the data.
    – gwideman
    Jul 20, 2019 at 7:03
  • this is not working mysql -u "$MYSQL_USER_NAME" -h host -p"$MYSQL_PASSWORD" -N -e 'show databases like "auth\_%"' | xargs mysqldump -u "$MYSQL_USER_NAME" -h host -p"$MYSQL_PASSWORD" > test
    – cloudbud
    Jan 21, 2020 at 11:16
  • For multiple wildcards you can use a query like this: SHOW TABLES FROM <yourdbname> WHERE Tables_in_<yourdbname> LIKE '%cms%' OR Tables_in_<yourdbname> LIKE '%role%';
    – Rufat
    Jul 17 at 11:07
5

This work for me

mysqldump -u USER -p DATABASE $(mysql -u USER -p -D DATABASE -Bse "show tables like 'PREFIX%'") > /tmp/DATABASE.sql
3

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.

2

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
2

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).

2
  • 3
    Turns out that "mysqlpump" is not a typo. dev.mysql.com/doc/refman/5.7/en/mysqlpump.html. Yes, the --include-tables abc% option does allow specifying tables to dump using a wildcard. But there seems to be no way to get mysqlpump to exclude views. Yep, pretty half-baked indeed.
    – gwideman
    Jul 20, 2019 at 8:03
  • I've had it randomly segfault at me (mysql 8) so it's probably more alpha than release
    – John
    Feb 28, 2021 at 1:01
1

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

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'`
1
  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.
    – Syscall
    Mar 26, 2021 at 14:46
0
mysql DATABASE -u USERNAME -p -e 'show tables like "PREFIX%"' | grep -v Tables_in | xargs mysqldump DATABASE -u USERNAME -p > DUMP.sql
1
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.
    – Syscall
    Mar 26, 2021 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.