Created
February 21, 2019 16:49
-
-
Save mathieu-aubin/a68e7873a7aa7a355ad2b972ab3f946d to your computer and use it in GitHub Desktop.
export mysql users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SQLLOGIN='root'; | |
SQLPWD='PASSWORD'; | |
FINAL_SQL_FILE='mysql-exported_users.sql'; | |
UHEADTMP=$(mktemp -t mysqltmp_uheader_XXXXXXX); | |
USRTMP=$(mktemp -t mysqltmp_usr_XXXXXXX); | |
USRGRTMP=$(mktemp -t mysqltmp_usrgr_XXXXXXX); | |
USRPRTMP=$(mktemp -t mysqltmp_usrpr_XXXXXXX); | |
mysql -u "${SQLLOGIN}" -p"${SQLPWD}" <<< "SELECT CONCAT('SHOW GRANTS FOR ','\'',user,'\'@\'',host,'\'') FROM mysql.user" > ${UHEADTMP}; | |
sed '1d' ${UHEADTMP} > ${USRTMP}; | |
while read SQLUSER; do | |
mysql -u "${SQLLOGIN}" -p"${SQLPWD}" <<< "${SQLUSER}" > ${USRGRTMP}; | |
sed '1d' ${USRGRTMP} >> ${USRPRTMP}; | |
done < ${USRTMP} | |
echo -e "FLUSH PRIVILEGES\n" >> ${USRPRTMP}; | |
awk '{print $0";"}' ${USRPRTMP} >${FINAL_SQL_FILE}; | |
sleep 0.1; | |
for TMPFILE in ${UHEADTMP} ${USRTMP} ${USRGRTMP} ${USRPRTMP}; do | |
if [[ -f ${TMPFILE} ]]; then | |
rm -f ${TMPFILE}; | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To export mysql users and privileges from an installation