dump all mysql tables into separate files automagically?

后端 未结 10 940
北恋
北恋 2020-11-29 20:21

I\'d like to get dumps of each mysql table into separate files. The manual indicates that the syntax for this is

mysqldump [options] db_name [tbl_name ...]
         


        
10条回答
  •  清酒与你
    2020-11-29 20:50

    Here is the corresponding import.

    #!/bin/bash
    
    # import-files-mysql.sh
    # Descr: Import separate SQL files for a specified database.
    # Usage: Run without args for usage info.
    # Author: Will Rubel
    # Notes:
    #  * Script will prompt for password for db access.
    
    [ $# -lt 3 ] && echo "Usage: $(basename $0)    []" && exit 1
    
    DB_host=$1
    DB_user=$2
    DB=$3
    DIR=$4
    
    DIR=$DIR/*
    
    
    echo -n "DB password: "
    read -s DB_pass
    echo
    echo "Importing separate SQL command files for database '$DB' into '$DB'"
    
    file_count=0
    
    
    for f in $DIR
    
    do 
        echo "IMPORTING FILE: $f"
    
        gunzip -c $f | mysql -h $DB_host -u $DB_user -p$DB_pass $DB
    
        (( file_count++ ))
    done
    
    echo "$file_count files importing to database '$DB'"
    

提交回复
热议问题