PostgreSQL data backup and recovery

PostgreSQL data backup and recovery

pg_dump backup

  • Backup of the database pgupgradedbis databak, p is the port
postgres@clw-db2:/pgdata/10.3> pg_dump pgupgradedb -h 10.10.56.17 -p 5438 -U postgres > databak
postgres@clw-db2:/pgdata/10.3> ll -lh
total 480M
drwx------ 19 postgres postgres 4.0K Jun  7 16:44 data
-rw-r--r--  1 postgres postgres 480M Jun  7 18:07 databak
postgres@clw-db2:/pgdata/10.3>
  • View the backup database file as follows:
postgres@clw-db2:/pgdata/10.3> cat databak
--
-- PostgreSQL database dump
--

-- Dumped from database version 10.3
-- Dumped by pg_dump version 10.3

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;


--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';


SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: pgupgrade; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.pgupgrade (
    id integer NOT NULL,
    age bigint,
    inserttime timestamp without time zone DEFAULT now()
);


ALTER TABLE public.pgupgrade OWNER TO postgres;

--
-- Name: pgupgrade_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

CREATE SEQUENCE public.pgupgrade_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.pgupgrade_id_seq OWNER TO postgres;

--
-- Name: pgupgrade_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--

ALTER SEQUENCE public.pgupgrade_id_seq OWNED BY public.pgupgrade.id;


--
-- Name: pgupgrade id; Type: DEFAULT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.pgupgrade ALTER COLUMN id SET DEFAULT nextval('public.pgupgrade_id_seq'::regclass);


--
-- Data for Name: pgupgrade; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.pgupgrade (id, age, inserttime) FROM stdin;
5       91647434        2018-06-07 17:47:38.821321
13      46223030        2018-06-07 17:47:38.82556
21      114249409       2018-06-07 17:47:38.829053
28      315543381       2018-06-07 17:47:38.831503
35      383642413       2018-06-07 17:47:38.833049
45      109718816       2018-06-07 17:47:38.8367
64      77561788        2018-06-07 17:47:38.843235
79      164887727       2018-06-07 17:47:38.84546
93      203783237       2018-06-07 17:47:38.846801
109     38643627        2018-06-07 17:47:38.854703
127     323372478       2018-06-07 17:47:38.856251
150     213251316       2018-06-07 17:47:38.858198
166     119008838       2018-06-07 17:47:38.859787
...
  • note
pg_dump 默认会使用当前登录操作系统的用户为登录数据库的用户,所以备份时请指定 -U 指定用户

备份的数据库可以在任意架构服务器上运行(32位,64位)

备份的数据库也可以在比当前版本新的数据库上运行

pg_dump 备份类似一个快照,不会对数据库进行锁

允许任意连接数据库的用户进行备份,更多参数请查看api

允许任意连接数据库的用户进行备份,更多参数请查看api

确保在备份时,d对象或者用户所属关系全部创建好了,否则恢复时出现权限错误

pg-dump restore backup data

  • Create a directory database needs to be restored
postgres=# create database pgupgradedb;
CREATE DATABASE
postgres=# \l
                                   List of databases
    Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------+----------+----------+-------------+-------------+-----------------------
 pgupgradedb | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 pocdb       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
 template1   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
(5 rows)
postgres=#
  • Recovery by psqlrestoring command
psql dbname < databak
  • Note: Execute the command change, you must first create a database need to restore the good name of the command does not automatically create a non-existent dbname

  • Began to recover

postgres@clw-db2:/pgdata/10/poc/data> psql pgupgradedb -h 10.10.56.17 -p 5432 -U postgres  < /pgdata/10.3/databak
Password for user postgres:
SET
SET
SET
SET
SET
 set_config
------------
(1 row)
SET
SET
SET
CREATE EXTENSION
COMMENT
SET
SET
CREATE TABLE
ALTER TABLE
CREATE SEQUENCE
ALTER TABLE
ALTER SEQUENCE
ALTER TABLE
COPY 11252994
  setval
----------
 11252998
(1 row)

See above represents the data recovery is complete

  • Verify that the correct recovery
postgres@clw-db2:/pgdata/10/poc/data> psql -p 5432
psql (10.3)
Type "help" for help.
pgupgradedb=# \l
                                   List of databases
    Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------+----------+----------+-------------+-------------+-----------------------
 pgupgradedb | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 pocdb       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
 template1   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
(5 rows)

pgupgradedb=# \d+
                             List of relations
 Schema |       Name       |   Type   |  Owner   |    Size    | Description
--------+------------------+----------+----------+------------+-------------
 public | pgupgrade        | table    | postgres | 560 MB     |
 public | pgupgrade_id_seq | sequence | postgres | 8192 bytes |
