Last active
August 29, 2015 14:23
-
-
Save starkers/0a17577d7dcb62b18181 to your computer and use it in GitHub Desktop.
dump mysql grants into SQL compatible sytax
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
#!/usr/bin/env bash | |
set -o nounset # Treat unset variables as an error | |
# | |
# you can call this script as such if needed: | |
# ./grants --user="mymysqlusername" --pass="password" --host="somehost" | |
# | |
# I much prefer .my.cnf or similar | |
grants(){ | |
mysql -B -N $@ -e "SELECT DISTINCT CONCAT( | |
'SHOW GRANTS FOR ''', user, '''@''', host, ''';' | |
) AS query FROM mysql.user" | \ | |
mysql $@ | \ | |
sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}' | |
} | |
#you can hardcode creds here but I don't recommend it: | |
#grants --user="$SrcUsername" --pass="$SrcPass" --host="$SrcHost" | |
#just call the function directly, note "mysql $@" above.. so you can pass extra flags here | |
grants |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i'm using this more and more these days. nice work starkers!