Dump a mysql database to a plaintext (CSV) backup from the command line

前端 未结 10 1057
广开言路
广开言路 2020-11-28 19:26

I\'d like to avoid mysqldump since that outputs in a form that is only convenient for mysql to read. CSV seems more universal (one file per table is fine). But if there ar

10条回答
  •  臣服心动
    2020-11-28 19:35

    If you want to dump the entire db as csv

    #!/bin/bash
    
    host=hostname
    uname=username
    pass=password
    
    port=portnr
    db=db_name
    s3_url=s3://bxb2-anl-analyzed-pue2/bxb_ump/db_dump/
    
    
    
    DATE=`date +%Y%m%d`
    rm -rf $DATE
    
    echo 'show tables' | mysql -B -h${host} -u${uname} -p${pass} -P${port} ${db} > tables.txt
    awk 'NR>1' tables.txt > tables_new.txt
    
    while IFS= read -r line
    do
      mkdir -p $DATE/$line
      echo "select * from $line" | mysql -B -h"${host}" -u"${uname}" -p"${pass}" -P"${port}" "${db}" > $DATE/$line/dump.tsv
    done < tables_new.txt
    
    touch $DATE/$DATE.fin
    
    
    rm -rf tables_new.txt tables.txt
    

提交回复
热议问题