views:

122

answers:

2

How can I use mysqldump to dump certain tables that start with common prefix?

A: 

Hi.

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.

mosg
A: 

Hehe, this is kind of a hack, but it works:

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.

unutbu
nice hack, and it work PERFECTLY, thanks...
mabuzer