PG 논리 백업 및 지정된 데이터베이스 객체 복구

60385 단어 PostgreSQL

테스트 데이터 생성

[postgres@lxm ~]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.

postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(3 rows)

postgres=# create database test template template0;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table test( id int primary key , name varchar);
CREATE TABLE
test=# insert into test select n, n
test-# ||'name' from generate_series(1,10) n;
INSERT 0 10
test=# select * from test;
 id |  name
----+--------
  1 | 1name
  2 | 2name
  3 | 3name
  4 | 4name
  5 | 5name
  6 | 6name
  7 | 7name
  8 | 8name
  9 | 9name
 10 | 10name
(10 rows)

test=# create table test2 (like test including all);
CREATE TABLE
test=# \d+ test2
                                        Table "public.test2"
 Column |       Type        | Collation | Nullable | Default | Storage  | Stats target | Description
--------+-------------------+-----------+----------+---------+----------+--------------+-------------
 id     | integer           |           | not null |         | plain    |              |
 name   | character varying |           |          |         | extended |              |
Indexes:
    "test2_pkey" PRIMARY KEY, btree (id)
Access method: heap

test=# insert into test2 select * from test;
INSERT 0 10
test=# select count(*) from test2;
 count
-------
    10
(1 row)

test=#
test=# \q


전체 라이브러리 테스트 백업

[postgres@lxm test2]$ pg_dump -U postgres -Ft -f test_tar.dmp -C -c --if-exists test
Password:
[postgres@lxm test2]$ ls
test_tar.dmp


test 데이터베이스 삭제

