Skip to content

Instantly share code, notes, and snippets.

@starkers
Last active August 29, 2015 14:23
Show Gist options
  • Save starkers/0a17577d7dcb62b18181 to your computer and use it in GitHub Desktop.
Save starkers/0a17577d7dcb62b18181 to your computer and use it in GitHub Desktop.
dump mysql grants into SQL compatible sytax
#!/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
@sot001
Copy link

sot001 commented Jul 1, 2015

i'm using this more and more these days. nice work starkers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment