Created
October 10, 2013 00:24
-
-
Save ejhayes/6910962 to your computer and use it in GitHub Desktop.
Dealing with double utf8 encoded mysql databases.
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 | |
set -e | |
OPTIND=1 # Reset in case getopts has been used previously in the shell. | |
function warn { | |
# Yellow'ish | |
echo -e "\033[1;33m$1\033[0m" 1>&2 | |
} | |
function info { | |
# Blue'ish | |
echo -e "\033[1;36m$1\033[0m" 1>&2 | |
} | |
function error { | |
# Red'ish | |
echo -e "\033[1;35m$1\033[0m" 1>&2 | |
} | |
function about { | |
info "Removes double UTF-8 encoding issue for MySQL databases.\n" | |
info "Usage: $0 -d [database] -u [user] -p [password]" | |
} | |
function usage { | |
about | |
exit 0 | |
} | |
function fail { | |
error "[ERROR] $1" | |
usage | |
exit 1 | |
} | |
# Defaults | |
USER="root" | |
PASSWORD="" | |
VERBOSE=0 | |
DATABASE="" | |
options=':h?:v:u:d:p:' | |
while getopts $options option; do | |
case $option in | |
h|\?) | |
usage | |
;; | |
v) VERBOSE=1 | |
;; | |
u) USER=$OPTARG | |
;; | |
d) DATABASE=$OPTARG | |
;; | |
p) PASSWORD=$OPTARG | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
[ "$1" = "--" ] && shift | |
# Must specify a DB | |
if [[ $DATABASE == "" ]]; then | |
fail "database not set" | |
fi | |
# Password to use | |
if [[ $PASSWORD == "" ]]; then | |
LOGIN_PARAMETER="" | |
else | |
LOGIN_PARAMETER="-p$PASSWORD" | |
fi | |
# Perform the double encoding fix | |
mysqldump -u $USER $LOGIN_PARAMETER \ | |
--add-drop-table \ | |
--opt \ | |
--quote-names \ | |
--skip-set-charset \ | |
--default-character-set=latin1 \ | |
$DATABASE > out.sql \ | |
&& mysql -u $USER $LOGIN_PARAMETER \ | |
--default-character-set=utf8 \ | |
$DATABASE < out.sql \ | |
&& rm out.sql | |
# Did we succeed? | |
if [ "$?" -eq "0" ]; then | |
info "Done!" | |
else | |
error "Failed..." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment