Mysqldump more than one table?

前端 未结 2 746
萌比男神i
萌比男神i 2021-02-01 23:03

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

相关标签:
2条回答
  • 2021-02-01 23:57

    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.

    0 讨论(0)
  • 2021-02-02 00:01

    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.

    0 讨论(0)
提交回复
热议问题