Database manager script

The scripts assumes the following " 
1) the websites are located at /websites/domain.com 
2) this script edits add and deletes database
 
#!/bin/bash
clear

add_DB ()
{


read -p " Enter your domain name: " d
read -p " Enter your choice of database name : " DB
read -p " Enter your user name: " u
read -p " Enter your paasword : " p

check_DB=$( mysql -u root -pagnello -e "show databases;" |grep "\<${DB}\>")

if [ -d /websites/"$d" ]; then

          if [ -z $check_DB ]; then
mysql -u root -pagnello -e "create database ${DB};"
mysql -u root -pagnello -e "GRANT ALL ON ${DB}.* to '${u}'@'localhost' IDENTIFIED BY '${p}'"

          else
echo -e "########## The database already exist ############"
          fi
else
echo -e "########## The domain does not exist #############"
fi
}

del_DB ()
{

read -p " Enter your domain name: " d
read -p " Enter your  database to be deleted  : " DB

check_DB=$(mysql -u root -pagnello -e 'show databases;' |grep "\<${DB}\>")
check_mysql_db=$(mysql -u root -pagnello -e 'use mysql;' -e "select  Db,User from db where Db='$DB';" |grep $DB |awk '{print $1}')
check_mysql_user=$(mysql -u root -pagnello -e 'use mysql;' -e "select  Db,User from db where Db='$DB';" |grep $DB |awk '{print $2}')

if [ -d /websites/"$d" ]; then

          if [ "$check_DB" = "$DB"  ]; then

                if [ "$DB" = "$check_mysql_db" ];then


mysql -u root -pagnello -e "drop database $DB;"
mysql -u root -pagnello -e 'use mysql;' -e "DELETE FROM db WHERE Db='${check_mysql_db}' AND Host='localhost'"
mysql -u root -pagnello -e 'use mysql;' -e "DELETE FROM user WHERE User='${check_mysql_user}' AND Host='localhost'"
                else
echo -e "########## The database does not match in the mysql DB file  ############"
                fi
          else
echo -e "########## The database does not exist #############"
          fi
else
echo -e "############# The domani does not exist ##########"
fi
}

edit_DB ()
{
read -p " Enter your domain name: " d
read -p " Enter your choice of database name : " DB
read -p " Enter your user name: " u
read -p " Enter your paasword : " p

check_DB=$( mysql -u root -pagnello -e "show databases;" |grep "\<${DB}\>")
check_DB=$(mysql -u root -pagnello -e 'show databases;' |grep "\<${DB}\>")
check_mysql_db=$(mysql -u root -pagnello -e 'use mysql;' -e "select  Db,User from db where Db='$DB';" |grep $DB |awk '{print $1}')
check_mysql_user=$(mysql -u root -pagnello -e 'use mysql;' -e "select  Db,User from db where Db='$DB';" |grep $DB |awk '{print $2}')

if [ -d /websites/"$d" ]; then

        if [ "$check_DB" = "$DB"  ]; then

                if [ "$DB" = "$check_mysql_db" ];then

                        if [ "$u" = "$check_mysql_user" ]; then

mysql -u root -pagnello -e 'use mysql;' -e "UPDATE user SET Password=PASSWORD('$p') WHERE user='$check_mysql_user';"
mysql -u root -pagnello -e 'use mysql;' -e "FLUSH PRIVILEGES;"

                        else
echo -e "################ the user that you enter does not exist #################"
                        fi
                else
echo -e "########## The database does not match in the mysql DB file  ############"
                fi
         else
echo -e "########## The database does not exist #############"
          fi
else
echo -e "############# The domani does not exist ##########"
fi
}

read -p "Do you want to add / delete /edit database: " r

case $r in
add ) add_DB
;;
delete ) del_DB
;;
edit ) edit_DB
;;
* ) echo -e "Please enter add / delete /edit "
esac
 
 
 --

No comments:

Other Articles

Enter your email address: