freedombone-repair-database 3.0KB

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