Guest User

Untitled

a guest
Oct 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. usage="
  4.  
  5. script to convert MySQL/MariaDB charset and collation on one database, all tables and columns
  6.  
  7. /!\ Use at your own risk, data loss can occur !
  8.  
  9. USAGE
  10. $(basename "$0") [options] [action] db_name db_user db_password
  11.  
  12. The db_password is optional
  13.  
  14. EXAMPLE
  15. $(basename "$0") --db-host=127.0.0.1 --charset=utf8 --collation=utf8_unicode_ci my_database root rootpassword
  16.  
  17. ACTION
  18. display Display sql only
  19. execute Run generated sql
  20.  
  21. OPTIONS
  22. --help -h show this help
  23. --version -v Display the script version
  24. --db-host database host(default: localhost)
  25. --charset set the seed value (default: utf8mb4)
  26. --collation set the collation to use (default: utf8mb4_unicode_ci)
  27. --execute if you pass this argument you indicate that you want to execute the sql (by default this script only display generated sql)
  28.  
  29. LICENSE
  30.  
  31. The MIT License
  32.  
  33. Copyright 2017 Sylvain COMBES
  34.  
  35. 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:
  36.  
  37. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  38.  
  39. 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.
  40. "
  41.  
  42. version='0.0.1'
  43. host='localhost'
  44. password=''
  45. charset='utf8mb4'
  46. collation='utf8mb4_unicode_ci'
  47. execute=false
  48. sql=''
  49.  
  50. for i in "$@"
  51. do
  52. case $i in
  53. -h|--help)
  54. echo "$usage";
  55. shift
  56. exit;
  57. ;;
  58. -v|--version)
  59. echo "$version";
  60. shift
  61. exit;
  62. ;;
  63. --db-host=*)
  64. host="${i#*=}"
  65. shift
  66. ;;
  67. --charset=*)
  68. charset="${i#*=}"
  69. shift
  70. ;;
  71. --collation=*)
  72. collation="${i#*=}"
  73. shift
  74. ;;
  75. --execute)
  76. execute=true
  77. echo "SQL will be executed ..."
  78. shift
  79. ;;
  80. --default)
  81. DEFAULT=YES
  82. shift
  83. ;;
  84. # *)
  85. # echo "${i#*=}";
  86. # ;;
  87. esac
  88. done
  89.  
  90. if [ -z "$1" ]
  91. then
  92. echo "Missing database name as first argument"
  93. exit 1
  94. fi
  95.  
  96. name="\`$1\`"
  97.  
  98. if [ -z "$2" ]
  99. then
  100. echo "Missing database user as second argument"
  101. exit 1
  102. fi
  103.  
  104. user="$2"
  105.  
  106. if [ ! -z "$3" ]
  107. then
  108. password="--password=$3"
  109. fi
  110.  
  111. mysql_command="mysql -s --host=$host --user=$user $password"
  112.  
  113. sql="${sql}ALTER DATABASE $name CHARACTER SET $charset COLLATE $collation;\n"
  114.  
  115. sql=${sql}$(echo "USE $name; SHOW TABLES;" | eval "${mysql_command}" | (
  116. while read TABLE; do
  117. echo "ALTER TABLE $name.\`$TABLE\` CONVERT TO CHARACTER SET $charset COLLATE $collation;"
  118. done
  119. ))
  120.  
  121.  
  122. if [ ${execute} = true ]
  123. then
  124. read -p "Are you sure? " -n 1 -r
  125. echo
  126. if [[ $REPLY =~ ^[Yy]$ ]]
  127. then
  128. echo -e "${sql}" | eval "${mysql_command}"
  129. echo 'done !'
  130. exit 0;
  131. else
  132. echo "Operation cancelled"
  133. exit 1;
  134. fi
  135. else
  136. echo -e "${sql}"
  137. exit 0;
  138. fi
  139.  
  140. exit 0;
Add Comment
Please, Sign In to add comment