From b3e918f2520fcbdd94e471f33be333a525452908 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Sun, 3 Oct 2021 12:18:08 -0700 Subject: [PATCH] -s save flag --- bin/vhost-mysql-db-add.sh | 68 ++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/bin/vhost-mysql-db-add.sh b/bin/vhost-mysql-db-add.sh index a4a93d8..2f88b49 100755 --- a/bin/vhost-mysql-db-add.sh +++ b/bin/vhost-mysql-db-add.sh @@ -12,34 +12,65 @@ help() thisfilename=$(basename -- "$0") echo "Creates default MySQL database and db user for specified virtualhost." echo "" - echo "usage: $thisfilename virtualhost. [OPTIONS]" + echo "usage: $thisfilename virtualhost [OPTIONS]" echo "" echo " -h Print this help." echo " -u USERNAME Username for accessing the database. Optional, autogenerated if none specified." echo " -p PASSWORD Password for username. Optional, random password generated if none specified." + echo " -s Save db info to /home/username/.my.cnf. Warning! This inlcudes the unencrypted password." + echo " -v Verbose - output newly created db info to console." echo "" - echo " MySQL database names is based on virtualhost, with . replaced by the word 'dot'" + echo " MySQL database names is based on virtualhost with . replaced by the word 'dot'" echo " and - replaced by the word 'dash'. If username is autogenerated it is based on" echo " the shell username & the virtualhost name." echo " e.g. for virtualost example.com the db name will be 'exampledotcom' and the" echo " username will be examplec@example.com." - echo " db info stored in a file named /srv/www/$virtualhost/db_info.txt" + echo " It is highly recommended to use either the -s or -v option if you don't use -p." exit } +while getopts "hu:p:sv" opt; do + case "${opt}" in + h ) + help + exit;; + u ) + username=${OPTARG} + ;; + p ) + password=${OPTARG} + ;; + s ) + save=true + ;; + v ) + verbose=true + ;; + \? ) + echo "Invalid option: $OPTARG" + exit 1 + ;; + : ) + echo "Invalid option: $OPTARG requires an argument" + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) # check for and set virtualhost if [ -n "$1" ]; then - if [ $1 == "-h" ]; then - help - else - virtualhost="${1,,}" - fi + virtualhost="${1,,}" else echo "virtualhost not set" exit 1 fi +echo "virtualhost=$virtualhost username=$username password=$password save=$save verbose=$verbose" + +exit + # make sure virtualhost exists if [ ! -d /srv/www/$virtualhost ]; then echo "virtualhost $virtualhost does not exist" @@ -79,12 +110,17 @@ mysql -e "GRANT ALL PRIVILEGES ON $database.* TO '$username'@'localhost';" mysqladmin flush-privileges # save mysql db info to file -touch /srv/www/$virtualhost/db_info.txt -chown $vhost_username:$vhost_username /srv/www/$virtualhost/db_info.txt -chmod 640 /srv/www/$virtualhost/db_info.txt -echo "hostname=127.0.0.1" > /srv/www/$virtualhost/db_info.txt -echo "database=$database" >> /srv/www/$virtualhost/db_info.txt -echo "username=$username" >> /srv/www/$virtualhost/db_info.txt -echo "password=$password" >> /srv/www/$virtualhost/db_info.txt +if [ -n $save ]; then + touch /home/$vhost_username/.my.cnf + chown $vhost_username:$vhost_username /home/$vhost_username/.my.cnf + chmod 640 /home/$vhost_username/.my.cnf + echo "[client]" > /home/$vhost_username/.my.cnf + echo "host=127.0.0.1" >> /home/$vhost_username/.my.cnf + echo "database=$database" >> /home/$vhost_username/.my.cnf + echo "user=$username" >> /home/$vhost_username/.my.cnf + echo "password=$password" >> /home/$vhost_username/.my.cnf +fi -echo "database '$database' created with username '$username' and password '$password'" +if [ -n $verbose ]; then + echo "database=$database user=$username password=$password" +fi