No interactive my-add.sh

This commit is contained in:
Victor LABORIE 2016-12-22 18:41:22 +01:00
parent eb1a71f96c
commit c5424683c3

125
mysql/files/my-add.sh Normal file → Executable file
View file

@ -1,43 +1,120 @@
#!/bin/sh
echo "Add an account / database in MySQL"
echo "Enter the name of the new database"
read db
usage() {
echo "Usage: $0 [ -d <database> -u <user> [-p <password>] [-f] ]"
}
echo "Enter account with all right on this new database"
echo "(you can use existant account)"
read login
if [ $# = 0 ]; then
is_interactive="true"
echo "Add an account / database in MySQL"
echo "Enter the name of the new database"
read db
echo -n "Does this account already exist ? [y|N] "
read confirm
echo "Enter account with all right on this new database"
echo "(you can use existant account)"
read user
else
while getopts ":d:u:p:f" opt; do
case "$opt" in
d)
db=$OPTARG
;;
u)
user=$OPTARG
;;
p)
password=$OPTARG
;;
f)
force="true"
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "Warning, if account exists, it will be reset !"
echo -n "Enter new password for new MySQL account (empty for random): "
read -s password
echo ""
length=${#password}
if [ -n $password ]; then
password=$(apg -n1 -E FollyonWek7)
echo "New password: $password"
if [ -z "${db}" ]; then
usage
exit 1
fi
if [ -z "${user}" ]; then
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 [ -z "${force}" ]; then
if [ -n "${password}" ]; then
echo "User $user already exist, update password with -f !" >&2
exit 1
fi
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
password=$(apg -n1)
random="yes"
fi
if [ -z "${force}" ]; then
mysql << END_SCRIPT
CREATE DATABASE \`$db\`;
GRANT ALL PRIVILEGES ON \`$db\`.* TO \`$login\`@localhost IDENTIFIED BY "$password";
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
else
mysql << END_SCRIPT
CREATE DATABASE \`$db\`;
GRANT ALL PRIVILEGES ON \`$db\`.* TO \`$login\`@localhost;
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
echo "If no error, new database $db is OK"
if [ -n "${random}" ]; then
echo "Password : ${password}"
fi