Postgresql/백업 및 복구

drop 시점을 모르는 상태에서의 특정 테이블 복구

dbavayne 2025. 3. 22. 13:34

상황 요약

  • 상황: 초기 데이터 적재 후 풀 백업 완료 → 작업자 실수로 테이블 DROP → DROP 시각 모름 → 무중단 복구 필요.
  • 목표: DB 정지 없이 DROP 직전 시점으로 테이블 복구.
  • 제약: sequence 복구도 따로 필요함

  • 초기 데이터 적재
CREATE TABLE test_table (
    id SERIAL PRIMARY KEY,
    name TEXT
);

INSERT INTO test_table (name) VALUES ('Alice'), ('Bob'), ('Charlie');
SELECT * FROM test_table;
  • full backup 완료 ( 테이블 별로 backup )
pg_dump -Fc -f full_backup.dump -d mydb
  • seqeunce 백업 확인
postgres@lkmpg:/backups/dump$ pg_restore -l full_backup.dump |grep  SEQUENCE
214; 1259 57395 SEQUENCE public test_table_id_seq postgres
3398; 0 0 SEQUENCE OWNED BY public test_table_id_seq postgres
3399; 0 0 SEQUENCE SET public test_table_id_seq postgres 

1. 스키마 복원

pg_restore -d mydb --schema-only -t test_table full_backup.dump

postgres@lkmpg:/backups/dump$ pg_restore -d mydb --schema-only -t test_table full_backup.dump
postgres@lkmpg:/backups/dump$ psql mydb
psql (15.12 (Ubuntu 15.12-1.pgdg24.04+1))
Type "help" for help.

mydb=# \d
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+----------
 public | test_table | table | postgres
(1 row)

2. 시퀀스 복원

pg_restore -d mydb -t test_table_id_seq full_backup.dump

postgres@lkmpg:/backups/dump$ pg_restore -d mydb -t test_table_id_seq full_backup.dump
postgres@lkmpg:/backups/dump$ psql mydb
psql (15.12 (Ubuntu 15.12-1.pgdg24.04+1))
Type "help" for help.

mydb=# \d
                List of relations
 Schema |       Name        |   Type   |  Owner
--------+-------------------+----------+----------
 public | test_table        | table    | postgres
 public | test_table_id_seq | sequence | postgres
(2 rows)

3. 데이터 복원

pg_restore -d mydb --data-only -t test_table full_backup.dump

postgres@lkmpg:/backups/dump$ pg_restore -d mydb --data-only -t test_table full_backup.dump
postgres@lkmpg:/backups/dump$ psql mydb
psql (15.12 (Ubuntu 15.12-1.pgdg24.04+1))
Type "help" for help.

mydb=# select * from test_table;
 id |  name
----+---------
  1 | Alice
  2 | Bob
  3 | Charlie
(3 rows)