{"id":432,"date":"2016-12-12T14:21:54","date_gmt":"2016-12-12T06:21:54","guid":{"rendered":"https:\/\/yyshell.com\/?p=432"},"modified":"2016-12-20T19:15:13","modified_gmt":"2016-12-20T11:15:13","slug":"%e7%bd%91%e7%bb%9c%e5%a4%87%e4%bb%bdmysql%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/yyshell.com\/?p=432","title":{"rendered":"\u7f51\u7edc\u5907\u4efdMySQL\u6570\u636e\u5e93"},"content":{"rendered":"<p>\u672c\u7f16\u4e3b\u8981\u662f\u7ed9\u5927\u5bb6\u8bb2\u8ff0\u4e0b\u6570\u636e\u5e93\u5907\u4efd\u76f8\u5173\u77e5\u8bc6\uff0c\u6211\u4eec\u7684\u5907\u4efd\u670d\u52a1\u5668\u4e3a192.168.42.15\uff0c\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e3a192.168.42.101\uff0c\u91c7\u7528\u9ed8\u8ba4\u7684mysqldump\u5de5\u5177\u8fdb\u884c\u5907\u4efd\u3002\u9996\u5148\u662f\u767b\u5f55\u6570\u636e\u5e93\u670d\u52a1\u5668\uff0c\u7528root\u7528\u6237\u767b\u5f55\uff1a<\/p>\n<pre class=\"lang:sh decode:true \">mysql -uroot -p<\/pre>\n<p>\u767b\u5f55\u540e\u521b\u5efa\u4e13\u95e8\u5907\u4efd\u7684\u5907\u4efd\u7528\u6237bakuser\uff1a<\/p>\n<pre class=\"lang:mysql decode:true \">CREATE USER 'bakuser'@'192.168.42.15' IDENTIFIED BY 'yourpasswd';\r\nGRANT SELECT,SHOW VIEW,TRIGGER,LOCK TABLES,RELOAD,FILE ON dbname.* TO 'bakuser'@'192.168.42.15';<\/pre>\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u4e0b\uff0c\u7b2c\u4e8c\u6761mysql\u547d\u4ee4\u4e2d\uff0c\u76f8\u5e94\u7684select\u7b49\u6743\u9650\u4e3a\u901a\u5e38\u5907\u4efd\u9700\u8981\u7684\u6743\u9650\uff0c\u5bf9\u4e8e\u5907\u4efd\u6765\u8bf4\u5e94\u8be5\u8db3\u591f\u4e86\uff0c\u5343\u4e07\u4e0d\u8981\u6388\u4e88all priviliges\uff0c\u6388\u6743dbname\u586b\u5199\u4f60\u9700\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u5373\u53ef\uff0c\u5982\u679c\u6709\u591a\u4e2a\u6570\u636e\u5e93\u8981\u5907\u4efd\uff0c\u5c31\u5199\u6210\u901a\u914d\u7b26*\u53f7\u3002\u521b\u5efa\u7528\u6237\u4f7f\u7528\u7684\u5bc6\u7801\u7528\u4f60\u4eec\u81ea\u5df1\u7684\u590d\u6742\u5bc6\u7801\uff0c\u4e0d\u8981\u4f7f\u7528yourpasswd\u3002<\/p>\n<p>\u5728\u76f8\u5e94\u7684\u5907\u4efd\u670d\u52a1\u5668192.168.42.15\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u5907\u4efd\u811a\u672cdb101bak.sh\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:sh decode:true\">!# \/bin\/bash\r\n#\u5b9a\u4e49\u65f6\u95f4\u53d8\u91cf\r\nyesterday=$(date +%F -d yesterday)\r\n\r\n#\u6570\u636e\u673a\u5668\u4fe1\u606f\r\ndb_username=\"bakuser\"\r\ndb_password=\"yourpasswd\"\r\ndb_host=\"192.168.42.101\"\r\n\r\n######################\r\n# \u540c\u6b65\u5e93\u6570\u636e\r\n######################\r\n\r\necho \"DB Sync Start... \"`date +'%F %T'`\r\n\r\nfor db in dbname\r\ndo\r\n        sql_file=\"\/backup\/101_mysql\/$db-$yesterday.sql\"\r\n        rar_file=\"\/backup\/101_mysql\/$db-$yesterday\"\r\n        echo \"$db Export Start...$sql_file \"`date +'%F %T'`\r\n        \/usr\/bin\/mysqldump -h $db_host -u $db_username -p\"$db_password\" $db &gt; $sql_file\r\n        \/usr\/local\/bin\/rar m -ma -r -rr5 -hpyourpassword $rar_file $sql_file\r\n        echo \"DB $db backup End... \"`date +'%F %T'`\r\ndone\r\necho \"DB ALL back End... \"`date +'%F %T'`<\/pre>\n<p>\u6b64\u5907\u4efd\u7684shell\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528linux\u7248\u672c\u7684rar\u8fdb\u884c\u4e86\u5907\u4efd\uff0c\u5e76\u6ca1\u6709\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7684tar\u8fdb\u884c\u5907\u4efd\uff0c\u4e3b\u8981\u662f\u56e0\u4e3arar\u7684\u81ea\u5e26\u6062\u590d\u4fe1\u606f\u7684\u529f\u80fd\u5f88\u597d\uff0c\u514d\u9664\u4e86\u4f7f\u7528\u7b2c\u4e09\u65b9\u8f6f\u4ef6\uff0c\u521b\u5efa\u6062\u590d\u5377\u4fe1\u606f\u7684\u9ebb\u70e6\uff0c\u53e6\u5916\uff0crar5\u683c\u5f0f\u81ea\u5e26\u7684\u52a0\u5bc6\u4e5f\u6bd4\u8f83\u597d\u7528\uff0c\u514d\u9664\u4e86\u4f7f\u7528openssl\u52a0\u5bc6\u7684\u9ebb\u70e6\uff0c\u53e6\u5916\uff0c\u5982\u679c\u8981\u6062\u590d\u6570\u636e\uff0crar\u5728pc\u4e0a\u57fa\u672c\u90fd\u4f1a\u6709\uff0c\u53ef\u4ee5\u8ba9\u666e\u901a\u7528\u6237\uff0c\u4e5f\u80fd\u81ea\u5df1\u6062\u590d\u6570\u636e\u3002RAR\u7684\u76f8\u5173\u4f7f\u7528\u53c2\u8003<a href=\"https:\/\/yyshell.com\/?p=422\">\u524d\u9762\u7684\u6587\u7ae0<\/a>\u3002<\/p>\n<p>\u6700\u540efor\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u4e00\u6b21\u5907\u4efd\u591a\u4e2a\u6587\u4ef6\uff0c\u5982\u679c\u4f60\u8981\u5907\u4efddbname1,dbname2,dbname3\u4e09\u4e2a\u6570\u636e\u5e93\uff0c\u76f4\u63a5\u5199\u6210for db in dbname1 dbname2 dbname3\u8fd9\u7c7b\u6837\u5f0f\u5373\u53ef\uff0c\u7f51\u7edc\u5907\u4efd\u514d\u53bb\u4e86\u6570\u636e\u5e93\u672c\u5730\u5907\u4efd\u540e\u5728scp\u6216\u8005rsync\u7684\u9ebb\u70e6\uff0c\u5728\u516c\u53f8\u5185\u7f51\u73af\u5883\u4e0b\uff0c\u4e5f\u662f\u6bd4\u8f83\u5b89\u5168\u7684\uff0cmysql\u5bc6\u7801\u9ed8\u8ba4\u597d\u50cf\u662fsha256 hash\u51fa\u6765\u7684\uff08\u8fd9\u70b9\u5f85\u6838\u5b9e\uff09\uff0c\u5373\u4f7f\u5185\u7f51\u6709\u4eba\u6293\u5305\uff0c\u8981\u66b4\u529b\u7834\u89e3\u76f8\u5e94\u7684\u5bc6\u7801\uff0c\u5728\u7edd\u5927\u90e8\u5206\u516c\u53f8\u5e94\u8be5\u4e5f\u662f\u4e0d\u53ef\u80fd\u7684\u3002<\/p>\n<p>\u6211\u4eec\u628a\u811a\u672c\u8bbe\u7f6e\u6210\u53ef\u6267\u884c\uff1a<\/p>\n<pre class=\"lang:sh decode:true \">chmod a+x \/opt\/scripts\/db101bak.sh<\/pre>\n<p>\u6700\u540e\u5c31\u662f\u5efa\u7acb\u5907\u4efd\u7684\u8ba1\u5212\u4efb\u52a1\u8f93\u5165\uff1a<\/p>\n<pre class=\"lang:sh decode:true \">crontab -e<\/pre>\n<p>\u8f93\u5165\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre class=\"lang:sh decode:true \">0 0 * * 6 \/opt\/scripts\/db101bak.sh &gt;&gt; \/opt\/scripts\/dbbak.log 2&gt;&amp;1<\/pre>\n<p>\u8fd9\u6837\u7684\u8bdd\uff0c\u5728\u6bcf\u4e2a\u5468\u516d\u76840\u70b9\uff0c\u5c31\u628a\u672c\u5468\u7684\u5185\u5bb9\u5907\u4efd\u5b8c\u6bd5\u4e86\uff0c\u751f\u6210\u7684\u6587\u4ef6\u683c\u5f0f\u6309\u7167\u811a\u672c\u4e2d\u7684\u5199\u6cd5\uff0c\u5e94\u8be5\u662f\u7c7b\u4f3c\u8fd9\u6837\u7684\uff0cdbname-2016-12-12.rar\u8fd9\u6837\u7684\u547d\u540d\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7f16\u4e3b\u8981\u662f\u7ed9\u5927\u5bb6\u8bb2\u8ff0\u4e0b\u6570\u636e\u5e93\u5907\u4efd\u76f8\u5173\u77e5\u8bc6\uff0c\u6211\u4eec\u7684\u5907\u4efd\u670d\u52a1\u5668\u4e3a192.168.42<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12,7],"tags":[73,17,29,71],"class_list":["post-432","post","type-post","status-publish","format-standard","hentry","category-linux","category-12","category-7","tag-backup","tag-linux","tag-mysql","tag-rar"],"_links":{"self":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts\/432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=432"}],"version-history":[{"count":2,"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts\/432\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/yyshell.com\/index.php?rest_route=\/wp\/v2\/posts\/432\/revisions\/435"}],"wp:attachment":[{"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yyshell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}