eole-mariadb/postservice/25-bdd-admin

84 lines
2.1 KiB
Plaintext
Raw 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)
/usr/share/eole/sbin/mariaPwd ${password}
2018-04-10 14:21:22 +02:00
accounts=($(CreoleGet accName))
limits="$(CreoleGet accLimitTarget '')"
accLimits=(${limits// /})
index=0
previousUsers=($(awk -F ':' '{print $1}' ${readerfile} ))
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
ipAddr=($(CreoleGet ip_my_dist))
ipMask=($(CreoleGet mask_my_dist))
if [[ ${ipMask} == "255.255.255.255" ]]
then
hostsList+=("${ipAddr}")
else
hostsList+=("${ipAddr}/${ipMask}")
fi
fi
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