freedomboneeee/src/freedombone-repair-database

96 lines
2.9 KiB
Plaintext
Raw Normal View History

2015-12-11 16:06:52 +01:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2015-12-11 16:06:52 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2015-12-11 16:06:52 +01:00
#
# Checks and repairs a given database
2018-04-08 14:30:21 +02:00
#
2015-12-11 16:06:52 +01:00
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
2015-12-11 16:06:52 +01:00
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero General Public License as published by
2015-12-11 16:06:52 +01:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2015-12-11 16:06:52 +01:00
#
2016-02-13 23:09:27 +01:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-12-11 16:06:52 +01:00
PROJECT_NAME='freedombone'
2018-03-02 20:17:02 +01:00
COMPLETION_FILE="$HOME/${PROJECT_NAME}-completed.txt"
CONFIG_FILE="$HOME/${PROJECT_NAME}.cfg"
2015-12-11 16:06:52 +01:00
export TEXTDOMAIN=${PROJECT_NAME}-repair-databases
export TEXTDOMAINDIR="/usr/share/locale"
# The database to be repaired
2018-03-02 20:17:02 +01:00
DATABASE="$1"
2015-12-11 16:06:52 +01:00
2018-03-02 20:17:02 +01:00
ADMIN_USERNAME=$(grep "Admin user" "$COMPLETION_FILE" | awk -F ':' '{print $2}')
2015-12-11 16:06:52 +01:00
ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
# Frequency - daily/weekly
BACKUP_TYPE='daily'
# migrate from database password file to using the password store
DATABASE_PASSWORD_FILE=/root/dbpass
if [ -f $DATABASE_PASSWORD_FILE ]; then
MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
stored_password=$(${PROJECT_NAME}-pass -u root -a mariadb)
if [[ "$stored_password" == "$MARIADB_PASSWORD" ]]; then
rm $DATABASE_PASSWORD_FILE
fi
fi
MYSQL_ROOT_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
2015-12-11 16:23:22 +01:00
TEMPFILE=/root/repair-database-$DATABASE
2015-12-11 16:06:52 +01:00
umask 0077
2018-03-02 20:17:02 +01:00
if [ "$2" ]; then
BACKUP_TYPE="$2"
2015-12-11 16:06:52 +01:00
fi
# check the database
2018-03-02 20:17:02 +01:00
mysqlcheck -c -u root --password="$MYSQL_ROOT_PASSWORD" "$DATABASE" > "$TEMPFILE"
2015-12-11 16:06:52 +01:00
# Attempt to repair the database if it contains errors
if grep -q "Error" "$TEMPFILE"; then
2018-03-02 20:17:02 +01:00
mysqlcheck -u root --password="$MYSQL_ROOT_PASSWORD" --auto-repair "$DATABASE"
2015-12-11 16:06:52 +01:00
else
# No errors were found, so exit
2018-03-02 20:17:02 +01:00
rm -f "$TEMPFILE"
2015-12-11 16:06:52 +01:00
exit 0
fi
2018-03-02 20:17:02 +01:00
rm -f "$TEMPFILE"
2015-12-11 16:06:52 +01:00
# Check the database again
2018-03-02 20:17:02 +01:00
mysqlcheck -c -u root --password="$MYSQL_ROOT_PASSWORD" "$DATABASE" > "$TEMPFILE"
2015-12-11 16:06:52 +01:00
# If it still contains errors then restore from backup
if grep -q "Error" "$TEMPFILE"; then
2018-03-02 20:17:02 +01:00
mysql -u root --password="$MYSQL_ROOT_PASSWORD" "$DATABASE" -o < "/var/backups/${DATABASE}_${BACKUP_TYPE}.sql"
2015-12-11 16:06:52 +01:00
# Send a warning email
2018-03-02 20:17:02 +01:00
echo $"$DATABASE database corruption could not be repaired. Restored from backup." | mail -s $"${PROJECT_NAME} database maintenance" "$ADMIN_EMAIL_ADDRESS"
rm -f "$TEMPFILE"
2015-12-11 16:06:52 +01:00
exit 1
fi
2018-03-02 20:17:02 +01:00
rm -f "$TEMPFILE"
2015-12-11 16:06:52 +01:00
exit 0