Created
October 16, 2017 08:32
-
-
Save sylvaincombes/8c9b6925414dc0f893d5b8f246f04657 to your computer and use it in GitHub Desktop.
script to convert MySQL/MariaDB charset and collation on one database, all tables and columns
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
#!/usr/bin/env bash | |
usage=" | |
script to convert MySQL/MariaDB charset and collation on one database, all tables and columns | |
/!\ Use at your own risk, data loss can occur ! | |
USAGE | |
$(basename "$0") [options] [action] db_name db_user db_password | |
The db_password is optional | |
EXAMPLE | |
$(basename "$0") --db-host=127.0.0.1 --charset=utf8 --collation=utf8_unicode_ci my_database root rootpassword | |
ACTION | |
display Display sql only | |
execute Run generated sql | |
OPTIONS | |
--help -h show this help | |
--version -v Display the script version | |
--db-host database host(default: localhost) | |
--charset set the seed value (default: utf8mb4) | |
--collation set the collation to use (default: utf8mb4_unicode_ci) | |
--execute if you pass this argument you indicate that you want to execute the sql (by default this script only display generated sql) | |
LICENSE | |
The MIT License | |
Copyright 2017 Sylvain COMBES | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
" | |
version='0.0.1' | |
host='localhost' | |
password='' | |
charset='utf8mb4' | |
collation='utf8mb4_unicode_ci' | |
execute=false | |
sql='' | |
for i in "$@" | |
do | |
case $i in | |
-h|--help) | |
echo "$usage"; | |
shift | |
exit; | |
;; | |
-v|--version) | |
echo "$version"; | |
shift | |
exit; | |
;; | |
--db-host=*) | |
host="${i#*=}" | |
shift | |
;; | |
--charset=*) | |
charset="${i#*=}" | |
shift | |
;; | |
--collation=*) | |
collation="${i#*=}" | |
shift | |
;; | |
--execute) | |
execute=true | |
echo "SQL will be executed ..." | |
shift | |
;; | |
--default) | |
DEFAULT=YES | |
shift | |
;; | |
# *) | |
# echo "${i#*=}"; | |
# ;; | |
esac | |
done | |
if [ -z "$1" ] | |
then | |
echo "Missing database name as first argument" | |
exit 1 | |
fi | |
name="\`$1\`" | |
if [ -z "$2" ] | |
then | |
echo "Missing database user as second argument" | |
exit 1 | |
fi | |
user="$2" | |
if [ ! -z "$3" ] | |
then | |
password="--password=$3" | |
fi | |
mysql_command="mysql -s --host=$host --user=$user $password" | |
sql="${sql}ALTER DATABASE $name CHARACTER SET $charset COLLATE $collation;\n" | |
sql=${sql}$(echo "USE $name; SHOW TABLES;" | eval "${mysql_command}" | ( | |
while read TABLE; do | |
echo "ALTER TABLE $name.\`$TABLE\` CONVERT TO CHARACTER SET $charset COLLATE $collation;" | |
done | |
)) | |
if [ ${execute} = true ] | |
then | |
read -p "Are you sure? " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo -e "${sql}" | eval "${mysql_command}" | |
echo 'done !' | |
exit 0; | |
else | |
echo "Operation cancelled" | |
exit 1; | |
fi | |
else | |
echo -e "${sql}" | |
exit 0; | |
fi | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment