{"id":15925,"date":"2024-11-18T08:25:14","date_gmt":"2024-11-18T00:25:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/15925\/"},"modified":"2024-11-18T08:25:14","modified_gmt":"2024-11-18T00:25:14","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8linux%e4%b8%8a%e5%a4%87%e4%bb%bd%e5%92%8c%e8%bf%98%e5%8e%9f%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15925\/","title":{"rendered":"\u5982\u4f55\u5728Linux\u4e0a\u5907\u4efd\u548c\u8fd8\u539f\u6570\u636e\u5e93"},"content":{"rendered":"<p>\u5982\u4f55\u5728\u4e0a\u5907\u4efd\u548c\u8fd8\u539f\u6570\u636e\u5e93<\/p>\n<p>\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5907\u4efd\u548c\u8fd8\u539f\u6570\u636e\u5e93\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u4e3a\u4e86\u9632\u6b62\u6570\u636e\u4e22\u5931\uff0c\u8fd8\u662f\u4e3a\u4e86\u8fc1\u79fb\u6570\u636e\u5e93\uff0c\u90fd\u9700\u8981\u638c\u63e1\u8fd9\u4e00\u6280\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728Linux\u4e0a\u5982\u4f55\u8fdb\u884c\u6570\u636e\u5e93\u5907\u4efd\u548c\u8fd8\u539f\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5907\u4efd\u6570\u636e\u5e93<\/p>\n<ol>\n<li>\u4f7f\u7528mysqldump\u547d\u4ee4\u5907\u4efdMySQL\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p>MySQL\u662f\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5907\u4efdMySQL\u6570\u636e\u5e93\u53ef\u4ee5\u4f7f\u7528mysqldump\u547d\u4ee4\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>mysqldump -u &lt;username&gt; -p&lt;password&gt;&lt;database_name&gt; &amp;gt; &lt;backup_file.sql&gt;&lt;\/backup_file.sql&gt;&lt;\/database_name&gt;&lt;\/password&gt;&lt;\/username&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c \u662f\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\uff0c \u662f\u6570\u636e\u5e93\u7684\u5bc6\u7801\uff0c \u662f\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c \u662f\u5907\u4efd\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c\u8981\u5907\u4efd\u540d\u4e3a&#8221;mydb&#8221;\u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a <\/p>\n<pre>mysqldump -u root -p123456 mydb &amp;gt; \/backup\/mydb_backup.sql<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u4f7f\u7528pg_dump\u547d\u4ee4\u5907\u4efdPostgreSQL\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p>\u7c7b\u4f3c\u5730\uff0c\u5907\u4efdPostgreSQL\u6570\u636e\u5e93\u53ef\u4ee5\u4f7f\u7528pg_dump\u547d\u4ee4\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>pg_dump -U &lt;username&gt; -W -Ft &lt;database_name&gt; -f &lt;backup_file.tar&gt;&lt;\/backup_file.tar&gt;&lt;\/database_name&gt;&lt;\/username&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c \u662f\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\uff0c \u662f\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c \u662f\u5907\u4efd\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c\u8981\u5907\u4efd\u540d\u4e3a&#8221;mydb&#8221;\u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a <\/p>\n<pre>pg_dump -U postgres -W -Ft mydb -f \/backup\/mydb_backup.tar<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e8c\u3001\u8fd8\u539f\u6570\u636e\u5e93<\/p>\n<ol>\n<li>\u8fd8\u539fMySQL\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p>\u8981\u8fd8\u539fMySQL\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>mysql -u &lt;username&gt; -p&lt;password&gt;&lt;database_name&gt;&lt;\/database_name&gt;&lt;\/password&gt;&lt;\/username&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c \u662f\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\uff0c \u662f\u6570\u636e\u5e93\u7684\u5bc6\u7801\uff0c \u662f\u8981\u8fd8\u539f\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c \u662f\u5907\u4efd\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5907\u4efd\u6587\u4ef6&#8221;mydb_backup.sql&#8221;\u8fd8\u539f\u5230\u540d\u4e3a&#8221;mydb&#8221;\u7684\u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a <\/p>\n<pre>mysql -u root -p123456 mydb &lt;ol start=\"2\"&gt;&lt;li&gt;\u8fd8\u539fPostgreSQL\u6570\u636e\u5e93&lt;\/li&gt;&lt;\/ol&gt;&lt;p&gt;\u8981\u8fd8\u539fPostgreSQL\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a&lt;\/p&gt;&lt;pre class=\"brush:bash;toolbar:false;\"&gt;pg_restore -U &lt;username&gt; -d &lt;database_name&gt;&lt;backup_file.tar&gt;&lt;\/backup_file.tar&gt;&lt;\/database_name&gt;&lt;\/username&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c \u662f\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\uff0c \u662f\u8981\u8fd8\u539f\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c \u662f\u5907\u4efd\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5907\u4efd\u6587\u4ef6&#8221;mydb_backup.tar&#8221;\u8fd8\u539f\u5230\u540d\u4e3a&#8221;mydb&#8221;\u7684\u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a <\/p>\n<pre>pg_restore -U postgres -d mydb \/backup\/mydb_backup.tar<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u5b9a\u671f\u5907\u4efd\u6570\u636e\u5e93<\/p>\n<p>\u5b9a\u671f\u5907\u4efd\u6570\u636e\u5e93\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\u3002\u901a\u8fc7\u7f16\u5199Shell\u811a\u672c\uff0c\u5e76\u4f7f\u7528crontab\u5b9a\u65f6\u4efb\u52a1\uff0c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5907\u4efd\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5907\u4efd\u811a\u672c\u793a\u4f8b\uff1a<\/p>\n<pre>#!\/bin\/bash\n\n#\u6570\u636e\u5e93\u5907\u4efd\u8def\u5f84\nbackup_dir=\"\/backup\"\n#\u6570\u636e\u5e93\u7528\u6237\u540d\nusername=\"root\"\n#\u6570\u636e\u5e93\u5bc6\u7801\npassword=\"123456\"\n#\u9700\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\u79f0\ndatabase_name=\"mydb\"\n#\u5907\u4efd\u6587\u4ef6\u540d\nbackup_file=\"${backup_dir}\/${database_name}_backup_$(date +%Y%m%d%H%M%S).sql\"\n\n#\u6267\u884c\u5907\u4efd\u547d\u4ee4\nmysqldump -u ${username} -p${password} ${database_name} &amp;gt; ${backup_file}\n\n#\u5220\u9664\u8fc7\u671f\u5907\u4efd\uff08\u4fdd\u7559\u6700\u8fd17\u5929\u7684\u5907\u4efd\uff09\nfind ${backup_dir} -name \"${database_name}_backup_*\" -type f -mtime +7 -exec rm -f {} ;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5c06\u4e0a\u8ff0\u811a\u672c\u4fdd\u5b58\u4e3abackup.sh\uff0c\u5e76\u6dfb\u52a0\u53ef\u6267\u884c\u6743\u9650\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u4f7f\u7528crontab\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1\uff1a<\/p>\n<pre>crontab -e<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u6253\u5f00\u7684\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff0c\u8868\u793a\u6bcf\u65e5\u51cc\u66682\u70b9\u6267\u884c\u5907\u4efd\u4efb\u52a1\uff1a<\/p>\n<pre>0 2 * * * \/bin\/bash \/path\/to\/backup.sh<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u5373\u53ef\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Linux\u4e0a\u8f7b\u677e\u5907\u4efd\u548c\u8fd8\u539f\u6570\u636e\u5e93\uff0c\u5e76\u5b9a\u671f\u6267\u884c\u5907\u4efd\u4efb\u52a1\u3002\u4fdd\u8bc1\u6570\u636e\u5e93\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\uff0c\u662f\u4fdd\u969c\u6570\u636e\u4e0d\u4e22\u5931\u7684\u91cd\u8981\u63aa\u65bd\u4e4b\u4e00\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Linux\u4e0a\u5907\u4efd\u548c\u8fd8\u539f\u6570\u636e\u5e93\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728\u4e0a\u5907\u4efd\u548c\u8fd8\u539f\u6570\u636e\u5e93 \u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5907\u4efd\u548c\u8fd8\u539f\u6570\u636e\u5e93\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u4e3a\u4e86\u9632\u6b62\u6570\u636e\u4e22\u5931\uff0c\u8fd8\u662f\u4e3a\u4e86\u8fc1\u79fb\u6570\u636e\u5e93\uff0c\u90fd\u9700\u8981\u638c\u63e1\u8fd9\u4e00\u6280\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728Linux\u4e0a\u5982\u4f55\u8fdb\u884c\u6570\u636e\u5e93\u5907\u4efd\u548c\u8fd8\u539f\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5907\u4efd\u6570\u636e\u5e93 \u4f7f\u7528mysqldump\u547d\u4ee4\u5907\u4efdMySQL\u6570\u636e\u5e93 MySQL\u662f\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5907\u4efdMySQL\u6570\u636e\u5e93\u53ef\u4ee5\u4f7f\u7528mysqldump\u547d\u4ee4\u3002\u5982\u4e0b\u6240\u793a\uff1a mysqldump -u &lt;username&gt; -p&lt;password&gt;&lt;database_name&gt; &amp;gt; &lt;backup_file.sql&gt;&lt;\/backup_file.sql&gt;&lt;\/database_name&gt;&lt;\/password&gt;&lt;\/username&gt; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c \u662f\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\uff0c \u662f\u6570\u636e\u5e93\u7684\u5bc6\u7801\uff0c \u662f\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c \u662f\u5907\u4efd\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c\u8981\u5907\u4efd\u540d\u4e3a&#8221;mydb&#8221;\u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a mysqldump -u root -p123456 mydb &amp;gt; \/backup\/mydb_backup.sql \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528pg_dump\u547d\u4ee4\u5907\u4efdPostgreSQL\u6570\u636e\u5e93 \u7c7b\u4f3c\u5730\uff0c\u5907\u4efdPostgreSQL\u6570\u636e\u5e93\u53ef\u4ee5\u4f7f\u7528pg_dump\u547d\u4ee4\u3002\u5982\u4e0b\u6240\u793a\uff1a pg_dump -U &lt;username&gt; -W -Ft &lt;database_name&gt; -f &lt;backup_file.tar&gt;&lt;\/backup_file.tar&gt;&lt;\/database_name&gt;&lt;\/username&gt; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c \u662f\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\uff0c \u662f\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c \u662f\u5907\u4efd\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c\u8981\u5907\u4efd\u540d\u4e3a&#8221;mydb&#8221;\u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a pg_dump -U postgres -W -Ft mydb -f \/backup\/mydb_backup.tar \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u8fd8\u539f\u6570\u636e\u5e93 \u8fd8\u539fMySQL\u6570\u636e\u5e93 \u8981\u8fd8\u539fMySQL\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a mysql -u &lt;username&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-15925","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15925"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15925\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}