mysql: rewrite of my-add.sh

This commit is contained in:
Victor LABORIE 2017-01-05 11:27:36 +01:00
parent d1d4961aa9
commit 64e11d789a

View file

@ -4,19 +4,44 @@ usage() {
echo "Usage: $0 [ -d <database> -u <user> [-p <password>] [-f] ]"
}
if [ $# = 0 ]; then
is_interactive="true"
interactive() {
echo "Add an account / database in MySQL"
echo "Enter the name of the new database"
read db
if ( is_db $db ); then
echo "Database $db already exist !" >&2
exit 1
fi
echo "Enter account with all right on this new database"
echo "(you can use existant account)"
read user
else
if ( is_user $user ); then
echo "Warning, account already exists, update password ? [N/y]"
read confirm
if [ "${confirm}" = "y" ] || [ "${confirm}" = "Y" ]; then
echo -n "Enter new password for existant MySQL account (empty for random): "
read password
if [ -z "${password}" ]; then
password=$(apg -n1)
fi
fi
else
echo -n "Enter new password for new MySQL account (empty for random): "
read password
if [ -z "${password}" ]; then
password=$(apg -n1)
fi
fi
mysql_add $db $user $password
}
cli() {
while getopts ":d:u:p:f" opt; do
case "$opt" in
d)
d)
db=$OPTARG
;;
u)
@ -35,7 +60,7 @@ else
esac
done
shift $((OPTIND-1))
if [ -z "${db}" ]; then
usage
exit 1
@ -45,76 +70,90 @@ else
usage
exit 1
fi
fi
is_db=$(mysql mysql -Ne "SELECT COUNT(Db) FROM db WHERE Db='${db}';")
if [ $is_db -gt 0 ]; then
echo "Database $db already exist !" >&2
exit 1
fi
is_user=$(mysql mysql -Ne "SELECT COUNT(User) from user WHERE User='${user}';")
if [ $is_user -gt 0 ]; then
if [ -n ${is_interactive} ]; then
echo "Warning, account already exists, update password ? [N/y]"
read confirm
if [ "${confirm}" = "y" ] || [ "${confirm}" = "Y" ]; then
force="update"
echo -n "Enter new password for existant MySQL account (empty for random): "
read password
fi
else
if ( is_db $db ); then
echo "Database $db already exist !" >&2
exit 1
fi
if ( is_user $user ); then
if [ -z "${force}" ]; then
if [ -n "${password}" ]; then
echo "User $user already exist, update password with -f !" >&2
exit 1
fi
else
force="update"
if [ -z "${password}" ]; then
password=$(apg -n1)
fi
fi
else
if [ -z "${password}" ]; then
password=$(apg -n1)
fi
fi
else
echo -n "Enter new password for new MySQL account (empty for random): "
read password
echo ""
mysql_add $db $user $password
}
fi
is_db() {
db=$1
mysql mysql -Ne "SHOW DATABASES;"|grep -q "^${db}$"
exit $?
}
if [ -z "${password}" ]; then
password=$(apg -n1)
random="yes"
fi
if [ -z "${force}" ]; then
mysql << END_SCRIPT
CREATE DATABASE \`${db}\`;
GRANT ALL PRIVILEGES ON \`${db}\`.* TO \`${user}\`@localhost;
FLUSH PRIVILEGES;
END_SCRIPT
if [ $? = 0 ]; then
if [ $is_user -gt 0 ]; then
echo "Database ${db} created"
else
echo "User ${user} and database ${db} created"
fi
is_user() {
user=$1
nb_user=$(mysql mysql -Ne "SELECT COUNT(User) from user WHERE User='${user}';")
if [ $nb_user -gt 0 ]; then
exit 0
else
exit 1
fi
}
else
mysql_add() {
db=$1
user=$2
password=$3
mysql << END_SCRIPT
CREATE DATABASE \`${db}\`;
GRANT ALL PRIVILEGES ON \`${db}\`.* TO \`${user}\`@localhost IDENTIFIED BY "${password}";
FLUSH PRIVILEGES;
END_SCRIPT
if [ $? = 0 ]; then
echo "Database ${db} created and password of ${user} updated"
echo -n "Create '${db}' database ..."
mysql -e "CREATE DATABASE ${db};"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO"
exit 1
fi
if [ -z "${password}" ]; then
echo -n "Grant '${user}' to '${db}' database ..."
mysql -e "GRANT ALL PRIVILEGES ON ${db}.* TO ${user}@localhost;"
grant=$?
else
echo -n "Grant '${user}' to '${db}' database with password '${password}' ..."
mysql -e "GRANT ALL PRIVILEGES ON ${db}.* TO ${user}@localhost IDENTIFIED BY '${password}';"
grant=$?
fi
if [ $grant -eq 0 ]; then
echo "OK"
else
echo "KO"
exit 1
fi
echo -n "Flush Mysql privileges ..."
mysql -e "FLUSH PRIVILEGES;"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO"
exit 1
fi
}
fi
if [ -n "${random}" ]; then
echo "Password : ${password}"
fi
main() {
if [ $# = 0 ]; then
interactive
else
cli $@
fi
}
main $@