How can I use mysqldump
to dump certain tables that start with common prefix?
15
27
Hehe, this is kind of a hack, but it works (using bash):
mysqldump -u USER -p DATABASE $(mysql -u USER -p -D DATABASE -Bse "show tables like 'PREFIX%'") > /tmp/DATABASE.out
Change the ALLCAPS words as needed.
-
-
this other answer is a dupe, but it goes into a bit more depth, I think. stackoverflow.com/a/5269543/8047 – Dan Rosenstark Jul 21 '12 at 21:57
-
@unutbu I am getting the following error when I try implementing this: "mysqldump: unknown option '-D'". Any thoughts? When I run just the mysql portion within the parenthesis it outputs the tables I want to dump - but then it throws the error when I try and use it within the mysqldump statement. – dayne Jul 1 '13 at 20:00
-
@dayne: The command I posted uses a bash-ism:
$(...)
to run a subcommand in a subprocess. The error message you received looks likemysqldump
was sent all the arguments -- even up to-D
which might be because the$(...)
was not recognized as indicating a subcommand. Perhaps you are not usingbash
? If that is the case, you'll need to translate the$(...)
into the right syntax for your shell. For example, you might need to replace$(...)
with backticks. – unutbu Jul 1 '13 at 20:07 -
@unutbu Thank you so much for your response. I just read that CMD does not support sub commands - so I will have to figure out something else. – dayne Jul 1 '13 at 20:37
0
You can create backup.sh script:
BACKUP_DIR="/path/to/backups"
DB_HOST="domain.com"
DB_USER="user"
DB_PASS="pass"
PREFIX="phpbb"
TMP_LIST = mysql --host=$DB_HOST --user=$DB_USER --password=$DB_PASS -e "show databases;" | grep $PREFIX
# just get to database name column (may be $1, don't remember)
TMP_LIST = cat $TMP_LIST | awk '{print $2}'
# Getting dbs
mkdir $BACKUP_DIR/tmp
for db in $TMP_LIST ; do
mysqldump --host=$DB_HOST --user=$DB_USER --password=$DB_PASS --opt $db > $BACKUP_DIR/tmp/$db.sql
zip -mj $BACKUP_DIR/tmp/$db.sql.zip $BACKUP_DIR/tmp/$db.sql 2>&1
done
Hope it help.