SQLite optimization tool

SQLite database optimization tool. Initially created to speed up firefox but can be used on every database.
You can download it here.

  1. #!/bin/sh
  2. #
  3. # Description: Clean firefox (or anyother) SQLite database file
  4. # Auhtor: Alexandre Joseph <http://alexandrejoseph.com/>
  5. # Created: October 31, 2009
  6. # Modified: October 31, 2009
  7. # License: GPL - http://www.gnu.org/copyleft/gpl.html
  8. #
  9.  
  10. DB_DIR=~/.mozilla/firefox/*.default
  11. # Process name to kill
  12. KILL_PROCESS="firefox"
  13. # Ask before killing process
  14. ASK_KILL=1
  15. # Number of time to do the vaccum
  16. REPEAT=1
  17.  
  18. usage()
  19. {
  20. echo "usage: $(basename $0) [options]"
  21. echo -e "\nOptions:"
  22. echo -e "\t-h\t Print this help and exit"
  23. echo -e "\t-y\t Kill the running processes without asking"
  24. echo -e "\t-p PROCESS_NAME\t Name of the process to kill before clean"
  25. echo -e "\t-d DB_DIRECTORY\t Directory path containing the databases"
  26. echo -e "\t-n REPEAT_NB\t Number of time to repeat the vaccum"
  27. exit 0
  28. }
  29.  
  30. clean_db()
  31. {
  32. for DB in $(ls $DB_DIR/*.sqlite); do
  33. echo -n "Cleaning $(basename $DB)... "
  34. sqlite3 $DB "VACUUM" &> /dev/null
  35. [ $? -eq 0 ] && echo "Done" || echo "Fail"
  36. done
  37. }
  38.  
  39. ARGS=$(getopt hyp:d:n: $*)
  40. set -- $ARGS
  41. for OPT in $ARGS; do
  42. case "$OPT" in
  43. "-h")
  44. help
  45. ;;
  46. "-y") shift
  47. ASK_KILL=0
  48. ;;
  49. "-p") shift
  50. KILL_PROCESS=$1; shift
  51. ;;
  52. "-n") shift
  53. [ ${1//[^[:digit:]]} = $1 ] && REPEAT=$1
  54. ;;
  55. "-d") shift
  56. [ -d $1 ] && DB_DIR=$1 || { echo "Directory '$1' does not exists"; exit 1; }
  57. ;;
  58. "--") shift
  59. break
  60. ;;
  61. esac
  62. done
  63.  
  64. # Kill the process
  65. if [ "x$(ps x | grep $KILL_PROCESS | grep -v grep)" != "x" ]; then
  66. if [ $ASK_KILL -eq 1 ]; then
  67. read -p "The process '$KILL_PROCESS' is running, do you want to kill it ? [Y/n] " KILL_IT
  68. [ "$KILL_IT" = "n" -o "$KILL_IT" = "N" ] && { echo "Aborting clean up"; exit 0; }
  69. fi
  70.  
  71. killall $KILL_PROCESS &> /dev/null
  72. fi
  73.  
  74. # Clean the database
  75. for ITER in $(seq 1 $REPEAT); do
  76. clean_db
  77. done
  78.  
  79.