(2 rows)

pgupgradedb=# select max(id) from pgupgrade;
   max
----------
 11252998
(1 row)

Has been found that, before the results of a successful backup and recovery and backup!

pg_basebackup base backup

  • Query the original database library data
pgupgradedb=# \d+
                             List of relations
 Schema |       Name       |   Type   |  Owner   |    Size    | Description
--------+------------------+----------+----------+------------+-------------
 public | pgupgrade        | table    | postgres | 141 MB     |
 public | pgupgrade_id_seq | sequence | postgres | 8192 bytes |
(2 rows)

pgupgradedb=# \d+ pgupgrade
                                                            Table "public.pgupgrade"
   Column   |            Type             | Collation | Nullable |                Default                | Storage | Stats target | Description
------------+-----------------------------+-----------+----------+---------------------------------------+---------+--------------+-------------
 id         | integer                     |           | not null | nextval('pgupgrade_id_seq'::regclass) | plain   |              |
 age        | bigint                      |           |          |                                       | plain   |              |
 inserttime | timestamp without time zone |           |          | now()                                 | plain   |              |

pgupgradedb=# select count(1) from pgupgrade;
  count
---------
 2834653
(1 row)
pgupgradedb=#
  • Backup of the database state at this time

  • Log database, create a duplicate backup role,

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# CREATE USER repl WITH PASSWORD '123456' REPLICATION;
CREATE ROLE
postgres=# \c pgupgradedb
You are now connected to database "pgupgradedb" as user "postgres".
pgupgradedb=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 repl      | Replication 
  • Modify pg_hba.conffiles, configuration roles need to use, IP addresses and other information, here is Ip 10.10.56.0 replusers

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                trust
host    replication     postgres        127.0.0.1/32            trust
host    replication     repl            10.10.56.0/0            trust
#host    replication     postgres        ::1/128                 trust
~
  • After modifying the configuration file, you need to restart the service
postgres@clw-db2:/pgdata/10.3/data> pg_ctl -D /pgdata/10.3/data restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2018-06-08 16:05:07.625 CST [24614] LOG:  listening on IPv4 address "0.0.0.0", port 5438
2018-06-08 16:05:07.625 CST [24614] LOG:  listening on IPv6 address "::", port 5438
2018-06-08 16:05:07.636 CST [24614] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5438"
2018-06-08 16:05:07.672 CST [24614] LOG:  ending log output to stderr
2018-06-08 16:05:07.672 CST [24614] HINT:  Future log output will go to log destination "csvlog".
2018-06-08 16:05:07.681 CST [24643] LOG:  database system was shut down at 2018-06-08 16:05:07 CST
2018-06-08 16:05:07.696 CST [24614] LOG:  database system is ready to accept connections
 done
server started
postgres@clw-db2:/pgdata/10.3/data>
  • Use pg_basebackupthe basis for a backup
postgres@clw-db2:/pgdata/10.3/data> /opt/pgsql-10/bin/pg_basebackup -h 10.10.56.17 -p 5438 -U repl -Fp -Xs -Pv -D /pgdata/10.3/backup/pgupgradedb_backup_20180608_data/
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/7B000060 on timeline 1
pg_basebackup: starting background WAL receiver
179855/179855 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/7B000130
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
postgres@clw-db2:/pgdata/10.3/data>
  • Parameter Description
- h 数据库地址 -p 数据库端口 -U 备份使用的用户 -Fp 指定备份输出的文件格式,此处为原文件
-Xs 采用流复制的 wal 日志方法 -Pv 输出备份过程的信息 -D 备份数据库的存放目录

When the above-mentioned backup, backup storage directory must be empty, the file path needs to be created, does not automatically create

  • After viewing the backup data directory
postgres@clw-db2:/pgdata/10.3/backup> ls
pgupgradedb_backup_20180608_data
postgres@clw-db2:/pgdata/10.3/backup> cd pgupgradedb_backup_20180608_data/
postgres@clw-db2:/pgdata/10.3/backup/pgupgradedb_backup_20180608_data> ls
backup_label  pg_commit_ts  pg_ident.conf  pg_notify    pg_snapshots  pg_subtrans  PG_VERSION  postgresql.auto.conf
base          pg_dynshmem   pg_logical     pg_replslot  pg_stat       pg_tblspc    pg_wal      postgresql.conf
global        pg_hba.conf   pg_multixact   pg_serial    pg_stat_tmp   pg_twophase  pg_xact
postgres@clw-db2:/pgdata/10.3/backup/pgupgradedb_backup_20180608_data>

At this base backup is complete

Guess you like

Origin blog.csdn.net/yaoqiancuo3276/article/details/80805815