dump all mysql tables into separate files automagically?

后端 未结 10 939
北恋
北恋 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:41

    I'm not bash master, but I'd just do it with a bash script. Without hitting MySQL, with knowledge of the data directory and database name, you could just scan for all .frm files (one for every table in that db/directory) for a list of tables.

    I'm sure there are ways to make it slicker and accept arguments or whatnot, but this worked well for me.

    tables_in_a_db_to_sql.sh

    #!/bin/bash
    
    database="this_is_my_database"
    datadir="/var/lib/mysql/"
    datadir_escaped="\/var\/lib\/mysql\/"
    
    all_tables=($(ls $datadir$database/*.frm | sed s/"$datadir_escaped$database\/"/""/g | sed s/.frm//g))
    
    for t in "${all_tables[@]}"; do
            outfile=$database.$t.sql
            echo "-- backing up $t to $outfile"
            echo "mysqldump [options] $database $t > $outfile"
            # mysqldump [options] $database $t > $outfile
    done
    

    Fill in the [options] and desired outfile convention as you need, and uncomment the last mysqldump line.

提交回复
热议问题