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