#!/bin/bash DB=$1 CONFIG=$2 SQL=$3 if ! [ "$CONFIG" ]; then echo "SYN-3 mysql management:" echo "Usage: $0 < /path/to/sqlfile" echo "" echo "-Makes sure mysql is running" echo "-If configfile contains the magic %mysql_passwd% string:" echo " -Create random password" echo " -Grant sane privileges with random password on a username with same name as the database" echo " -Store password in the config file." echo " -If no database exists, open run mysql and pipe sql-data to it." echo "TIP: make sure your sql statements dont do any GRANTS or try to do things on other databases" exit 1 fi if echo $DB|grep _; then echo "Dont use underscores in the database name!"; exit 1 fi if grep %mysql_passwd "$CONFIG"; then echo "* Getting mysql root password.." #make sure those permissions are safe: chown root /etc/my.passwd chmod 700 /etc/my.passwd ROOTPW=`cat /etc/my.passwd` if ! [ "$ROOTPW" ]; then echo "Please put the sql rootpassword in /etc/my.passwd!" exit 1 fi echo "* Making sure mysql is running.." svcstart /service/mysql || exit 1 echo "* Resetting permissions and password for $DB" PASSWD=`cat /proc/sys/kernel/random/uuid` || exit 1 mysql --password="$ROOTPW" --execute="drop user '$DB'@localhost;" &>/dev/null mysql --password="$ROOTPW" --execute="grant all privileges on \`$DB\`.* to '$DB'@'localhost' identified by '$PASSWD';" || exit 1 if ! mysqlshow --password="$ROOTPW" "$DB" 2> /dev/null; then echo "* Importing sql statements...." mysql --password="$PASSWD" --user="$DB" || exit 1 fi echo "* Updating $CONFIG with password.." sed -i "s/%mysql_passwd%/$PASSWD/" "$CONFIG" || exit 1 echo "* cleaning up..." svcreset /service/mysql || exit 1 else echo "Configfile $CONFIG already has a password - doing nothing." echo "Please replace the password in the config with %mysql_passwd% if you want something to happen." fi exit 0