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
As per the mysqldump docs:
mysqldump name_of_db --ignore-table=name_of_db.name_of_table
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.
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 :-)
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-table
directive 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
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
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"