Skip certain tables with mysqldump
You can use the --ignore-table option. So you could do
mysqldump -u USERNAME -pPASSWORD DATABASE --ignore-table=DATABASE.table1 > database.sql
There is no whitespace after -p
(this is not a typo).
To ignore multiple tables, use this option multiple times, this is documented to work since at least version 5.0.
If you want an alternative way to ignore multiple tables you can use a script like this:
#!/bin/bash
PASSWORD=XXXXXX
HOST=XXXXXX
USER=XXXXXX
DATABASE=databasename
DB_FILE=dump.sql
EXCLUDED_TABLES=(
table1
table2
table3
table4
tableN
)
IGNORED_TABLES_STRING=''
for TABLE in "${EXCLUDED_TABLES[@]}"
do :
IGNORED_TABLES_STRING+=" --ignore-table=${DATABASE}.${TABLE}"
done
echo "Dump structure"
mysqldump --host=${HOST} --user=${USER} --password=${PASSWORD} --single-transaction --no-data --routines ${DATABASE} > ${DB_FILE}
echo "Dump content"
mysqldump --host=${HOST} --user=${USER} --password=${PASSWORD} ${DATABASE} --no-create-info --skip-triggers ${IGNORED_TABLES_STRING} >> ${DB_FILE}
Building on the answer from @Brian-Fisher and answering the comments of some of the people on this post, I have a bunch of huge (and unnecessary) tables in my database so I wanted to skip their contents when copying, but keep the structure:
mysqldump -h <host> -u <username> -p <schema> --no-data > db-structure.sql
mysqldump -h <host> -u <username> -p <schema> --no-create-info --ignore-table=schema.table1 --ignore-table=schema.table2 > db-data.sql
The resulting two files are structurally sound but the dumped data is now ~500MB rather than 9GB, much better for me. I can now import these two files into another database for testing purposes without having to worry about manipulating 9GB of data or running out of disk space.
for multiple databases:
mysqldump -u user -p --ignore-table=db1.tbl1 --ignore-table=db2.tbl1 --databases db1 db2 ..
Another example for ignoring multiple tables
/usr/bin/mysqldump -uUSER -pPASS --ignore-table={db_test.test1,db_test.test3} db_test> db_test.sql
using --ignore-table
and create an array of tables, with syntaxs like
--ignore-table={db_test.table1,db_test.table3,db_test.table4}
Extra:
Import database
# if file is .sql
mysql -uUSER -pPASS db_test < backup_database.sql
# if file is .sql.gz
gzip -dc < backup_database.sql.gz | mysql -uUSER -pPASSWORD db_test
Simple script to ignore tables and export in .sql.gz to save space
#!/bin/bash
#tables to ignore
_TIGNORE=(
my_database.table1
my_database.table2
my_database.tablex
)
#create text for ignore tables
_TDELIMITED="$(IFS=" "; echo "${_TIGNORE[*]/#/--ignore-table=}")"
#don't forget to include user and password
/usr/bin/mysqldump -uUSER -pPASSWORD --events ${_TDELIMITED} --databases my_database | gzip -v > backup_database.sql.gz
Links with information that will help you
- mysqldump & gzip
- mysqldump to a tar.gz
- How do I load a sql.gz file to my database?
Note: tested in ubuntu server with mysql Ver 14.14 Distrib 5.5.55
To exclude some table data, but not the table structure. Here is how I do it:
Dump the database structure of all tables, without any data:
mysqldump -u user -p --no-data database > database_structure.sql
Then dump the database with data, except the excluded tables, and do not dump the structure:
mysqldump -u user -p --no-create-info \
--ignore-table=database.table1 \
--ignore-table=database.table2 database > database_data.sql
Then, to load it into a new database:
mysql -u user -p newdatabase < database_structure.sql
mysql -u user -p newdatabase < database_data.sql