Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

How can I dump a specific table or set of tables without including the rest of the db tables?

share|improve this question
6  
May i suggest "man mysqldump"? The answer could have between found in less time than it took to write this post. Rolando politely covered the answer, however. – Aaron Brown Dec 17 '11 at 3:58

4 Answers

up vote 27 down vote accepted

If you are dumping tables t1, t2, and t3 from mydb

mysqldump -u... -p... mydb t1 t2 t3 > mydb_tables.sql

If you have a ton of tables in mydb and you want to dump everything except t1, t2, and t3, do this:

DBTODUMP=mydb
TBLIST=`mysql -u... -p... -AN -e"select group_concat(table_name separator ' ') from information_schema.tables where table_schema='${DBTODUMP}' and table_name not in ('t1','t2','t3')"`
mysqldump -u... -p... ${DBTODUMP} ${TBLIST} > mydb_tables.sql

Give it a Try !!!

share|improve this answer
2  
To exclude just a few tables you can use --ignore-table=Table1 --ignore-table=Table2 --ignore-table=Table3 etc. – codewaggle Dec 13 '12 at 13:06
@codewaggle You are right. You should submit that as an answer. – RolandoMySQLDBA Dec 13 '12 at 15:09

When you have more than a few tables it is much better running something like this:

mysql databasename -u [user] -p[password] -e 'show tables like "table_name_%"' | grep -v Tables_in | xargs mysqldump [databasename] -u [root] -p [password] > [target_file]

or somethink like this:

mysqldump -u [user] -p[password] databasename `echo "show tables like 'table_name_%';" | mysql -u[user] -p[password] databasename | sed '/Tables_in/d'` > [target_file]
share|improve this answer

A note to expand on the answer by RolandoMySQLDBA.

The script he included is a great approach for including (and table_name in) or excluding (and table_name NOT in) a list of tables.

If you just need to exclude one or two tables, you can exclude them individually with the --ignore-table option:

mysqldump -u -p etc. --ignore-table=Table1 --ignore-table=Table2 > dump_file.sql
share|improve this answer
+1 for your answer !!! – RolandoMySQLDBA Dec 15 '12 at 18:34

You can try some GUI clients like SQLyog. Where you can select the tables which you want to dump.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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