No interactive my-add.sh
This commit is contained in:
parent
eb1a71f96c
commit
c5424683c3
125
mysql/files/my-add.sh
Normal file → Executable file
125
mysql/files/my-add.sh
Normal file → Executable file
|
@ -1,43 +1,120 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
echo "Add an account / database in MySQL"
|
usage() {
|
||||||
echo "Enter the name of the new database"
|
echo "Usage: $0 [ -d <database> -u <user> [-p <password>] [-f] ]"
|
||||||
read db
|
}
|
||||||
|
|
||||||
echo "Enter account with all right on this new database"
|
if [ $# = 0 ]; then
|
||||||
echo "(you can use existant account)"
|
is_interactive="true"
|
||||||
read login
|
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] "
|
echo "Enter account with all right on this new database"
|
||||||
read confirm
|
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
|
if [ -z "${db}" ]; then
|
||||||
echo "Warning, if account exists, it will be reset !"
|
usage
|
||||||
echo -n "Enter new password for new MySQL account (empty for random): "
|
exit 1
|
||||||
read -s password
|
fi
|
||||||
echo ""
|
|
||||||
|
if [ -z "${user}" ]; then
|
||||||
length=${#password}
|
usage
|
||||||
|
exit 1
|
||||||
if [ -n $password ]; then
|
fi
|
||||||
password=$(apg -n1 -E FollyonWek7)
|
|
||||||
echo "New password: $password"
|
|
||||||
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
|
mysql << END_SCRIPT
|
||||||
CREATE DATABASE \`$db\`;
|
CREATE DATABASE \`${db}\`;
|
||||||
GRANT ALL PRIVILEGES ON \`$db\`.* TO \`$login\`@localhost IDENTIFIED BY "$password";
|
GRANT ALL PRIVILEGES ON \`${db}\`.* TO \`${user}\`@localhost;
|
||||||
FLUSH PRIVILEGES;
|
FLUSH PRIVILEGES;
|
||||||
END_SCRIPT
|
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
|
else
|
||||||
|
|
||||||
mysql << END_SCRIPT
|
mysql << END_SCRIPT
|
||||||
CREATE DATABASE \`$db\`;
|
CREATE DATABASE \`${db}\`;
|
||||||
GRANT ALL PRIVILEGES ON \`$db\`.* TO \`$login\`@localhost;
|
GRANT ALL PRIVILEGES ON \`${db}\`.* TO \`${user}\`@localhost IDENTIFIED BY "${password}";
|
||||||
FLUSH PRIVILEGES;
|
FLUSH PRIVILEGES;
|
||||||
END_SCRIPT
|
END_SCRIPT
|
||||||
|
|
||||||
|
if [ $? = 0 ]; then
|
||||||
|
echo "Database ${db} created and password of ${user} updated"
|
||||||
|
fi
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "If no error, new database $db is OK"
|
if [ -n "${random}" ]; then
|
||||||
|
echo "Password : ${password}"
|
||||||
|
fi
|
||||||
|
|
Loading…
Reference in a new issue