auto_restore_database.sh

This script allows to restore databases automatically (MONEY, SEMAME, SHOPPING, YAMIKAWAN)

  GNU nano 3.2                                                          auto_restore_databases.sh                                                                    

#!/bin/bash -e
# This script allows to restore TPMT database dumps automatically, every day 
# It is assumed that all previous dumps have been synchronized in the Directory /home/database_backup/ from PiDatabase machine
# This script must be installed as a Cron task, run every morning (the database backup occurs every day at 23:55, on PiDatabase machine)
DIRECTORY="/mnt/databases_backup/" # where the backups from PiDatabase are mounted
#BROKER='bubuoat.mine.nu'
#TOPIC='rsync'
FILE_NAME=""
USERNAME='root'
PASSWORD='15sotong15'
LOG_FILE='/home/pi/auto_restore.log'
DATABASELIST=( MONEY SESAME SHOPPING WATCHES YAMIKAWAN )

echo "################################################" >> $LOG_FILE
echo "Databases restore started " >> $LOG_FILE
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
echo "" & date >> $LOG_FILE

for DATABASE in "${DATABASELIST[@]}"
do
        FILE_NAME=$(ls "$DIRECTORY$DATABASE"* -t | head -1)
        echo "File Name: $FILE_NAME" >> $LOG_FILE
        mysql -u$USERNAME -p$PASSWORD $DATABASE < $FILE_NAME
        if [ $? -eq 0 ]; then
                echo "Success at restoring $FILE_NAME into $DATABASE" >> $LOG_FILE
        else
                echo "There was a problem while restoring the database into $DATABASE" >> $LOG_FILE
                curl -s \
                  --form-string "token=aeuca87bjjuqknkwswm9bpvy1ygngo" \
                  --form-string "user=u6ysovfgq1nhysszxzh91qnwadch2y" \
                  --form-string "title=Database restoration"\
                  --form-string "message=Problem while restoring database: $DATABASE" \
                  https://api.pushover.net/1/messages.json
        fi
done
echo "################################################" >> $LOG_FILE
exit