Created
May 16, 2013 14:10
-
-
Save ain/5591995 to your computer and use it in GitHub Desktop.
MySQL select query dump to CSV
This file contains 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 | |
usage="Usage: mysql_csv_dump database_name username" | |
err[1]="Invalid command" | |
# Check for database name argument | |
if [ -z "$1" ] | |
then | |
echo ${err[1]} | |
echo "$usage" | |
exit 1 | |
else | |
db_name=$1 | |
fi | |
# Check for username argument | |
if [ -z "$2" ] | |
then | |
echo ${err[1]} | |
echo "$usage" | |
exit 1 | |
else | |
username=$2 | |
fi | |
output="$(date +%Y%m%d%H%M%S).csv" | |
`mysql ${db_name} -u ${username} -p -B -e "[your SQL select here];" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > $output` | |
if [ -f $output ] | |
then | |
echo "Data dump successful!" | |
else | |
echo "Data dump failed!" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment