Last active
November 14, 2019 17:31
-
-
Save bokwoon95/18ea52c2f8b60228290d4a6da38d47d9 to your computer and use it in GitHub Desktop.
dump an SQL file's query into CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT_PATH=${BASH_SOURCE[0]} | |
SCRIPT_NAME=${SCRIPT_PATH##*/} | |
# SCRIPT_DIR="$(cd "$(dirname "${SCRIPT_PATH:-$PWD}")" 2>/dev/null 1>&2 && pwd)" | |
HELPDOC=$(cat <<HEREDOC | |
Usage: $SCRIPT_NAME <file.sql> [--db <mysql_database_name> --user <username> --pass <password>] | |
Dumps the query from a provided SQL file into a CSV file. The CSV file will | |
be in same place as the SQL file, but with '.csv' appended to the end of it. | |
Examples: | |
$SCRIPT_NAME file.sql | |
$SCRIPT_NAME file.sql --db mydatabase --user username --pass password123 customers.sql | |
Options: | |
--help Show this help | |
--db Database name | |
--user Database username | |
--pass Database password | |
HEREDOC | |
) | |
# Unpack script arguments | |
argc="$#";: "$((i=0))" | |
while [ "$((i))" -lt "$argc" ]; do | |
case "$1" in | |
--help|-h) Help='true';; | |
--user) shift;: $((i=i+1)); User="$1";; | |
--pass) shift;: $((i=i+1)); Pass="$1";; | |
--db) shift;: $((i=i+1)); Db="$1";; | |
*) SqlFile="$1" | |
esac | |
shift;: "$((i=i+1))" | |
done | |
# If --help option specified or no arguments passed, print $HELPDOC and exit | |
if [ "$Help" ] || [ ! "$SqlFile" ]; then | |
echo "$HELPDOC" | |
exit 0 | |
fi | |
# Build SELECT ... INTO OUTFILE ... query | |
if [ -f "$SqlFile" ]; then | |
# Join all lines from SQL file into one and strip trailing ';' (if it exists) | |
SELECT="$(<"$SqlFile" tr -s '\n' ' ' | awk '{$1=$1;print}' | sed 's/;$//g')" | |
[ "$SELECT" ] && SELECT="$SELECT INTO OUTFILE '$PWD/$SqlFile.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n';" | |
fi | |
# Ensure $SELECT is not an empty string before running it in MySQL | |
if [ "$SELECT" ]; then | |
if [ ! "$Db" ]; then read -rp "Enter your MySQL database name: "; Db="$REPLY"; fi | |
if [ ! "$User" ]; then read -rp "Enter your MySQL username: "; User="$REPLY"; fi | |
if [ ! "$Pass" ]; then read -rp "Enter your MySQL password: "; Pass="$REPLY"; fi | |
echo "$SELECT" | tee /dev/tty | mysql -u"$User" -p"$Pass" -D "$Db" | |
dumpsuccess=$? # check if dump was successful | |
fi | |
# MySQL dumps NULL values as '\N', we want to remove these from the CSV file | |
if [ "$dumpsuccess" -eq 0 ]; then | |
echo 'Removing all \N from csv...' | |
[ -f "$PWD/$SqlFile.csv" ] && sed -i.bak 's/\\N//g' "$PWD/$SqlFile.csv" | |
[ -f "$PWD/$SqlFile.csv.bak" ] && rm "$PWD/$SqlFile.csv.bak" | |
[ -f "$PWD/$SqlFile.csv" ] && echo "$PWD/$SqlFile.csv was successfully dumped" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment