Postgres流式備份(4)配置新數據庫備份

配置文件初始化

在/etc/barman.d新建數據庫配置文件

touch /etc/barman.d/192.168.220.133.conf

文件內容如下

[192.168.220.133]
; Human readable description
description =  "PG10"

; 備份目錄
backup_directory = /home/backup/192.168.220.133

; PostgreSQL connection string (mandatory)
conninfo = host=192.168.220.133 user=barman dbname=postgres

; PostgreSQL streaming connection string
; To be used by pg_basebackup for backup and pg_receivexlog for WAL streaming
; NOTE: streaming_barman is a regular user with REPLICATION privilege
streaming_conninfo = host=192.168.220.133 user=streaming_barman

; Backup settings (via pg_basebackup)
backup_method = postgres
;streaming_backup_name = barman_streaming_backup

; WAL streaming settings (via pg_receivexlog)
streaming_archiver = on
slot_name = barman
;streaming_archiver_name = barman_receive_wal
;streaming_archiver_batch_size = 50

; PATH setting for this server
path_prefix = "/usr/pgsql-10/bin"

對於備份目錄,需設置barman對文件夾的權限

chown -R barman:barman /home/backup/192.168.220.133
#只允許PG賬戶讀寫
chmod -R 700 /home/backup/192.168.220.133

建立數據庫用戶

在數據庫服務器上執行命令,創建用戶

su postgres
createuser -s -P barman

在備份服務器上配置用戶名密碼

barman默認使用本用戶根目錄下的.pgpass文件存儲密碼,要求該文件只允許barman用戶讀寫

su barman
touch ~/.pgpass
chmod 0600 ~/.pgpass

文件內配置示例

內容格式爲:[數據庫IP]:[數據庫端口]:[]數據庫:[用戶]:[密碼],除密碼外可用*匹配任何內容

詳見:https://www.postgresql.org/docs/current/libpq-pgpass.html

192.168.220.133:5432:*:barman:barman

測試連接

在備份服務器下測試是否能夠連接數據庫

psql -c 'SELECT version()' -U barman -h 192.168.220.133 postgres

如過通過,在數據庫配置文件中設置

conninfo = host=192.168.220.133 user=barman dbname=postgres

數據庫服務器設置

配置pg_hba.conf

允許來自備份服務器的用戶和複製用戶通過密碼訪問,例如

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             0.0.0.0/0               md5
host    replication     all             ::1/128                 ident

reload數據庫

配置postgresql.conf

# 啓用wal
wal_level = replica
# 設置WAL發送者數量,取決於接受者的數量
max_wal_senders = 10
# 服務器最大複製槽數量
max_replication_slots = 10

重啓數據庫服務器

添加複製用戶streaming_barman

su postgres
createuser -P --replication streaming_barman

在備份服務器將該用戶相關連接信息配置到.pgpass

su barman
vi ~/.pgpass

例如

192.168.220.133:5432:*:barman:barman
192.168.220.133:5432:*:streaming_barman:barman

在備份服務器測試複製用戶是否可用

psql -U streaming_barman -h 192.168.220.133 -c "IDENTIFY_SYSTEM" replication=1

如過通過,在數據庫配置文件中設置

conninfo = host=192.168.220.133 user=streaming_barman

SSH互信

備份服務器的barman用戶和數據庫服務器的postgres用戶之間需要做SSH互信,以實現以下功能

  • 數據庫服務器通過archive_command將歸檔日誌備份到備份服務器
  • 備份服務器通過rsync備份數據庫服務器文件

需要對repmgr集羣中的每個節點間配置SSH互信

數據庫服務器192.168.220.133下執行

su postgres
# 生成祕鑰到用戶主目錄下的.ssh文件夾下
ssh-keygen -t rsa
# 將祕鑰拷貝到遠程機器
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
# 驗證是否授權完成
ssh [email protected] date

對於192.168.220.136

su barman
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
# 驗證是否授權完成
ssh [email protected] date

備份配置

流複製

Barman使用pg_receivewal(PG10)或pg_receivexlog(PG9)複製WAL流。

pg_receivewal支持PG9.3及更高版本,同一臺服務器上可以部署多個版本的pg_receivewal/ pg_receivexlog,並使用path_prefix配置指定具體版本

PG數據庫可通過複製槽來保障所有備服務器收到WAL之前不會刪除WAL

設置數據庫配置文件

streaming_archiver = on
slot_name = barman

通過命令在數據庫服務器上添加複製槽

barman receive-wal --create-slot 192.168.220.133

正常將返回

Creating physical replication slot 'barman' on server 'pg'
Replication slot 'barman' created

如果停止複製,需要將複製槽刪除,否則數據庫將一直保留wal文件,導致wal文件過多。刪除複製槽命令如下:

barman receive-wal --drop-slot 192.168.220.133

WAL歸檔

Barman通過Postgres的archive_command實現歸檔備份

在備份服務器,查詢WALs目錄位置

barman show-server 192.168.220.133 |grep incoming_wals_directory

得到wal歸檔的incoming目錄:/home/backup/192.168.220.133/incoming

數據庫服務器需要安裝rsync
修改數據庫postgresql.conf配置


wal_level = 'replica'
# 啓用歸檔模式
archive_mode = on
# 歸檔執行的命令%p表示歸檔文件目錄,%f表示歸檔文件名稱
archive_command = 'rsync -a %p [email protected]:/home/backup/192.168.220.133/incoming/%f'
# 設置最大每60秒歸檔一次,防止WAL過少導致長時間未歸檔
archive_timeout = 60

重啓數據庫服務

驗證歸檔配置

su barman
barman switch-wal --force --archive 192.168.220.133 --archive-timeout 65

全量備份

Barman使用pg_basebackup進行全量基本備份

  • 對於PG9.2需要使用9.2版本的pg_basebackup,
  • 對於PG9.3及更高,可以大於等於該版本的pg_basebackup
    可通過path_prefix指定使用目錄下的pg_basebackup版本

備份服務器設置

backup_method = postgres

檢查配置是否正常

barman check 192.168.220.133

開始備份

barman backup 192.168.220.133
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章