mysqldump exclude databases

0
mysqldump doesn’t offer an option to exclude a database from the dump, similar to –ignore-table for tablesBash to the rescue!

This short shell script lets you exclude a list of databases

#!/bin/bash
DATABASES_TO_EXCLUDE=”EXCLUDEDB1 EXCLUDEDB2
EXCLUSION_LIST=”‘information_schema’,’mysql'”
for DB in `echo “$ {DATABASES_TO_EXCLUDE}”`
do
EXCLUSION_LIST=”$ {EXCLUSION_LIST},’$ {DB}'”
done
SQLSTMT=”SELECT schema_name FROM information_schema.schemata”
SQLSTMT=”$ {SQLSTMT} WHERE schema_name NOT IN ($ {EXCLUSION_LIST})”
MYSQLDUMP_DATABASES=”–databases”
for DB in `mysql -hHOST -uUSER -pPASSWORD -ANe”$ {SQLSTMT}”`
do
MYSQLDUMP_DATABASES=”$ {MYSQLDUMP_DATABASES} $ {DB}”
done
MYSQLDUMP_OPTIONS=”–routines –triggers –events -hHOST -uUSER -pPASSWORD
mysqldump $ {MYSQLDUMP_OPTIONS} $ {MYSQLDUMP_DATABASES} > dump.sql

 

You might also like More from author