freedombone-repair-database 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Checks and repairs a given database
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  32. export TEXTDOMAIN=${PROJECT_NAME}-repair-databases
  33. export TEXTDOMAINDIR="/usr/share/locale"
  34. # The database to be repaired
  35. DATABASE=$1
  36. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  37. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  38. # Frequency - daily/weekly
  39. BACKUP_TYPE='daily'
  40. MYSQL_ROOT_PASSWORD=$(cat /root/dbpass)
  41. TEMPFILE=/root/repair-database-$DATABASE
  42. umask 0077
  43. if [ $2 ]; then
  44. BACKUP_TYPE=$2
  45. fi
  46. # check the database
  47. mysqlcheck -c -u root --password="$MYSQL_ROOT_PASSWORD" $DATABASE > $TEMPFILE
  48. # Attempt to repair the database if it contains errors
  49. if grep -q "Error" "$TEMPFILE"; then
  50. mysqlcheck -u root --password="$MYSQL_ROOT_PASSWORD" --auto-repair $DATABASE
  51. else
  52. # No errors were found, so exit
  53. rm -f $TEMPFILE
  54. exit 0
  55. fi
  56. rm -f $TEMPFILE
  57. # Check the database again
  58. mysqlcheck -c -u root --password="$MYSQL_ROOT_PASSWORD" $DATABASE > $TEMPFILE
  59. # If it still contains errors then restore from backup
  60. if grep -q "Error" "$TEMPFILE"; then
  61. mysql -u root --password="$MYSQL_ROOT_PASSWORD" $DATABASE -o < /var/backups/${DATABASE}_${BACKUP_TYPE}.sql
  62. # Send a warning email
  63. echo $"$DATABASE database corruption could not be repaired. Restored from backup." | mail -s $"${PROJECT_NAME} database maintenance" $ADMIN_EMAIL_ADDRESS
  64. rm -f $TEMPFILE
  65. exit 1
  66. fi
  67. rm -f $TEMPFILE
  68. exit 0