Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #!/bin/bash
  2. #title :Export - MySQL to .tsv
  3. #description :This script will generate a .tsv file for each table from MySQL database.
  4. #author :Weryques S. Silva
  5. #date :01/09/2017
  6. #version :1.0
  7.  
  8. user='user'
  9. password='password'
  10. database='database-name'
  11. directory='/path/to/anywhere/'
  12. encoding='utf8mb4'
  13. #host='localhost'
  14. #port=3306
  15.  
  16. mysqldump --single-transaction -u $user -p$password -T $directory --fields-enclosed-by='"' --default-character-set=$encoding $database
  17.  
  18. rm *.sql
  19.  
  20. tables=$(mysql -u $user -p$password $database -sN -e "SHOW TABLES;")
  21.  
  22. for table in $tables; do
  23.  
  24. mysql -u $user -p$password -e "SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR '\t') FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='$database' and table_name='$table' INTO OUTFILE '$directory$table.tsv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '';"
  25.  
  26. cat $table.txt >> $table.tsv && rm $table.txt
  27.  
  28. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement