Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude data for certain tables but keep structure with mysqldump?

I'm doing a regular dump of a database that uses the database for logging. I need to create a mysqldump command that dumps everything from the database but excludes the row information for the log tables.

I see the no-data parameter, but that doesn't seem to support selecting only certain tables.

like image 873
mattalxndr Avatar asked Jan 28 '11 19:01

mattalxndr


People also ask

How will you take the table and schema structure only by Mysqldump?

Use the --no-data switch with mysqldump to tell it not to dump the data, only the table structure. This will output the CREATE TABLE statement for the tables. To target specific tables, enter them after the database name. Save this answer.

Can Mysqldump lock tables?

By default, mysqldump locks all the tables it's about to dump. This ensure the data is in a consistent state during the dump.

What is single transaction in Mysqldump?

The --single-transaction flag will start a transaction before running. Rather than lock the entire database, this will let mysqldump read the database in the current state at the time of the transaction, making for a consistent data dump.

Does Mysqldump include indexes?

No, it does not export indexes. Indexes are rebuilt upon loading the mysqldump back into mysql.


2 Answers

you can combine with shell script to help better

#/bin/bash

# dump all except for table log
tables=$(mysql -N <<< "show tables from your_db" | grep -Ev "^log$" | xargs); 
mysqldump your_db $tables > backup.sql

# dump structure for table log
mysqldump -d your_db log >> backup.sql
like image 147
ajreal Avatar answered Oct 06 '22 15:10

ajreal


Run 2 commands. One where you list all tables that you want a full dump of, one where you dump only the table definition

#structure only
mysqldump -d -q mydb table1 table2 table3

#all data too
mysqldump -q mydb table4 table5 table6
like image 21
nos Avatar answered Oct 06 '22 15:10

nos