interactive_restore_database.sh

This script allow to restore a database,under the control of the User.

#!/bin/bash
# This script allows to restore TPMT database dumps
# It is presumed that all previous dumps have been made in the Directory /home/database_backup/
DIRECTORY="/home/database_backup"
FILE_PATH=""
FILE_NAME=""
DATABASE=""
clear
echo
echo
echo
echo ###########################################
echo #       Welcome to Database Restore       #
echo ###########################################
echo

echo "Please enter your database Username"
read USERNAME
while [ $USERNAME = "" ] 
do
        echo "You must enter a Username"
        read USERNAME
done
echo "Please enter your Password"
read PASSWORD
while [ $PASSWORD = "" ] 
do
        echo "You must enter a Password"
        read PASSWORD
done
# Select the database to restore
echo "Select the Database"
echo "1 = TPMT"
echo "2 = THRMT"
echo "3 = MONEY"
echo "4 = SESAME"
echo "5 = YAMIKAWAN"
read CHOICE
while [ $CHOICE = "" ] 
do
        echo "Select the Database"
        echo "1 = TPMT"
        echo "2 = THRMT"
        echo "3 = MONEY"
        echo "4 = SESAME"
        echo "5 = YAMIKAWAN"
        echo "6 = SHOPPING"
        echo "7 = wordpress"
        read CHOICE
done
# test the choice selected
if [ $CHOICE = 1 ]
then
        DATABASE="TPMT";
fi
if [ $CHOICE = 2 ]
then
        DATABASE="THRMT";
fi
if [ $CHOICE = 3 ]
then
        DATABASE="MONEY";
fi
if [  $CHOICE  =  4 ]
then
        DATABASE="SESAME";
fi
if [  $CHOICE  =  5 ]
then
        DATABASE="YAMIKAWAN";
fi
if [  $CHOICE  =  6 ]
then
        DATABASE="SHOPPING";
fi
if [  $CHOICE  =  7 ]
then
        DATABASE="wordpress";
fi


# list the backup file available in /home directory
echo "##################### Listing TPMT Backup files #####################"
ls -ll $DIRECTORY | grep $DATABASE
echo "#####################################################################"
echo "Select the file you want to restore from the list above"
read FILE_NAME
while [ $FILE_NAME = "" ] 
do
        echo "You must select a file in the list above!"
        read FILE_NAME
done
echo "You entered:" $FILE_NAME
echo "Do you confirm (yes/no)?"
read ANSWER
#echo "You answered:" $ANSWER
while [ "$ANSWER" = "" ]
do
        echo "You need to answer the question by yes or no!"
        echo "yes/no?:"
        read ANSWER
done
#echo "Your answer was:" $ANSWER
if [ "$ANSWER" = "no" ]
then
	echo
        echo "Thank you for wasting my time!"
	exit
fi
# answer was 'yes'
FILE_PATH="$DIRECTORY$FILE_NAME"
echo "Looking for file "$FILE_PATH
# check that the file effectively exists
echo "Checking if the file exists..."
if [ -f $FILE_PATH ];
then
	echo "The file '$FILE_PATH' exists"
	echo
        echo "Restoring the database!"
        # restore the selected database
        echo "Running command: 'mysql -u$USERNAME -p$PASSWORD $DATABASE < $FILE_PATH'"
        mysql -u$USERNAME -p$PASSWORD $DATABASE < $FILE_PATH
        if [ $? != 0 ];
        then
                echo "Return result: "$?
                echo "There was a problem! Database has not been restored."
                exit
        fi
else
        echo "The file you selected does not exist!"
        echo "Exiting now! Good bye!"
        echo
        exit
fi
echo
echo "Database restored successfully!"
exit