Skip to content

Instantly share code, notes, and snippets.

@1000k
Last active August 29, 2015 14:26
Show Gist options
  • Save 1000k/c9c1ef0c013026a6c10d to your computer and use it in GitHub Desktop.
Save 1000k/c9c1ef0c013026a6c10d to your computer and use it in GitHub Desktop.
MySQLのテーブルをExcelに取り込める形式で出力するBashスクリプト ref: http://qiita.com/1000k/items/c8714a12e8c189fd508d
#!/bin/sh
# ***************
# config
# ***************
id=DB_USER_NAME
pw=DB_USER_PASSWORD
db=DB_NAME
tables=("clients" "points" "users")
dir=/tmp/mysql_dump/
# ***************
# exec
# ***************
mkdir -p ${dir}
chmod 777 ${dir}
for table in ${tables[@]}; do
f=${dir}${table}.csv
header=${dir}header
data=${dir}data
echo "${table}..."
rm -f ${f} ${header} ${data}
mysql -u${id} -p${pw} ${db} -e "SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ',') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='${table}' INTO OUTFILE '${header}'"
mysql -u${id} -p${pw} ${db} -e "SELECT * FROM ${table} INTO OUTFILE '${data}' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'"
cat ${data} >> ${header}
mv -f ${header} ${f}
nkf -s --overwrite ${f}
sed -i -e 's/\\N//g' ${f}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment