source: npl/commonservers/mysql_conf/root/sbin/syn3-mysqlcreate @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100755
File size: 2.0 KB
Line 
1#!/bin/bash
2
3DB=$1
4CONFIG=$2
5SQL=$3
6
7if ! [ "$CONFIG" ]; then
8    echo "SYN-3 mysql management:"
9    echo "Usage: $0 <database> <configfile> < /path/to/sqlfile"
10    echo ""
11    echo "-Makes sure mysql is running"
12    echo "-If configfile contains the magic %mysql_passwd% string:"
13    echo " -Create random password"
14    echo " -Grant sane privileges with random password on a username with same name as the database"
15    echo " -Store password in the config file."
16    echo " -If no database exists, open run mysql and pipe sql-data to it."
17    echo "TIP: make sure your sql statements dont do any GRANTS or try to do things on other databases"
18    exit 1
19fi
20
21if echo $DB|grep _; then
22    echo "Dont use underscores in the database name!";
23    exit 1
24fi
25
26
27if grep %mysql_passwd "$CONFIG"; then
28    echo "* Getting mysql root password.."
29    #make sure those permissions are safe:
30    chown root /etc/my.passwd
31    chmod 700 /etc/my.passwd
32    ROOTPW=`cat /etc/my.passwd`
33
34    if ! [ "$ROOTPW" ]; then
35        echo "Please put the sql rootpassword in /etc/my.passwd!"
36        exit 1
37    fi
38
39    echo "* Making sure mysql is running.."
40    svcstart /service/mysql || exit 1
41
42    echo "* Resetting permissions and password for $DB"
43    PASSWD=`cat /proc/sys/kernel/random/uuid` || exit 1
44    mysql --password="$ROOTPW" --execute="drop user '$DB'@localhost;" &>/dev/null
45    mysql --password="$ROOTPW" --execute="grant all privileges on \`$DB\`.* to '$DB'@'localhost' identified by '$PASSWD';" || exit 1
46
47    if ! mysqlshow --password="$ROOTPW" "$DB" 2> /dev/null; then
48        echo "* Importing sql statements...."
49        mysql --password="$PASSWD" --user="$DB" || exit 1
50    fi
51
52    echo "* Updating $CONFIG with password.."
53    sed -i "s/%mysql_passwd%/$PASSWD/" "$CONFIG" || exit 1
54
55    echo "* cleaning up..."
56    svcreset /service/mysql || exit 1
57else
58    echo "Configfile $CONFIG already has a password - doing nothing."
59    echo "Please replace the password in the config with %mysql_passwd% if you want something to happen."
60fi
61
62
63
64exit 0
65
66
Note: See TracBrowser for help on using the repository browser.