11.5 pgBackRest
pgBackRest는 PostgreSQL의 가장 널리 쓰이는 운영급 백업 도구입니다. Crunchy Data와 커뮤니티가 유지보수하는 BSD 라이선스 오픈소스. 풀·증분·차분 백업, 병렬 처리, WAL archive, 외부 저장소(S3·Azure·GCS·SFTP)·암호화·체크섬·자체 복구 시나리오 검증을 모두 지원합니다.
핵심 기능
| 기능 | 메모 |
|---|---|
| 풀 / 증분 / 차분 백업 | 페이지 단위 증분 |
| 병렬 처리 | --process-max |
| 외부 저장소 | S3 / Azure Blob / GCS / SFTP / 로컬 |
| 압축 | gzip / lz4 / zstd / bz2 |
| 암호화 | AES-256-CBC |
| 체크섬 검증 | 백업·복원 양쪽 |
| 보존 정책 | 풀 N개·일 단위 |
| timeline 인식 PITR | 시점·LSN·xid |
| async archive_command | 처리량 ↑ |
| stanza 단위 격리 | 한 호스트가 여러 클러스터 백업 |
설치
# RHEL/Rocky (PGDG)
sudo dnf install -y pgbackrest
# Debian/Ubuntu
sudo apt-get install -y pgbackrest기본 설정 — /etc/pgbackrest.conf
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=3
repo1-retention-diff=14
compress-type=zst
compress-level=3
process-max=4
log-level-console=info
log-level-file=detail
[main]
pg1-path=/var/lib/pgsql/17/data
pg1-port=5432
pg1-user=postgres| 섹션 | 의미 |
|---|---|
[global] | 모든 stanza에 공통 |
[main] | stanza 이름 — 임의의 식별자 |
stanza 만들기
sudo -u postgres pgbackrest --stanza=main stanza-create
sudo -u postgres pgbackrest --stanza=main checkcheck는 archive_command가 정상 동작하는지 검증합니다.
PostgreSQL 측 설정
# postgresql.conf
archive_mode = on
archive_command = 'pgbackrest --stanza=main archive-push %p'reload.
백업
# 풀 백업
sudo -u postgres pgbackrest --stanza=main backup --type=full
# 증분 (마지막 백업 대비)
sudo -u postgres pgbackrest --stanza=main backup --type=incr
# 차분 (마지막 풀 대비)
sudo -u postgres pgbackrest --stanza=main backup --type=diff기본은 마지막 풀 백업이 없으면 풀, 있으면 증분.
백업 목록
sudo -u postgres pgbackrest --stanza=main infostanza: main
status: ok
cipher: none
db (current)
wal archive min/max (17): 000000010000000000000005/000000010000000000000023
full backup: 20260520-020000F
timestamp start/stop: 2026-05-20 02:00:00 / 2026-05-20 02:08:23
database size: 240GB, backup size: 240GB
repository size: 53.4GB, repository backup size: 53.4GB
incr backup: 20260521-020000F_20260521-020000I
timestamp start/stop: 2026-05-21 02:00:00 / 2026-05-21 02:01:12
database size: 240GB, backup size: 1.2GB
repository size: 53.7GB, repository backup size: 280MB복원
# 가장 최근 백업으로 전체 복원
sudo -u postgres pgbackrest --stanza=main restore
# 특정 백업
sudo -u postgres pgbackrest --stanza=main restore --set=20260520-020000F
# PITR
sudo -u postgres pgbackrest --stanza=main restore \
--type=time --target='2026-05-23 09:50:00' \
--target-action=promote--type | 의미 |
|---|---|
default | 백업 시점 직후까지 |
immediate | base 일관성 회복 직후 |
time | --target=... 시각 |
xid | 트랜잭션 ID |
lsn | LSN |
name | named restore point |
PGDATA가 비어 있어야 복원 가능합니다. --delta 옵션은 차이만 갱신.
외부 저장소
S3
[global]
repo1-type=s3
repo1-s3-bucket=my-pg-backups
repo1-s3-region=ap-northeast-2
repo1-s3-endpoint=s3.ap-northeast-2.amazonaws.com
repo1-s3-key=AKIAxxxxxxxxxxxxxxxx
repo1-s3-key-secret=xxxxxxxxxxxxxxxxxxxx
repo1-path=/pgbackrestAzure Blob
repo1-type=azure
repo1-azure-account=mystorageaccount
repo1-azure-container=pgbackups
repo1-azure-key=xxxxxxxxxxxxxxxxxxxxxxxx다중 repo
[global]
repo1-path=/var/lib/pgbackrest # 로컬 SSD
repo2-type=s3 # S3 오프사이트
repo2-s3-bucket=...로컬 + S3 동시. 빠른 복원은 로컬, 장기 보존은 S3.
보존 정책
repo1-retention-full=3 # 풀 백업 3개 보관
repo1-retention-full-type=count # 또는 'time'
repo1-retention-diff=14 # 차분 14개
repo1-retention-archive=14 # archive WAL 보존 일수pgbackrest expire로 명시적 정리도 가능하며, 자동 expire는 backup 명령에 포함됩니다.
검증 — verify
sudo -u postgres pgbackrest --stanza=main verify저장된 백업의 체크섬을 검증해 저장 도중의 손상을 검출합니다. 운영 정기 작업에 포함합니다.
운영 자동화
# crontab — sudo -u postgres
30 2 * * 0 pgbackrest --stanza=main --type=full backup
30 2 * * 1-6 pgbackrest --stanza=main --type=incr backup
0 4 * * 0 pgbackrest --stanza=main verify일요일 풀 + 평일 증분 + 일요일 verify. Cron의 결과는 PostgreSQL 로그 또는 외부 모니터링으로 통합합니다.
자주 쓰는 옵션
| 옵션 | 의미 |
|---|---|
--process-max=N | 병렬 워커 수 |
--compress-type=zst | 압축 알고리즘 |
--start-fast | 시작 즉시 체크포인트 |
--archive-async | archive_command 비동기 (처리량 ↑) |
--repo1-cipher-type=aes-256-cbc | 암호화 |
--repo1-cipher-pass='...' | 암호화 키 |
async archive_command는 archive_command가 WAL을 큐에 쌓고 별도 프로세스가 push하는 방식으로, TPS 높은 시스템에서 archive lag을 줄입니다.
archive-async=y
spool-path=/var/spool/pgbackrestdelta 복원
--delta는 PGDATA에 일부 파일이 있어도 변경 부분만 갱신. 큰 클러스터 재복원 시 시간 절약합니다.
pgbackrest --stanza=main --delta restore체크섬 비교로 이미 같은 파일은 건너뜁니다. 단, 잘못 쓰면 일관성이 깨질 위험이 있으니 운영자가 동작을 명확히 이해한 뒤 사용합니다.
모니터링
# 정상 stanza
pgbackrest --stanza=main info --output=jsonJSON 출력을 Prometheus exporter(직접 제작 또는 pgbackrest-exporter)로 노출합니다. 알람:
- 마지막 백업이 24시간 이상 안 됨
- 백업 실패
- WAL archive lag 큼
운영 안티패턴
| 안티패턴 | 위험 |
|---|---|
pgbackrest backup만 돌리고 verify·복원 검증 안 함 | 사고 시 복원 실패 |
| repo가 같은 호스트 디스크에만 | 호스트 사고 시 같이 사라짐 |
repo1-retention-archive가 풀 백업보다 짧음 | PITR window가 base 백업 사이클보다 좁아짐 |
| 같은 stanza 이름을 두 클러스터에서 사용 | 서로 덮어씀 |
| postgres user가 pgBackRest 명령 못 실행 | 권한 문제 |
정리
pgBackRest는 운영 PostgreSQL 백업의 표준 도구입니다. stanza 단위로 클러스터를 분리하고, 다중 repo로 로컬과 S3를 동시에 둘 수 있으며, 풀·증분·차분 백업과 WAL archive, PITR을 하나로 통합 관리합니다. --archive-async로 archive lag을 최소화합니다.
운영에서는 verify, 복원 검증, 외부 저장소, 모니터링을 한 세트로 갖춥니다. 보존 정책은 archive 보존을 풀 보존보다 길게 잡아야 오래된 풀에서 시작하는 PITR이 막히지 않습니다.