{"id":1836,"date":"2012-12-17T12:48:10","date_gmt":"2012-12-17T12:48:10","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=1836"},"modified":"2023-05-11T07:56:58","modified_gmt":"2023-05-11T07:56:58","slug":"is-your-backup-complete-backup-mysql-users-privileges-grants","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/is-your-backup-complete-backup-mysql-users-privileges-grants","title":{"rendered":"Is your backup complete? &#8211; backup MySQL users privileges \/ grants &#8211; pt-show-grants"},"content":{"rendered":"\n<p>Everybody knows the criticality of backup. You might have had your database dumped, data directory copied, flat files exported and even config and binary log files backed-up. But did you backup your MySQL user privileges?<\/p>\n\n\n\n<p>One can easily forget to backup the user privileges but it is much easier to do and you should backup your MySQL server user privileges \/ grants regularly.<\/p>\n\n\n\n<p><strong>1. Backup MySQL user privileges using mysqldump:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>$] mysqldump -uUSER -pPASS --databases mysql > mysql.sql<\/code><\/code><\/pre>\n\n\n\n<!--more Continue Reading...-->\n\n\n\n<p><strong>2. Copy MySQL data directory:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>$] cd \/var\/lib\/mysql\/ &#91; assuming default data directory ]<br>\nmysql&gt; flush tables with read lock;<br>\n$] cp -R mysql \/path\/to\/backup\/mysql_grants_dir<br>\nmysql&gt; unlock tables<\/code><\/code><\/pre>\n\n\n\n<p><strong>3. Using Percona tool pt-show-grants:<\/strong><\/p>\n\n\n\n<p>pt-show-grants shows grants (user privileges) from a MySQL server. It can also write REVOKE and DROP user statements as required.<\/p>\n\n\n\n<p>If you donot have Percona Tools already installed you may download individual tool as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>$] wget percona.com\/get\/pt-show-grants<br>$] chmod +x pt-show-grants<br>$] pt-show-grants -uUSER -pPASSWORD > user_grants.sql<\/code><\/code><\/pre>\n\n\n\n<p>You may also include following options:<br>&#8211;separate: Write grants with individual privileges<br>&#8211;revoke: Write revoke grants for all grants<br>&#8211;drop: Add drop user command before grant syntax<\/p>\n\n\n\n<p>For more info:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>$] perldoc pt-show-grants<\/code><\/code><\/pre>\n\n\n\n<p>Using pt-show-grants:<br>Getting MySQL user grants for specified user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><br><code>root@ubuntu:~# pt-show-grants -uroot -pkedar --revoke --drop --only kedar<br> -- Grants dumped by pt-show-grants<br> -- Dumped from server Localhost via UNIX socket, MySQL 5.5.27-0ubuntu2-log at 2012-12-17 04:54:34<br> -- Revoke statements for 'kedar'@'localhost'<br> REVOKE LOCK TABLES, RELOAD, SELECT, SHOW DATABASES, SHOW VIEW ON *.* FROM 'kedar'@'localhost';<br> DROP USER 'kedar'@'localhost';<br> DELETE FROM `mysql`.`user` WHERE `User`='kedar' AND `Host`='localhost';<br> -- Grants for 'kedar'@'localhost'<br> GRANT LOCK TABLES, RELOAD, SELECT, SHOW DATABASES, SHOW VIEW ON *.* TO 'kedar'@'localhost';<\/code><br>You can also specify comma separated list of users.<\/code><\/pre>\n\n\n\n<p><strong>Advantages \/ use:<\/strong><br>&#8211; Easily replicate users from one server to another.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>\/\/ Creating user USER_X on server B like that on server A<br>\npt-show-grants -uUSER -pPASS -hSERVER_A --only USER_X | mysql -uUSER -pPASS -hSERVER_B<\/code><\/code><\/pre>\n\n\n\n<p>&#8211; Unified sorted canonicalized grant syntax to put in version control<br>&#8211; As pt-show-grants canonicalize the grant syntax, it&#8217;d be good to do &#8220;diff&#8221; the grants of two different MySQL instances.<br>&#8211; Easily generate revoke syntax for particular user\/ users:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n<code>root@ubuntu:~# pt-show-grants -uroot -pkedar --revoke --only kedar | grep REVOKE\n REVOKE LOCK TABLES, RELOAD, SELECT, SHOW DATABASES, SHOW VIEW ON *.* FROM 'kedar'@'localhost';<\/code><\/code><\/pre>\n\n\n\n<p>It is important to ensure that your backup is complete and we&#8217;re not missing the database users.<\/p>\n","protected":false},"excerpt":{"rendered":"Everybody knows the criticality of backup. You might have had your database dumped, data directory copied, flat files exported and even config and binary log files backed-up. But did you&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,377],"tags":[15,351,350,427,318,352,349],"class_list":{"0":"post-1836","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-articles","8":"tag-backup","9":"tag-backup-privileges","10":"tag-backup-user-grants","11":"tag-mysql","12":"tag-mysql-database","13":"tag-pt-show-grants","14":"tag-user-privileges"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=1836"}],"version-history":[{"count":16,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1836\/revisions"}],"predecessor-version":[{"id":2882,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1836\/revisions\/2882"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=1836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=1836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=1836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}