1 | #!/bin/bash |
---|
2 | |
---|
3 | DB=$1 |
---|
4 | CONFIG=$2 |
---|
5 | SQL=$3 |
---|
6 | |
---|
7 | if ! [ "$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 |
---|
19 | fi |
---|
20 | |
---|
21 | if echo $DB|grep _; then |
---|
22 | echo "Dont use underscores in the database name!"; |
---|
23 | exit 1 |
---|
24 | fi |
---|
25 | |
---|
26 | |
---|
27 | if 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 |
---|
57 | else |
---|
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." |
---|
60 | fi |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | exit 0 |
---|
65 | |
---|
66 | |
---|