> For the complete documentation index, see [llms.txt](https://docs.tibero.com/tmaxopensql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tibero.com/tmaxopensql/installation/configuration/barman.md).

# Barman

## 개요

OpenSQL의 효율적인 백업 및 복구를 지원하는 Barman 구성 및 실행방법에 대해 설명합니다.

***

## 환경 설정

### Barman

Streaming Replication을 통한 백업을 사용하는 경우 `pg_basebackup`, `pg_receivewal` 등 PostgreSQL 클라이언트 유틸리티가 필요합니다. OpenSQL 패키지를 통해 설치합니다. PostgreSQL 서비스는 기동하지 않습니다.

```bash
$ . ./setenv.sh `pwd`
$ ./install.sh postgresql
```

PostgreSQL 유틸리티를 사용자 `barman` 이 사용할 수 있도록 경로 설정합니다.

```
su - barman
```

```bash
$ echo 'export PATH=$PATH:$PG_HOME/bin' >> ~/.bashrc
$ source ~/.bashrc
```

* /etc/barman.conf

```
; Main configuration file

[barman] # 글로벌 환경 구성, OS 사용자 이름 및 barman 데이터와 로그를 저장할 경로 등을 지정

; System user
barman_user = barman
configuration_files_directory = /etc/barman.d

; Main directory
barman_home = /var/lib/barman

log_file = /var/log/barman/barman.log
log_level = INFO
```

* /etc/barman.d/opensql.conf

```
[opensql] # 서버 이름
cluster = opensql   # 후술할 모듈들과 같은 구성임을 나타내기 위한 태그
conninfo = host=node1 port=5432 user=opensql dbname=opensql
streaming_conninfo = host=node1 port=5432 user=opensql_streaming dbname=opensql
backup_method = postgres
streaming_archiver = on
slot_name = barman

; conninfo 사용자 권한 요구사항
; user=opensql          : pg_monitor 또는 superuser 권한 필요
; user=opensql_streaming: REPLICATION 권한 필요
; pg_hba.conf에서 barman 호스트의 접근을 허용해야 합니다.

[pg-1]    # 모델 이름, Patroni REST API에서 제공하는 멤버 이름과 일치해야 함
cluster = opensql   # 이 모델이 종속된 서버를 구분하기 위한 태그
model = true        # 서버 정의가 아닌, 서버에 종속되어 일부 옵션을 오버라이딩 하는 '모델'임을 명시
conninfo = host=node1 port=5432 user=opensql dbname=opensql
streaming_conninfo = host=node1 port=5432 user=opensql_streaming dbname=opensql

[pg-2]    # 모델 이름, Patroni REST API에서 제공하는 멤버 이름과 일치해야 함
cluster = opensql   # 이 모델이 종속된 서버를 구분하기 위한 태그
model = true        # 서버 정의가 아닌, 서버에 종속되어 일부 옵션을 오버라이딩 하는 '모델'임을 명시
conninfo = host=node2 port=5432 user=opensql dbname=opensql
streaming_conninfo = host=node2 port=5432 user=opensql_streaming dbname=opensql

[pg-3]    # 모델 이름, Patroni REST API에서 제공하는 멤버 이름과 일치해야 함
cluster = opensql   # 이 모델이 종속된 서버를 구분하기 위한 태그
model = true        # 서버 정의가 아닌, 서버에 종속되어 일부 옵션을 오버라이딩 하는 '모델'임을 명시
conninfo = host=node3 port=5432 user=opensql dbname=opensql
streaming_conninfo = host=node3 port=5432 user=opensql_streaming dbname=opensql

```

### Barman Agent

Primary - Replica 로 구성된 PostgreSQL 클러스터에서 Primary 노드에 Streaming 연결을 맺고 WAL을 스트리밍 받기 위해 Barman은 Primary 노드로의 접속 정보를 필요로 합니다.

Patroni의 REST API를 활용하면 Switchover 및 Failover가 발생하는 경우에도 클러스터의 정보 및 Primary 노드로의 접속 정보를 동적으로 가져올 수 있습니다.

Barman 자체적인 Configuration Switch 기능과 연동하여, PostgreSQL 클러스터의 리더 노드에 변경이 생긴 경우에도 Barman이 Streaming Connection을 새로운 Primary 노드로 맺기 위한 관리 서버 Barman Agent를 제공합니다.

#### 필수 Python 패키지 설치

Barman Agent 관련 파일(`server.py`, `config.yml`, `requirements.txt`, `barman-agent.service`)은 OpenSQL 패키지의 `barman-agent/` 디렉토리에 포함되어 있습니다.

```bash
$ pip install -r requirements.txt
```

#### 실행

```bash
# 셸에서 직접 실행하고자 하는 경우
## Barman을 실행할 수 있는 권한이 있는 사용자여야 한다.
## Barman 기본 환경 설정의 [barman] 섹션 barman_user 필드
$ python3 server.py &

# systemd 서비스로 등록하는 경우
## barman-agent 실행파일과 config.yml 파일의 경로는 systemd 서비스 파일에 정의된 기본값이다.
## 필요에 따라 해당 파일들의 경로 또는 실행할 사용자를 서비스 파일을 수정하여 변경할 수 있다.
$ sudo chmod 755 server.py
$ sudo cp server.py /usr/local/bin/barman-agent

$ sudo cp config.yml /var/lib/barman/config.yml  

$ sudo cp barman-agent.service /usr/lib/systemd/system/.
$ sudo systemctl daemon-reload
$ sudo systemctl enable barman-agent
$ sudo systemctl start barman-agent

```

Barman Agent는 실행 시 config 파일 경로를 인자로 받습니다. systemd 서비스 파일의 기본 경로는 `/var/lib/barman/config.yml`입니다. 해당 설정파일의 예시 및 설정할 수 있는 항목들에 대한 설명은 아래와 같습니다.

```yaml
listen: "192.168.0.100"   ## Optional. 바인딩할 인터페이스 주소. 기본값: 0.0.0.0 (모든 인터페이스 허용)
port: 8080                ## Optional. 바인딩할 TCP 포트 번호. 기본값: 8080
cluster: opensql          ## 필수. /etc/barman.d/opensql.conf 의 서버 이름([opensql])과 일치해야 합니다.
patroni:                  ## 필수. Patroni 노드들의 REST API 엔드포인트를 리스트로 입력합니다.
  - node1:8008
  - node2:8008
  - node3:8008
```

* `listen` : 항목이 없는 경우 기본 `0.0.0.0` 값이 사용됩니다.
* `port` : 항목이 없는 경우 기본 `8080` 값이 사용됩니다.
* `cluster` : 필수 항목으로 Barman 환경 구성에서 설정한 서버 이름과 일치해야 한다. `barman config-switch` 명령을 실행할 때 참조됩니다.
* `patroni` : 필수 항목으로 YAML 리스트 형식으로 접근 가능한 Patroni 서버들의 REST API 엔드포인트들을 입력합니다. 요청을 받고 최신 Patroni 클러스터 정보를 가져오기 위해 참조되며 Patroni 서버로 보내는 HTTP 요청은 2초의 Timeout을 가집니다. 실패한 Endpoint는 제외되고 마지막으로 성공한 Endpoint를 캐시하여 사용합니다.

### Patroni

Barman에서 사용할 Replication Slot을 지정합니다. 추가적으로 Barman Agent를 같이 배포하는 경우 Patroni에서 Role Change가 발생했을 때 실행할 Callback을 지정해야 합니다.

* patroni.yml

  ```yaml
  name: pg-1 ## Patroni 멤버 이름. Barman 환경구성 파일에 정의한 모델 이름([pg-1], [pg-2], ...)과 일치해야 한다.
  bootstrap:
    dcs:
      slots:
        barman:
          type: physical
  postgresql:
    callbacks:
      on_role_change: "curl 'http://{barman-host}:8080/renew_config'"
  ```

{% hint style="info" %}
`bootstrap.dcs.slots`는 클러스터 초기 부트스트랩 시에만 적용됩니다. 이미 운영 중인 클러스터에 Replication Slot을 추가하려면 `patronictl edit-config`를 사용하십시오.
{% endhint %}

***

## 실행

Streaming Replication 실행

```bash
$ barman receive-wal opensql ## Foreground 프로세스로 receive-wal 시작
Starting receive-wal for server opensql

$ barman cron                ## barman receive-wal 을 실행하는 Cron Job을 등록
Starting WAL archiving for server opensql
Starting streaming archiver for server opensql

```

`barman cron`은 주기적으로 실행되어야 합니다. crontab에 등록합니다.

```bash
$ which barman   ## barman 실행 파일 경로 확인
/usr/local/bin/barman

$ crontab -e -u barman
## 아래 내용 추가 (경로는 위 which barman 결과로 대체)
* * * * * /usr/local/bin/barman cron
```

설정이 완료되면 `barman check` 명령으로 연결 상태, Replication Slot, WAL 스트리밍 등을 검증합니다.

```bash
$ barman check opensql
```

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.tibero.com/tmaxopensql/installation/configuration/barman.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
