barman

拓扑:

 

注意:

      1.barman的安装需要使用postgresql的模块,尽量保证pg1 ,barman-server,backup-pg上的postgresql版本一致。

       

 

步骤:

1 在pg1上安装postgresql-11
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum install postgresql11
yum install postgresql11-server
/usr/pgsql-11/bin/postgresql-11-setup initdb

此时会生成一个postgres的系统用户,修改密码和用户主目录
vi /etc/passwd
将postgres用户的主目录改为/home/postgresql

vi pg_hba.conf
追加下面内容
host all all 192.168.10.118/32 trust
vi postgresql.conf
打开注释
listen_addresses = '*'

systemctl start postgresql-11


su - postgres
psql
postgres = # alter user postgres with password 'postgres';
postgres = \q

测试一下,此时用客户端连接一下数据库是否正常,可能需要开放防火墙端口,或者修改pg_hba.conf

2 同理,在backup-pg上安装postgresql-11

3 在barman服务器上安装Barman
首先需要安装以下存储库:
Enterprise Linux(EPEL)存储库的额外包
yum -y install epel-release

PostgreSQL全球开发组RPM存储库
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql11
运行此命令以安装Barman
yum -y install barman


4 建立ssh互联
需要确保:
4.1用户postgres可以从pg1远程连接到barman-server,以下为示范。
4.2用户barman可以从barman-server远程连接到pg1
4.3用户barman可以从barman-server远程连接到backup-pg

从pg1切换到用户postgres,如果它还不是当前用户:
sudo su - postgres
运行以下命令以生成SSH密钥对:
ssh-keygen -t rsa
生成密钥后,将在postgres用户的主目录下创建一个.ssh目录,其中包含密钥。
现在需要将SSH公钥复制到barman-server上barman用户的.ssh目录下的authorized_keys文件中。

运行以下命令输出postgres用户的公钥内容:
cat ~/.ssh/id_rsa.pub 复制输出的内容。

切换到连接到barman-server服务器的控制台并切换到用户barman:
sudo su - barman
运行以下命令以创建.ssh目录,设置其权限,将公钥内容复制到该authorized_keys文件,最后使该文件可读写:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
(测试一下:切换回pg1(postgres用户)下,测试连接:ssh barman@192.168.10.118)

同理,操作4.2,4.3并测试。
(免密登录可能会遇到的问题参考:https://my.oschina.net/ihsxin/blog/1531838)


5 配置Barman进行备份
切换到barman服务器
vi /etc/barman.conf
打开注释,修改参数
compression = gzip
immediate_checkpoint = true
basebackup_retry_times = 3
basebackup_retry_sleep = 30
last_backup_maximum_age = 1 DAYS
追加下面内容
[pg1]
description = "Main DB Server"
ssh_command = ssh postgres@192.168.10.26
conninfo = host=192.168.10.26 user=postgres
retention_policy_mode = auto
retention_policy = RECOVERY WINDOW OF 7 days
wal_retention_policy = main

6 修改pg1的postgresql.conf文件
(barman-server)sudo su - barman
运行此命令以找到传入的备份目录:
barman show-server pg1 | grep incoming_wals_directory
可能输出如下的值:
barman show-server command outputincoming_wals_directory: /var/lib/barman/main-db-server/incoming
记下值incoming_wals_directory; 在这个例子中,它是/var/lib/barman/main-db-server/incoming
现在切换到pg1控制台。
vi postgresql.conf
wal_level = replica
archive_mode = on
archive_command = 'rsync -a %p barman@192.168.10.118:/var/lib/barman/main-db-server/incoming/%f'
重启pg1的PostgreSQL:systemctl restart postgresql-11


7 测试barman
barman check pg1
应该输出类似:
barman check command outputServer main-db-server:
PostgreSQL: OK
archive_mode: OK
wal_level: OK
archive_command: OK
continuous archiving: OK
directories: OK
retention policy settings: OK
backup maximum age: FAILED (interval provided: 1 day, latest backup age: No available backups)
compression settings: OK
minimum redundancy requirements: OK (have 0 backups, expected at least 0)
ssh: OK (PostgreSQL server)
not in recovery: OK
(可以查看barman日志,定位问题)
barman list-server
应该输出类似:
pg1 - Main DB Server
第8步 - 创建第一个备份
barman backup main-db-server
应该输出类似:
Starting backup for server main-db-server in /var/lib/barman/main-db-server/base/20151111T051954
Backup start at xlog location: 0/2000028 (000000010000000000000002, 00000028)
Copying files.
Copy done.
Asking PostgreSQL server to finalize the backup.
Backup size: 26.9 MiB. Actual size on disk: 26.9 MiB (-0.00% deduplication ratio).
Backup end at xlog location: 0/20000B8 (000000010000000000000002, 000000B8)
Backup completed
Processing xlog segments for main-db-server
Older than first backup. Trashing file 000000010000000000000001 from server main-db-server
000000010000000000000002
000000010000000000000002.00000028.backup
ls -l /var/lib/barman 查看备份文件位置
barman list-backup pg1 列出备份
barman show-backup pg1 backup-id 将显示一组详细信息;backup-id为备份id
第9步 - 计划备份
在barman-server上以barman用户身份运行此命令
crontab -e
追加下面内容
30 23 * * * /usr/bin/barman backup main-db-server
* * * * * /usr/bin/barman cron
解释:
第一个命令将在每晚11:30 PM 运行pg1的完整备份。
第二个命令将每分钟运行一次,并对WAL文件和基本备份文件执行维护操作
第10步 - 模拟“灾难”
删除pg1上的表或者数据库
步骤11 - 恢复或迁移到远程服务器
首先,停止backup-pg的PostgreSQL服务
在barman-server下:
barman show-backup pg1 latest 获取backup-id 和Begin time
执行备份恢复:
barman recover --target-time "Begin time" --remote-ssh-command "ssh postgres@192.168.10.119" pg1 backup-id /var/lib/pgsql/11/data
可能输出如下的值:
Starting remote restore for server main-db-server using backup backup-id
Destination directory: /var/lib/pgsql/9.4/data
Doing PITR. Recovery target time: Begin time
Copying the base backup.
Copying required WAL segments.
Generating recovery.conf
Identify dangerous settings in destination directory.

IMPORTANT
These settings have been modified to prevent data losses

postgresql.conf line 207: archive_command = false

Your PostgreSQL server has been successfully prepared for recovery!
检查是否恢复:
在barman-server下:
systemctl start postgresql-11

--------------------------------------------------------------

streaming方式
1 在pg1上新增数据库超级用户streaming_barman,配置pg_hba.conf
host replication streaming_barman 192.168.10.118/32 trust
重启pg1的数据库
barman服务器上确认 streaming_barman 流复制是否正常
psql -h 192.168.10.26 -U streaming_barman -c "IDENTIFY_SYSTEM" replication=1
2 在pg1上设置postgresql.conf
max_wal_senders=2
max_replication_slots =2
重启pg1的数据库
3 barman服务器 修改配置barman.conf
[main-db-server]
description = "dn1 PostgreSQL Database (Streaming-Only)"
conninfo = host=192.168.10.26 user=barman dbname=postgres
streaming_conninfo = host=192.168.10.26 user=streaming_barman
backup_method = postgres
streaming_archiver = on
slot_name = barman
4 检查
barman show-server all
barman receive-wal --create-slot main-db-server
barman cron
barman check main-db-server
后续备份及还原过程和ssh方式一样,到上面第8步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值