Mysql – thesqldump entire structure but only data from selected tables in a single command

MySQLmysqldump

My database has 3 tables: table1, table2 and table3

I would like to do a mysqldump on this database with the following conditions:

  • Dump structure for all tables
  • Only dump data for table1 and table2, ignore data in table3

Currently, I do this with 2 mysqldump statements

mysqldump -u user -p -d db > db_structure.sql
mysqldump -u user -p db --ignore-table=db.table3 > table1_and_table2_data.sql

Import them in the same order they were dumped (structure, then data from table1 and table2)

Is there a way to combine this into a single mysqldump command?

Best Answer

You can't combine them in one command but you can execute both commands at the same time and output to the same file.

mysqldump -u user -p --no-data db > structure.sql; mysqldump -u user -p db table1 table2 >> structure.sql

to avoid having to enter the password twice you can do -ppassword (note the lack of space!). Also use --no-data in the first command or you end up with the data as well. -d isn't needed when you're doing just one database.