[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.

postgres=# drop database test;
DROP DATABASE
postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(3 rows)

postgres=# \q


테스트 풀 라이브러리 복원

[postgres@lxm test2]$ pg_restore -U postgres -d postgres  -C -c --if-exists test_tar.dmp
Password:
[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.

postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 test      | postgres | UTF8     | C       | C     |
(4 rows)

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \dt
         List of relations
 Schema | Name  | Type  |  Owner
--------+-------+-------+----------
 public | test  | table | postgres
 public | test2 | table | postgres
(2 rows)

test=# select count(*) from test;
 count
-------
    10
(1 row)

test=# select count(*) from test2;
 count
-------
    10
(1 row)


test 데이터베이스 삭제

[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.

postgres=# drop database test;
DROP DATABASE
postgres=# \q
[postgres@lxm t

테스트 테이블 및 데이터 복구


방법 1

  • toc 파일 생성
    [postgres@lxm test2]$ pg_restore -U postgres -l -f test.toc test_tar.dmp
    [postgres@lxm test2]$ ls
    test_tar.dmp  test.toc
    
    
  • 테스트 2 관련 코드를 주석합니다
    [postgres@lxm test2]$ more test.toc
    ;
    ; Archive created at 2020-06-16 12:43:38 CST
    ;     dbname: test
    ;     TOC Entries: 10
    ;     Compression: 0
    ;     Dump Version: 1.14-0
    ;     Format: TAR
    ;     Integer: 4 bytes
    ;     Offset: 8 bytes
    ;     Dumped from database version: 13beta1
    ;     Dumped by pg_dump version: 13beta1
    ;
    ;
    ; Selected TOC Entries:
    ;
    200; 1259 16545 TABLE public test postgres
    201; 1259 16551 TABLE public test2 postgres
    3076; 0 16545 TABLE DATA public test postgres
    3077; 0 16551 TABLE DATA public test2 postgres
    2945; 2606 16558 CONSTRAINT public test2 test2_pkey postgres
    2943; 2606 16560 CONSTRAINT public test test_pkey postgres
    [postgres@lxm test2]$
    [postgres@lxm test2]$ vim test.toc
    [postgres@lxm test2]$
    [postgres@lxm test2]$ more test.toc
    ;
    ; Archive created at 2020-06-16 12:43:38 CST
    ;     dbname: test
    ;     TOC Entries: 10
    ;     Compression: 0
    ;     Dump Version: 1.14-0
    ;     Format: TAR
    ;     Integer: 4 bytes
    ;     Offset: 8 bytes
    ;     Dumped from database version: 13beta1
    ;     Dumped by pg_dump version: 13beta1
    ;
    ;
    ; Selected TOC Entries:
    ;
    200; 1259 16545 TABLE public test postgres
    ;201; 1259 16551 TABLE public test2 postgres
    3076; 0 16545 TABLE DATA public test postgres
    ;3077; 0 16551 TABLE DATA public test2 postgres
    ;2945; 2606 16558 CONSTRAINT public test2 test2_pkey postgres
    2943; 2606 16560 CONSTRAINT public test test_pkey postgres
    [postgres@lxm test2]$
    
    
  • 데이터베이스 테스트 및 테이블 테스트 복구
    [postgres@lxm test2]$ pg_restore -U postgres -d postgres -Ft -L test.toc  -C -c --if-exists test_tar.dmp
    Password:
    [postgres@lxm test2]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# \l
                                 List of databases
       Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
    -----------+----------+----------+---------+-------+-----------------------
     postgres  | postgres | UTF8     | C       | C     |
     template0 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     template1 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     test      | postgres | UTF8     | C       | C     |
    (4 rows)
    
    postgres=# \c test
    You are now connected to database "test" as user "postgres".
    test=# \dt
            List of relations
     Schema | Name | Type  |  Owner
    --------+------+-------+----------
     public | test | table | postgres
    (1 row)
    
    test=# select count(*) from test;
     count
    -------
        10
    (1 row)
    
    test=# select * from test;
     id |  name
    ----+--------
      1 | 1name
      2 | 2name
      3 | 3name
      4 | 4name
      5 | 5name
      6 | 6name
      7 | 7name
      8 | 8name
      9 | 9name
     10 | 10name
    (10 rows)
    
    test=#
    
    

  • 방법 2

    [postgres@lxm test2]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# \l
                                 List of databases
       Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
    -----------+----------+----------+---------+-------+-----------------------
     postgres  | postgres | UTF8     | C       | C     |
     template0 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     template1 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     test      | postgres | UTF8     | C       | C     |
    (4 rows)
    
    postgres=# drop database test;
    DROP DATABASE
    postgres=# \q
    [postgres@lxm test2]$ pg_restore -U postgres -d postgres -Ft -t test  -C -c --if-exists test_tar.dmp
    Password:
    [postgres@lxm test2]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# \l
                                 List of databases
       Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
    -----------+----------+----------+---------+-------+-----------------------
     postgres  | postgres | UTF8     | C       | C     |
     template0 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     template1 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     test      | postgres | UTF8     | C       | C     |
    (4 rows)
    
    postgres=# \c test
    You are now connected to database "test" as user "postgres".
    test=# \dt
            List of relations
     Schema | Name | Type  |  Owner
    --------+------+-------+----------
     public | test | table | postgres
    (1 row)
    
    test=# \d+ test
                                             Table "public.test"
     Column |       Type        | Collation | Nullable | Default | Storage  | Stats target | Description
    --------+-------------------+-----------+----------+---------+----------+--------------+-------------
     id     | integer           |           | not null |         | plain    |              |
     name   | character varying |           |          |         | extended |              |
    Access method: heap
    
    test=# select count(*) from test;
     count
    -------
        10
    (1 row)
    
    

    # 복구 함수


    방법 1


    함수 만들기

    [postgres@lxm test_function]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# create database test template template0;
    CREATE DATABASE
    postgres=# \c test
    You are now connected to database "test" as user "postgres".
    test=# create table test(id int primary key , name varchar);
    CREATE TABLE
    test=# insert into test values(1,'a'),(2,'b');
    INSERT 0 2
    test=# CREATE OR REPLACE FUNCTION func_test()
    test-# RETURNS integer
    test-# AS
    test-# $BODY$
    test$# declare
    test$# BEGIN
    test$#
    test$# END;
    test$# $BODY$  LANGUAGE plpgsql;
    CREATE FUNCTION
    test=# \df
                             List of functions
     Schema |   Name    | Result data type | Argument data types | Type
    --------+-----------+------------------+---------------------+------
     public | func_test | integer          |                     | func
    (1 row)
    
    test=# \dt
            List of relations
     Schema | Name | Type  |  Owner
    --------+------+-------+----------
     public | test | table | postgres
    (1 row)
    
    test=# \q
    [postgres@lxm test_function]$ ls
    test_t.dmp
    [postgres@lxm test_function]$ rm -f test_t.dmp
    [postgres@lxm test_function]$ pg_dump -U postgres -Ft -f test_t.dmp test
    Password:
    [postgres@lxm test_function]$ ls
    test_t.dmp
    
    

    test 데이터베이스 삭제

    [postgres@lxm test_function]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# \l
                                 List of databases
       Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
    -----------+----------+----------+---------+-------+-----------------------
     postgres  | postgres | UTF8     | C       | C     |
     template0 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     template1 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     test      | postgres | UTF8     | C       | C     |
    (4 rows)
    
    postgres=# drop database test;
    DROP DATABASE
    postgres=# \q
    
    

    복구 함수 func_테스트 및 테이블 테스트

    [postgres@lxm test_function]$ pg_restore -Upostgres -d postgres -t test -P func_test\(\) -C -c --if-exists test_t.dmp
    Password:
    [postgres@lxm test_function]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# \l
                                 List of databases
       Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
    -----------+----------+----------+---------+-------+-----------------------
     postgres  | postgres | UTF8     | C       | C     |
     template0 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     template1 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     test      | postgres | UTF8     | C       | C     |
    (4 rows)
    
    postgres=# \c test
    You are now connected to database "test" as user "postgres".
    test=# \df
                             List of functions
     Schema |   Name    | Result data type | Argument data types | Type
    --------+-----------+------------------+---------------------+------
     public | func_test | integer          |                     | func
    (1 row)
    
    test=# \dt
            List of relations
     Schema | Name | Type  |  Owner
    --------+------+-------+----------
     public | test | table | postgres
    (1 row)
    
    test=# \q
    [postgres@lxm test_function]$
    
    
    

    방법 2

    [postgres@lxm test_function]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# \c test
    You are now connected to database "test" as user "postgres".
    test=# \df
                           List of functions
     Schema | Name | Result data type | Argument data types | Type
    --------+------+------------------+---------------------+------
    (0 rows)
    
    test=# \dt
            List of relations
     Schema | Name | Type  |  Owner
    --------+------+-------+----------
     public | test | table | postgres
    (1 row)
    
    test=# create table test2 (like test including all);
    CREATE TABLE
    test=#
    test=# CREATE OR REPLACE FUNCTION func_add(a int, b int)
    test-# RETURNS integer
    test-# AS
    test-# $BODY$
    test$# declare
    test$# BEGIN
    test$# return $1+$2;
    test$# END;
    test$# $BODY$  LANGUAGE plpgsql;
    CREATE FUNCTION
    test=#
    test=# CREATE OR REPLACE FUNCTION func_test()
    test-# RETURNS integer
    test-# AS
    test-# $BODY$
    test$# declare
    test$# BEGIN
    test$#
    test$# END;
    test$# $BODY$  LANGUAGE plpgsql;
    CREATE FUNCTION
    test=# \q
    
    
  • 백업
    [postgres@lxm test_function]$ pg_dump -U postgres -Ft -f test_tar.dmp  test
    
    
  • toc 파일 생성
    [postgres@lxm test_function]$ pg_restore -Upostgres -l -f test.toc test_tar.dmp
    [postgres@lxm test_function]$ ls
    test_tar.dmp  test.toc
    [postgres@lxm test_function]$ more test.toc
    ;
    ; Archive created at 2020-06-16 13:54:24 CST
    ;     dbname: test
    ;     TOC Entries: 10
    ;     Compression: 0
    ;     Dump Version: 1.14-0
    ;     Format: TAR
    ;     Integer: 4 bytes
    ;     Offset: 8 bytes
    ;     Dumped from database version: 13beta1
    ;     Dumped by pg_dump version: 13beta1
    ;
    ;
    ; Selected TOC Entries:
    ;
    202; 1255 16700 FUNCTION public func_add(integer, integer) postgres
    203; 1255 16701 FUNCTION public func_test() postgres
    200; 1259 16688 TABLE public test postgres
    201; 1259 16694 TABLE public test2 postgres
    3074; 0 16688 TABLE DATA public test postgres
    3075; 0 16694 TABLE DATA public test2 postgres
    
    
  • 테스트 데이터베이스 삭제
    [postgres@lxm test_function]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# drop database if exists test;
    DROP DATABASE
    postgres=# \q
    
    
  • 회복
    [postgres@lxm test_function]$ pg_restore -Upostgres -d postgres -L test.toc -C -c --if-exists test_tar.dmp
    Password:
    [postgres@lxm test_function]$ psql
    Password for user postgres:
    psql (13beta1)
    Type "help" for help.
    
    postgres=# \l
                                 List of databases
       Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
    -----------+----------+----------+---------+-------+-----------------------
     postgres  | postgres | UTF8     | C       | C     |
     template0 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     template1 | postgres | UTF8     | C       | C     | =c/postgres          +
               |          |          |         |       | postgres=CTc/postgres
     test      | postgres | UTF8     | C       | C     |
    (4 rows)
    
    postgres=# \c test
    You are now connected to database "test" as user "postgres".
    test=# \dt
            List of relations
     Schema | Name | Type  |  Owner
    --------+------+-------+----------
     public | test | table | postgres
    (1 row)
    
    test=# \df
                             List of functions
     Schema |   Name   | Result data type | Argument data types  | Type
    --------+----------+------------------+----------------------+------
     public | func_add | integer          | a integer, b integer | func
    (1 row)
    
    

    총결산


  • 실제 테스트 체험에 의하면 -P를 통해 파라미터가 있는 함수를 복구했는데 복구에 성공하지 못했다. 아마도 내 문법에 문제가 있는 것 같다. 비교해 보면 지정한 파라미터가 있는 함수만 복구하면 toc파일을 생성하고 편집하여 비교적 편리하게 할 수 있다.

    좋은 웹페이지 즐겨찾기