{"id":123,"date":"2024-10-30T17:49:24","date_gmt":"2024-10-30T09:49:24","guid":{"rendered":"https:\/\/zhoujibin.com\/?p=123"},"modified":"2024-10-30T17:49:25","modified_gmt":"2024-10-30T09:49:25","slug":"pg%e5%ae%9a%e6%97%b6%e5%a4%87%e4%bb%bd%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/zhoujibin.com\/?p=123","title":{"rendered":"PG\u5b9a\u65f6\u5907\u4efd\u6570\u636e"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u7f16\u5199\u5907\u4efd\u811a\u672c<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b0\u5efa\u76ee\u5f55<\/h2>\n\n\n\n<p>\u7528\u4e8e\u5b58\u653e\u5f53\u524d\u7528\u6237\u4e0b\u7684\u81ea\u5b9a\u4e49\u5b9a\u65f6\u4efb\u52a1\u811a\u672c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u65b0\u5efa\u5b9a\u65f6\u4efb\u52a1\u811a\u672c\u5b58\u653e\u76ee\u5f55\nmkdir -p \/home\/myCron;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b0\u5efa\u6587\u4ef6<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>touch \/home\/myCron\/backup_pg.sh;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6388\u4e88\u53ef\u6267\u884c\u6743\u9650<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x \/home\/myCron\/backup_pg.sh;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f16\u5199\u5220\u9664\u811a\u672c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/home\/myCron\/backup_pg.sh<\/code><\/pre>\n\n\n\n<p>\u952e\u5165\u952e\u76d8\u7684 i \u952e \uff0c\u7c98\u8d34 \u811a\u672c\uff0c\u7136\u540e\u952e\u5165\u4ee5\u4e0b\u5185\u5bb9\uff0c\u952e\u5165\u7684 esc \u952e\uff0cshift+: \uff0c\u8f93\u5165 wq \u540e \u56de\u8f66\u4fdd\u5b58\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b9a\u65f6\u5907\u4efd pg \u5e93\u6570\u636e\uff0c\u6bcf\u6b21\u5907\u4efd\u524d\u5148\u5220\u9664\u6307\u5b9a\u5927\u4e8e\u6307\u5b9a\u95f4\u9694\u7684\u5907\u4efd\n# \u83b7\u53d6\u672c\u673aip\nhost_ip=`\/sbin\/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d \"addr:\"\u200b`\n\n# pg \u5907\u4efd\u6587\u4ef6\u7684\u5b58\u653e\u4f4d\u7f6e\uff0c\u4e0d\u5b58\u548b\u5219\u4f1a\u65b0\u5efa\npg_backup_data_path=\/data\/backup_db\/pg;\n\n# pg dump\u6267\u884c\u547d\u4ee4\u6240\u5728\u76ee\u5f55\npg_dump_sh_path=\/usr\/pgsql-11\/bin\/pg_dump;\n\n# pg \u6570\u636e\u5e93 \u4e3b\u673a\u540d\u79f0\u6216ip\uff08\u82e5\u5728\u672c\u5730\u8bf7\u4f7f\u7528127.0.0.1\uff09\npg_database_host=$host_ip;\necho 'pg_database_host is '$pg_database_host;\n# pg \u6570\u636e\u5e93\u7aef\u53e3\npg_database_port=5432;\n\n# pg \u7528\u6237\u6743\u9650\npg_database_role=postgres;\n# pg \u7528\u6237\u540d\npg_database_user=postgres;\n# pg \u5bc6\u7801\uff0c\u6839\u636e\u5b9e\u9645\u6e05\u7a7a\u586b\u5199\npg_database_password=password@dba;\n\n# pg \u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u586b\u5199\nbackup_database_name=db_name\n\n# \u5907\u4efd\u6587\u4ef6\u7684\u540e\u7f00\u540d\nbackup_file_suffix=.backup;\n\n# \u5220\u9664\u5907\u4efd\u6570\u636e\u4e0e\u5f53\u524d\u65f6\u95f4\u7684\u95f4\u9694\u5929\u6570,\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u586b\u5199\ndelete_backup_data_interval=15;\n\n# \u5f53\u5929\u7684\u65f6\u95f4\nnow_date=`date +%Y-%m-%d`;\n\ndeclare -a not_delete_file_arr;\n\n# \u83b7\u5f97\u95f4\u9694\u65f6\u95f4\u5185\u7684\u5907\u4efd\u6587\u4ef6\u540d\u79f0\nfor ((i=0; i&lt;$delete_backup_data_interval; i++))\ndo\n    not_delete_file_arr&#91;$i]=$pg_backup_data_path'\/'`date -d \"-$i day \" +%Y-%m-%d`'_'$backup_database_name'_'$host_ip''$backup_file_suffix;\ndone\n# \u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\nexist_backup_file_str=`find $pg_backup_data_path -name *$backup_database_name*$backup_file_suffix`;\n\n#\u8981\u5c06$a\u5206\u5272\u5f00\uff0c\u53ef\u4ee5\u8fd9\u6837\uff1a\nOLD_IFS=\"$IFS\"\nIFS=\",\"\nexist_backup_file_str_arr=($exist_backup_file_str)\nIFS=\"$OLD_IFS\"\n# \u67e5\u627e\u5907\u4efd\u6587\u4ef6\u4fe1\u606f\nfor exist_name in ${exist_backup_file_str_arr&#91;@]}\ndo\n   isNeedDelete=1;\n    for not_delete_name in ${not_delete_file_arr&#91;@]}\n    do \n    # \u5220\u9664\u5907\u4efd\n      if &#91; $exist_name = $not_delete_name ]; then \n           isNeedDelete=0;\n        break;\n      fi;\n    done;\n    if &#91; $isNeedDelete = 1 ]; then \n        echo ''$exist_name' \u5907\u4efd\u9700\u8981\u5220\u9664';\n        rm -rf $exist_name;\n      fi;\ndone;\n\n# \u65b0\u5efa\u5907\u4efd\u76ee\u5f55\uff08\u82e5\u4e0d\u5b58\u5728\uff09\nif &#91; ! -d $pg_backup_data_path ]; then \n mkdir -p $pg_backup_data_path;\n echo $pg_backup_data_path' \u5907\u4efd\u6587\u4ef6\u5b58\u653e\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5df2\u7ecf\u81ea\u52a8\u521b\u5efa';\nfi;\n\n# \u6700\u65b0\u7684\u5907\u4efd\u6587\u4ef6\u540d\u79f0\uff1a\u65e5\u671f_\u6570\u636e\u5e93\u540d\u79f0\uff08\u6570\u636e\u5e93ip\uff09+\u5907\u4efd\u6587\u4ef6\u540e\u7f00\uff0c\u793a\u4f8b:2022-06-15_test_127.0.0.1.backup\nnow_backup_file_path=$pg_backup_data_path\/$now_date'_'$backup_database_name'_'$host_ip''$backup_file_suffix;\n\n# \u5907\u4efd\u6587\u4ef6\u81f3\u5f53\u524d\u670d\u52a1\u5668\necho '\u5f00\u59cb\u5907\u4efd\u3010'$backup_database_name'\u3011\u6570\u636e\u81f3\u672c\u5730\u670d\u52a1\u5668'\n$pg_dump_sh_path  \"host=$pg_database_host port=$pg_database_port user=$pg_database_user password=$pg_database_password dbname=$backup_database_name\" --role \"$pg_database_role\"  --format custom --blobs --encoding UTF8 --verbose --file \"$now_backup_file_path\"\necho '\u7ed3\u675f\u5907\u4efd\u3010'$backup_database_name'\u3011\u6570\u636e\u81f3\u672c\u5730\u670d\u52a1\u5668\uff0c\u5907\u4efd\u4f4d\u7f6e'$now_backup_file_path;\n\n# \u82e5\u6587\u4ef6\u9700\u8981\u540c\u65f6\u5907\u4efd\u81f3\u5176\u4ed6\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u53ea\u6709scp\u547d\u4ee4\u3002\n# \u5907\u4efd\u6587\u4ef6\u81f3 192.168.1.13 centOs \u670d\u52a1\u5668\necho '\u5f00\u59cb\u5907\u4efd\u3010'$backup_database_name'\u3011\u6570\u636e\u81f3\u3010192.168.1.13     centOs \u3011\u670d\u52a1\u5668'\n# \u793a\u4f8b\uff0c\u5f53\u524d\u5907\u4efd\u6587\u4ef6\u540c\u6b65\u590d\u5236\u81f3 192.168.1.13 \u670d\u52a1\u5668\u7684 \/data\/backup_database\/postgresql \u76ee\u5f55\nfile_server_backup_path=\/data\/backup_database\/postgresql;\n# \u6ce8\u610f\uff0c\u670d\u52a1\u5668\u4e4b\u95f4\u9700\u8981\u914d\u7f6e\u514d\u5bc6\u767b\u5f55,scp \u590d\u5236\u76ee\u5f55\u65f6\u9700\u8981\u6dfb\u52a0 -r \u53c2\u6570\nscp $now_backup_file_path  root@192.168.1.13:$file_server_backup_path\necho '\u7ed3\u675f\u5907\u4efd\u3010'$backup_database_name'\u3011\u6570\u636e\u81f3\u3010192.168.1.13     centOs \u3011\u670d\u52a1\u5668'<\/code><\/pre>\n\n\n\n<p>vim \u7528\u6cd5\u53c2\u8003\uff1ahttps:\/\/www.runoob.com\/linux\/linux-vim.html<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u811a\u672c\u6dfb\u52a0\u81f3\u5b9a\u65f6\u4efb\u52a1<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e\n# \u6dfb\u52a0\u4ee5\u4e0b\u811a\u672c\u5e76\u4fdd\u5b58\uff08\u65f6\u95f4\u53ef\u81ea\u884c\u8c03\u6574\uff09\uff0c\u5e76\u5220\u9664\u8bb0\u5f55\u65e5\u5fd7\n0 12 * * * \/home\/myCron\/backup_pg.sh &gt;&gt; \/home\/myCron\/backup_pg.log 2&gt;&amp;1;\n# \u82e5\u5b9a\u65f6\u4efb\u52a1\u6ca1\u6709\u751f\u6548\uff0c\u91cd\u8f7d\u5b9a\u65f6\u4efb\u52a1\nsystemctl restart crond;\n# \u67e5\u770b\u7f16\u5199\u7684\u5b9a\u65f6\u4efb\u52a1\ncrontab -l;\n0 12 * * * \/home\/myCron\/backup_pg.sh &gt;&gt; \/home\/myCron\/backup_pg.log 2&gt;&amp;1;<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1aLinux crontab \u547d\u4ee4\u53c2\u8003\uff1ahttps:\/\/www.runoob.com\/linux\/linux-comm-crontab.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Linux Crontab \u5b9a\u65f6\u4efb\u52a1\u53c2\u8003\uff1a https:\/\/www.runoob.com\/w3cnote\/linux-crontab-tasks.html<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u514d\u5bc6\u767b\u5f55\u914d\u7f6e<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u670d\u52a1\u5668<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5f53\u524d\u670d\u52a1\u5668<\/th><th>\u76ee\u6807\u670d\u52a1\u5668<\/th><th>\u5907\u6ce8<\/th><\/tr><\/thead><tbody><tr><td>192.168.1.12<\/td><td>192.168.1.13<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u751f\u6210\u6388\u6743\u5bc6\u5319<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># \u751f\u6210\u5bc6\u5319\uff0c\u4e00\u81f4\u70b9\u51fb\u56de\u8f66\u76f4\u81f3\u7ed3\u675f,\u5bc6\u5319\u9ed8\u8ba4\u5b58\u653e\u4f4d\u7f6e\/root\/.ssh\/\nssh-keygen;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u590d\u5236\u5bc6\u5319\u81f3\u76ee\u6807\u670d\u52a1\u5668<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u5f0f\u4e00<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-copy-id -i \/root\/.ssh\/id_rsa.pub root@192.168.1.13;\n# \u8f93\u5165\u76ee\u6807\u670d\u52a1\u5668\u5bc6\u7801\u56de\u8f66\u5373\u53ef<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u5f0f\u4e8c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/root\/.ssh\/id_rsa.pub | ssh  root@192.168.1.13 'cat &gt;&gt; \/root\/.ssh\/authorized_keys';\n# \u8f93\u5165\u76ee\u6807\u670d\u52a1\u5668\u5bc6\u7801\u56de\u8f66\u5373\u53ef<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199\u5907\u4efd\u811a\u672c \u65b0\u5efa\u76ee\u5f55 \u7528\u4e8e\u5b58\u653e\u5f53\u524d\u7528\u6237\u4e0b\u7684\u81ea\u5b9a\u4e49\u5b9a\u65f6\u4efb\u52a1\u811a\u672c \u65b0\u5efa\u6587\u4ef6 \u6388\u4e88\u53ef\u6267\u884c\u6743\u9650 \u7f16\u5199\u5220\u9664\u811a\u672c \u952e\u5165\u952e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[28,26],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-database","tag-pg","tag-sql"],"_links":{"self":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123"}],"version-history":[{"count":1,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/124"}],"wp:attachment":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}