配置文件初始化
在/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 barman@192.168.220.139
# 验证是否授权完成
ssh barman@192.168.220.139 date
对于192.168.220.136
su barman
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@192.168.220.133
# 验证是否授权完成
ssh postgres@192.168.220.133 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 barman@192.168.220.139:/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