Posted By

eristoddle on 07/15/11


Tagged


Versions (?)

Shell MySql Tool


 / Published in: ActionScript
 

  1. #!/bin/bash
  2. #
  3. # Version 0.02 - JF Nutbroek 2007
  4. #
  5.  
  6. # Path to MySQL binary
  7. MYSQL="/opt/lampp/bin/mysql"
  8.  
  9. # Code starts here
  10.  
  11. check() {
  12. OUT=$?
  13. if [ ! $OUT -eq 0 ]; then
  14. echo "Error!"
  15. exit 2
  16. fi
  17. }
  18.  
  19. getinfo() {
  20. echo "##### WARNING - Expert only! #####"
  21. echo ""
  22. echo "Please enter your MySQL username [enter=root]"
  23. read USERNAME
  24. if [ "$USERNAME" = "" ]; then
  25. USERNAME="root"
  26. fi
  27. echo "Please enter your MySQL password"
  28. stty -echo
  29. read PASSWORD
  30. stty echo
  31. echo ""
  32. DBNAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost --batch --skip-column-names -e "show databases" | sed 's/ /%/g'`"
  33. PS3='Please select the database:'
  34. select DB in $DBNAMES ; do
  35. break
  36. done
  37. echo ""
  38. }
  39.  
  40. optimize() {
  41. clear
  42. echo "Optimizing database..."
  43. echo ""
  44. getinfo
  45. TABLENAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -D $DB -e "show tables\G;" | grep 'Tables_in_' | sed -n 's/.*Tables_in_.*: \([_0-9A-Za-z]*\).*/\1/p'`"
  46. for TABLENAME in $TABLENAMES
  47. do
  48. $MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -s -D $DB -e "optimize table $TABLENAME;"
  49. done
  50. echo ""
  51. echo "Completed"
  52. }
  53.  
  54. repair() {
  55. clear
  56. echo "Repairing database..."
  57. echo ""
  58. getinfo
  59. TABLENAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -D $DB -e "show tables\G;" | grep 'Tables_in_' | sed -n 's/.*Tables_in_.*: \([_0-9A-Za-z]*\).*/\1/p'`"
  60. for TABLENAME in $TABLENAMES
  61. do
  62. $MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -s -D $DB -e "repair table $TABLENAME;"
  63. done
  64. echo ""
  65. echo "Completed"
  66. }
  67.  
  68. case "$1" in
  69. 'optimize')
  70. setterm -background blue
  71. clear
  72. optimize
  73. setterm -background black
  74. ;;
  75. 'repair')
  76. setterm -background blue
  77. clear
  78. repair
  79. setterm -background black
  80. ;;
  81. *)
  82. echo "Usage: mysqltool optimize | repair"
  83. esac
  84.  
  85. exit 0
  86.  

Report this snippet  

You need to login to post a comment.