{"id":741,"date":"2019-08-06T11:37:57","date_gmt":"2019-08-06T08:37:57","guid":{"rendered":"https:\/\/blog.airmeno.ru\/?p=741"},"modified":"2020-03-12T12:39:18","modified_gmt":"2020-03-12T09:39:18","slug":"rezervnye-kopii-i-vosstanovlenie-baz-pos","status":"publish","type":"post","link":"https:\/\/blog.airmeno.ru\/?p=741","title":{"rendered":"\u0420\u0435\u0437\u0435\u0440\u0432\u043d\u044b\u0435 \u043a\u043e\u043f\u0438\u0438 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0431\u0430\u0437 PostgreSQL"},"content":{"rendered":"\n<p>\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 \u0431\u0430\u0437\u044b:<br><strong>pg_dump <\/strong>\u2014 \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 Postgres \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0432 \u0444\u0430\u0439\u043b \u0438\u043b\u0438 \u0430\u0440\u0445\u0438\u0432.<br>\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441: pg_dump [\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440-\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u2026] [\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u2026] [\u0438\u043c\u044f_\u0431\u0434]<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0431\u0430\u0437\u044b (dump):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# \u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0434\u0430\u0442\u0430 \ncur_date=$(date --iso)\n\nPGPASSWORD=YourPassword\n# \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 \u0431\u0430\u0437 \npg_dump -U postgres mybase | gzip -c > \/backup\/mybase_$cur_date.sql.gz<\/code><\/pre>\n\n\n\n<p>\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f PGPASSWORD \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u043f\u0430\u0440\u043e\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f postgres \u0432 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043d\u0435 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u043e\u043b\u044c. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c. \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c password file (.pgpass). <\/p>\n\n\n\n<p>\u0424\u0430\u0439\u043b .pgpass \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u0440\u043d\u0435 \u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043e\u0442 \u0438\u043c\u0435\u043d\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a pg_dump. \u041f\u0440\u0438\u0432\u0438\u043b\u0435\u0433\u0438\u0438 \u043d\u0430 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0444\u0430\u0439\u043b\u0443 \u0434\u043e\u043b\u0436\u043d\u044b \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0438 \u0434\u043b\u044f \u0433\u0440\u0443\u043f\u043f\u044b (chmod 0600 ~\/.pgpass). \u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u0432\u0438\u043b\u0435\u0433\u0438\u0438 \u043c\u0435\u043d\u0435\u0435 \u0441\u0442\u0440\u043e\u0433\u0438\u0435, \u0442\u043e \u0444\u0430\u0439\u043b ~\/.pgpass \u0431\u0443\u0434\u0435\u0442 \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f. \u0415\u0441\u043b\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f PGPASSWORD, \u0442\u043e \u0444\u0430\u0439\u043b ~\/.pgpass \u043d\u0435 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f. <\/p>\n\n\n\n<p>\u0424\u043e\u0440\u043c\u0430\u0442 \u0444\u0430\u0439\u043b\u0430 .pgpass \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u0445\u043e\u0441\u0442:\u043f\u043e\u0440\u0442:\u0431\u0430\u0437\u0430:\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c:\u043f\u0430\u0440\u043e\u043b\u044c<\/code><\/pre>\n\n\n\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b .pgpass:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch ~\/.pgpass\nchmod 0600 ~\/.pgpass<\/code><\/pre>\n\n\n\n<p>\u0412 \u0444\u0430\u0439\u043b \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*:*:*:postgres:Postgres_Password<\/code><\/pre>\n\n\n\n<p>\u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c postgres:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>service postgresql restart<\/code><\/pre>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# \u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0434\u0430\u0442\u0430 \ncur_date=$(date --iso)\n\n# \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 \u0431\u0430\u0437 \npg_dump -U postgres mybase | gzip -c > \/backup\/mybase_$cur_date.sql.gz<\/code><\/pre>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0442\u043a\u0440\u044b\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044c \u0432 \u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.<\/p>\n\n\n\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435:<br><a href=\"https:\/\/postgrespro.ru\/docs\/postgrespro\/10\/app-pgdump\">https:\/\/postgrespro.ru\/docs\/postgrespro\/10\/app-pgdump<\/a><br><a href=\"https:\/\/www.postgresql.org\/docs\/current\/libpq-pgpass.html\">https:\/\/www.postgresql.org\/docs\/current\/libpq-pgpass.html<\/a><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435<\/strong><\/p>\n\n\n\n<p>\u0420\u0430\u0437\u0430\u0440\u0445\u0438\u0432\u0438\u0440\u0443\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0439 \u0430\u0440\u0445\u0438\u0432:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gunzip mybase_xx.yy.zz.gz<\/code><\/pre>\n\n\n\n<p>\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u0431\u0430\u0437\u0443 mybase \u0441 \u0434\u0430\u043c\u043f\u0430:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>psql -U postgres -d mybase -f mybase.sql<\/code><\/pre>\n\n\n\n<p><em>\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f (\u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0434\u043b\u044f PostgreSQL)<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>su postgres\npsql\n\n\\c db_name \u2013 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0431\u0430\u0437\u0435 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c db_name\n\\du \u2013 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439\n\\l \u2013 \u0441\u043f\u0438\u0441\u043e\u043a \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445\n\\dt - \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u0430\u0431\u043b\u0438\u0446 \u0432 \u0431\u0430\u0437\u0435 \n\\d \"table\" - \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b\n\\q \u2013 \u0432\u044b\u0445\u043e\u0434\n\n# \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0431\u0430\u0437\u0443 \npostgres=# DROP DATABASE mybase;\n\n# \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u0430\u0437\u0443:\npostgres=# CREATE DATABASE mybase;\n\n# \u0443\u0434\u0430\u043b\u0438\u0442\u044c \/ \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0431\u0430\u0437\u0435 \npostgres=# \\c mybase\nmybase-# DROP TABLE TABLE_NAME;\nmybase-# CREATE TABLE TABLE_NAME;\n\n# \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u043f\u043e\u043b\u043d\u044b\u043c\u0438 \u043f\u0440\u0430\u0432\u0430\u043c\u0438 \u0434\u043b\u044f \u0431\u0430\u0437\u044b mybase\npostgres=# CREATE USER username WITH PASSWORD 'user_password';\npostgres=# GRANT ALL PRIVILEGES ON DATABASE username to mybase;\n\n!!! \u041f\u043e\u0441\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\nsystemctl restart postgresql\n\n\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 pg_hba.conf, \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 \u0431\u0430\u0437\u044b:pg_dump \u2014 \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 Postgres \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0432 \u0444\u0430\u0439\u043b \u0438\u043b\u0438 \u0430\u0440\u0445\u0438\u0432.\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441: pg_dump [\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440-\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u2026] [\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u2026] [\u0438\u043c\u044f_\u0431\u0434]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[65,5],"tags":[17],"_links":{"self":[{"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=\/wp\/v2\/posts\/741"}],"collection":[{"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=741"}],"version-history":[{"count":3,"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":1072,"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions\/1072"}],"wp:attachment":[{"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.airmeno.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}