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,16 +4,41 @@ 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
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 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)
@ -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
is_user=$(mysql mysql -Ne "SELECT COUNT(User) from user WHERE User='${user}';") if ( is_user $user ); then
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"
fi
fi
else
echo -n "Enter new password for new MySQL account (empty for random): "
read password
echo ""
fi
if [ -z "${password}" ]; then if [ -z "${password}" ]; then
password=$(apg -n1) password=$(apg -n1)
random="yes"
fi fi
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 else
echo "User ${user} and database ${db} created" if [ -z "${password}" ]; then
password=$(apg -n1)
fi fi
fi fi
mysql_add $db $user $password
}
is_db() {
db=$1
mysql mysql -Ne "SHOW DATABASES;"|grep -q "^${db}$"
exit $?
}
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 else
exit 1
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"
fi fi
}
fi mysql_add() {
db=$1
user=$2
password=$3
if [ -n "${random}" ]; then echo -n "Create '${db}' database ..."
echo "Password : ${password}" mysql -e "CREATE DATABASE ${db};"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO"
exit 1
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
}
main() {
if [ $# = 0 ]; then
interactive
else
cli $@
fi
}
main $@