본문 바로가기
  • A space that records me :)
DataBase/PostgreSQL

[PostgreSQL] pg_basebackup vs pg_rewind

by yjkim_97 2021. 11. 22.

[2021.11.22]

PostgreSQL의 백업 명령 중 pg_basebackup과 pg_rewind 간의 차이에 대해서 알아보았다.


1. 정의

pg_basebackup

pg_basebackup is used to take a base backup of a running PostgreSQL database cluster.
...

Backups are always taken of the entire database cluster; it is not possible to back up individual databases or database objects.
...
The backup will include all files in the data directory and tablespaces, including the configuration files and any additional files placed in the directory by third parties, except certain temporary files managed by PostgreSQL.

  • 현재 가동중인 PostgreSQL 데이터베이스 클러스터를 기본 백업할 때 사용한다.
  • 백업은 항상 데이터베이스 클러스터 단위로 백업된다.
  • 데이터 디렉토리와 테이블 스페이스 등 모든 파일을 백업한다. (conf 파일, 기타 사용자가 추가한 파일 등 모두..)

 

pg_rewind

pg_rewind is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.
...
Unlike taking a new base backup or using a tool like rsync, pg_rewind does not require comparing or copying unchanged relation blocks in the cluster.
...
When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence.
...
pg_rewind will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates.
  • 장애가 발생한 노드를 다른 노드를 복사하여 동기화 하기 위한 도구이다. 일반적으로는 새 master 서버를 아카이빙하는 대기 서버로 failover 후 다시 온라인 모드로 전환하는 것이다.
  • rsync와 같은 기존 백업 도구와 달리, pg_rewind는 클러스터간의 변경된 블록만 복사한다.
  • pg_rewind가 끝난 후, 대상 서버가 다시 시작되면 아카이브 복구가 시작되어 분기 지점 이전의 마지막 체크포인트부터 WAL 파일을 가져온다. 
  • pg_rewind 실행 중 직접 쓸수 없는 (ex. SSH 키 인증서 파일 등) 파일이 존재하면 도중에 실패 처리 된다.
    SSH key 인증서 파일같은 쓰기가 제어된 파일은 data 디렉토리 내부에 존재하면 안된다.

PostgreSQL에서는 pg_rewind 실패 시 pg_basebackup을 진행하는 것을 추천한다.

 

2. 결론

결론은 pg_basebackup은 전체를 백업하는 것이고 pg_rewind는 싱크를 맞추는 것이다.

(pg_basebackup은 data 전체 복사, pg_rewind는 싱크가 다른 파일만 복사)