mysqldump - exclude some table data

73.1k Views Asked by At

Is it possible, using mysql dump to export the entire database structure, but exclude certain tables data from export.

Say the database has 200 tables, I wish to export the structure of all 200 tables, but i want to ignore the data of 5 specific tables.

If this is possible, how is it done?

11

There are 11 best solutions below

5
On BEST ANSWER

As per the mysqldump docs:

mysqldump name_of_db --ignore-table=name_of_db.name_of_table
0
On

Another possibility that I use is to avoid the lines inserting data into the wanted table.

The principle is to filter out the INSERT INTO lines using grep -v

mysqldump name_of_db | grep -v 'INSERT INTO \`name_of_table\` VALUES'

or

mysqldump name_of_db | grep -v 'INSERT INTO \`name_of_db\`.\`name_of_table\` VALUES'

That you can easily get into a gziped file and a separated error file

mysqldump name_of_db | grep -v 'INSERT INTO \`name_of_db\`.\`name_of_table\`' | gzip > /path/dumpfile.sql.gz 2> /path/name_of_db.err

and therefore get a nice backup of what you want and know what failed if any :-)

2
On

In mysqldump from MariaDB in version 10.1 or higher you can use --ignore-table-data:

mysqldump --ignore-table-data="db_name.table" db_name > export.sql

For multiple tables repeat the --ignore-table-data option:

mysqldump --ignore-table-data="db_name.table_1" --ignore-table-data="db_name.table_2" db_name > export.sql

From MariaDB mysqldump docs:

--ignore-table-data=name

Do not dump the specified table data (only the structure). To specify more than one table to ignore, use the directive multiple times, once for each table. Each table must be specified with both database and table names. From MariaDB 10.1.46, MariaDB 10.2.33, MariaDB 10.3.24, MariaDB 10.4.14 and MariaDB 10.5.3. See also --no-data.

6
On

This will produce export.sql with structure from all tables and data from all tables excluding table_name

mysqldump --ignore-table=db_name.table_name db_name > export.sql
mysqldump --no-data db_name table_name >> export.sql
11
On

I think that AmitP's solution is great already - to improve it even further, I think it makes sense to create all tables (structure) first and then fill it with data, except the ones "excluded"

mysqldump --no-data db_name > export.sql
mysqldump --no-create-info --ignore-table=db_name.table_name db_name >> export.sql

if you want to exclude more than 1 table, simply use the --ignore-tabledirective more often (in the 2nc command) - see mysqldump help:

--ignore-table=name   Do not dump the specified table. To specify more than one
                      table to ignore, use the directive multiple times, once
                      for each table.  Each table must be specified with both
                      database and table names, e.g.,
                     --ignore-table=database.table
0
On

Previous answers don't fix the issue with the AUTO_INCREMENT when we export the structure and don't show how to export some specific data in tables.

To go further, we must do :

1/ Export the structure

mysqldump --no-data db_name | sed 's/ AUTO_INCREMENT=[0-9]*\b//g' > export-structure.sql

2/ Export only data and ignores some tables

mysqldump --no-create-info --ignore-table=db_name.table_name1 [--ignore-table=db_name.table_name2, ...] db_name >> export-data.sql

3/ Export specific data in one table

mysqldump --no-create-info --tables table_name --where="id not in ('1', '2', ...)" > export-table_name-data.sql

I tried to use the --skip-opt option to reset AUTO_INCREMENT but this also delete the AUTO_INCREMENT definition on the field, the CHARSET and other things

0
On

The below command will export the database structure and ignore the data

mysqldump --no-data --databases -u[db_user] -p[db_password] [schema] > File.sql

Then export the data ignoring the table

mysqldump --ignore-table=[schema.table_name] --databases -u[db_user] -p[db_password] [schema] >> File.sql

2
On

How about remove the inserts after exporting https://gist.github.com/lionslair/b867e95aad4703e9cd1333dbe3f36024

0
On

To further improve on kantholy's answer, adding compression and removing most of the disk writes by not writing uncompressed data:

#!/bin/bash
echo -n "db name:"
read -r db_name
echo -n "username:"
read -r username
echo -n "Exclude data from table:"
read -r exclude_table_data

{
 mysqldump "$db_name" --user="$username" --password --no-tablespaces --no-data \
 && \
 mysqldump "$db_name" --user="$username" --password --no-tablespaces --no-create-info \
 --ignore-table="${db_name}.${exclude_table_data}";
} \
| bzip2 -c9 \
> "${db_name}_$(date +%y%m%d_%H%M).sql.bz2"
1
On

I am a new user, and do not have enough reputation to vote or comment on answers, so I am simply sharing this as an answer.

@kantholy clearly has the best answer.

@AmitP's method dumps all structure and data to a file, and then a drop/create table statement at the end. The resulting file will still require you to import all of your unwanted data before simply destroying it.

@kantholy's method dumps all structure first, and then only data for the table you do not ignore. This means your subsequent import will not have to take the time to import all the data you do not want - especially important if you have very large amounts of data you want to ignore to save time.

To recap, the most efficient answer is:

mysqldump --no-data db_name > export.sql
mysqldump --no-create-info --ignore-table=db_name.table_name1 [--ignore-table=db_name.table_name2, ...] db_name >> export.sql
0
On

In my opinion the best answer is from Steak, the only answer really working on any case.

All the answers suggesting two dumps are wrong, or at least they can work just under certain premises.

As many have pointed above you can have problems with sequences.

But I find more critical that the database can have triggers that validate or process information (suppose a trigger that insert records on table B when inserting on table A) - in this case, the sequence of creating the full schema (including triggers) and then inserting the data will create a different set of results.