eole-mariadb/postservice/25-bdd-admin

90 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2018-04-10 14:21:22 +02:00
#!/bin/bash
todo=$(CreoleGet dbCreateFixAdmin 'non')
2018-04-11 17:00:18 +02:00
autoRepair=$(CreoleGet dbAutoOptimizeAndRepare 'non')
2018-04-12 17:38:56 +02:00
role=$(CreoleGet dbClusterPosition 'Leader')
if [[ ${role} == 'Node' ]]
then
exit 0
fi
2018-04-10 14:21:22 +02:00
if [[ ${todo} != 'non' ]]
then
readerfile="/root/.mysqlreader"
touch "${readerfile}"
chmod 600 ${readerfile}
tempFile=$(mktemp -p /root)
sqlFile=$(mktemp -p /root)
2018-04-12 17:38:56 +02:00
password=$(pwgen -1 10)
2018-05-09 15:54:38 +02:00
/usr/share/eole/sbin/mariadbPwd ${password}
2018-04-12 17:38:56 +02:00
2018-04-10 14:21:22 +02:00
accounts=($(CreoleGet accName))
limits="$(CreoleGet accLimitTarget '')"
accLimits=(${limits// /})
previousUsers=($(awk -F ':' '{print $1}' ${readerfile} ))
index=0
2018-04-10 14:21:22 +02:00
for user in ${accounts[@]}
do
PASS=$(awk -F ':' "/${user}:/ {print \$2}" ${readerfile})
[[ -z ${PASS} ]] && PASS=$(openssl rand -base64 32)
hostsList=()
hostsList+=('localhost')
if [[ ${accLimits[${index}]} != "Default" ]]
then
hostsList+=(${accLimits[${index}]//,/ })
else
2018-05-09 18:32:26 +02:00
ipAddr=($(CreoleGet ipMariaDBLimit))
ipMask=($(CreoleGet maskMariaDBLimit))
2018-05-28 16:13:45 +02:00
ix=0
for ip in ${ipAddr[@]}
do
if [[ ${ipMask[${ix}]} == "255.255.255.255" ]]
then
hostsList+=("${ip}")
else
hostsList+=("${ip}/${ipMask[${ix}]}")
fi
ix=$((ix+1))
done
2018-04-10 14:21:22 +02:00
fi
2018-05-28 16:13:45 +02:00
2018-04-10 14:21:22 +02:00
for hst in ${hostsList[@]}
do
SQL="DROP USER IF EXISTS '${user}'@'${hst}';"
SQL="${SQL} FLUSH PRIVILEGES;"
SQL="${SQL} CREATE USER '${user}'@'${hst}' IDENTIFIED BY '${PASS}';"
SQL="${SQL} GRANT ALL PRIVILEGES ON *.* TO '${user}'@'${hst}' WITH GRANT OPTION;"
SQL="${SQL} FLUSH PRIVILEGES;"
echo "${SQL}" >> ${sqlFile}
echo " Managing user: ${user}@${hst}"
done
echo "${user}:${PASS}" >> ${tempFile}
index=$((index+1))
done
2018-04-12 17:38:56 +02:00
mysql -u root --password=${password} < ${sqlFile}
2018-04-10 14:21:22 +02:00
mv ${tempFile} ${readerfile}
chmod 600 ${readerfile}
rm -rf ${sqlFile}
fi
2018-04-11 17:00:18 +02:00
if [[ ${autoRepair} == "oui" ]]
then
2018-04-12 17:38:56 +02:00
mysqlcheck -u root -p ${password} --auto-repair --check --all-databases
mysqlcheck -u root -p ${password} --optimize --check --all-databases
2018-04-11 17:00:18 +02:00
fi
2018-04-10 14:21:22 +02:00
exit 0