Postgres流式备份(6)备份和恢复

备份

定时维护

定时维护,强制执行保留策略、WAL文件管理
Barman如通过rpm安装将自动添加定时配置文件/etc/cron.d/barman,内容如下,设置每分钟进行一次定时维护

# m h  dom mon dow   user     command
  * *    *   *   *   barman   [ -x /usr/bin/barman ] && /usr/bin/barman -q cron

全量备份

手动执行备份

# 备份133,133为配置的服务名称
barman backup 133

如果备份报错:WAL archive: FAILED (please make sure WAL shipping is setup)
在数据库上执行

select pg_switch_xlog() ; -- for 9.6 and earlier 
or
select pg_switch_wal() ; -- for 10

在barman上执行

# 执行维护,强制开始收取WAL
barman cron; 
# 检查是否可以开始备份
barman check 133;
# 开始备份
barman back_up 133;
# 备份所有服务
barman backup all

可以使用cron配置定时执行备份,修改/etc/cron.d/barman,添加全量定时备份

# m h  dom mon dow   user     command
  0 1    *   *   *   barman   [ -x /usr/bin/barman ] && /usr/bin/barman -q backup all
重新加载定时配置
systemctl reload crond

检查当前流复制状态

barman replication-status 133

恢复

查询已有备份

# 133为配置的服务名称
barman list-backup 133

返回结果例如

133 20190104T012032 - Fri Jan  4 01:20:39 2019 - Size: 48.6 MiB - WAL Size: 0 B
133 20190103T232839 - Thu Jan  3 23:28:42 2019 - Size: 48.5 MiB - WAL Size: 320.0 MiB

第二列为backup id,第四列为备份时间

本机还原

需要先安装与备份数据库相同版本的PostgreSQL

Barman需要对还原的目标文件夹具备权限

chown -R barman:barman /home/pg
chmod -R 700 /home/pg

恢复备份到指定时间点,可以大于当前时间,表示恢复到最新时间

su barman
barman recover --target-time "2019-01-04 01:07:30" 133 20190103T232839 /home/pg

Postgres对还原的目标文件夹具备权限

chown -R postgres:postgres /home/pg
chmod -R 700 /home/pg

修改服务器配置文件postgresql.conf,检查以下配置

  • listen_addresses:改为本机IP
  • port:改为本机端口
  • shared_buffers:改为本机合适的内存配置
  • shared_preload_libraries:选择本机已安装的插件

修改pg_hba.conf,设置合适的访问权限

检查Barman备份的streaming目录下是否有.partial后缀的文件,如果将该文件拷贝到恢复目录的pg_wal下,并删除.partial后缀

启动Postgres

查看Postgre日志,如出现以下内容,表示恢复到了指定的2019-01-04 01:07:30.563802+08时间点

2019-01-14 10:09:12 CST [30671]: [140-1] user=,db=,app=,client= LOG:  recovery stopping before commit of transaction 23584393, time 2019-01-04 01:07:30.563802+08
2019-01-14 10:09:12 CST [30671]: [141-1] user=,db=,app=,client= LOG:  recovery has paused
2019-01-14 10:09:12 CST [30671]: [142-1] user=,db=,app=,client= HINT:  Execute pg_wal_replay_resume() to continue.

此时数据库是只读的,可以打开数据库查看数据是否恢复正确

  1. 如果正确,使用超级管理员用户执行select pg_wal_replay_resume(),让数据库从只读变为可读写
  2. 如果不正确,停止数据库,修改恢复目标,再次重启恢复
    参考:https://www.postgresql.org/docs/current/recovery-target-settings.html

远程还原

还原目标服务器需要先安装与备份数据库相同版本的PostgreSQL

还原前需要先对被还原服务器做SSH免秘钥登录,否则还原将出错

恢复备份到指定时间点,可以大于当前时间,表示恢复到最新时间

barman recover --remote-ssh-command "ssh [email protected]" --target-time "2019-01-04 01:07:30" 133 20190103T232839 /home/pg

Postgres对还原的目标文件夹具备权限

chown -R postgres:postgres /home/pg
chmod -R 700 /home/pg

修改服务器配置文件postgresql.conf,检查以下配置

  • listen_addresses:改为本机IP
  • port:改为本机端口
  • shared_buffers:改为本机合适的内存配置
  • shared_preload_libraries:选择本机已安装的插件

修改pg_hba.conf,设置合适的访问权限

检查Barman备份的streaming目录下是否有.partial后缀的文件,如果将该文件拷贝到恢复目录的pg_wal下,并删除.partial后缀

启动Postgres

查看Postgre日志,如出现以下内容,表示恢复到了指定的2019-01-04 01:07:30.563802+08时间点

2019-01-14 10:09:12 CST [30671]: [140-1] user=,db=,app=,client= LOG:  recovery stopping before commit of transaction 23584393, time 2019-01-04 01:07:30.563802+08
2019-01-14 10:09:12 CST [30671]: [141-1] user=,db=,app=,client= LOG:  recovery has paused
2019-01-14 10:09:12 CST [30671]: [142-1] user=,db=,app=,client= HINT:  Execute pg_wal_replay_resume() to continue.

此时数据库是只读的,可以打开数据库查看数据是否恢复正确

  1. 如果正确,使用超级管理员用户执行select pg_wal_replay_resume(),让数据库从只读变为可读写
  2. 如果不正确,停止数据库,修改恢复目标,再次重启恢复
    参考:https://www.postgresql.org/docs/current/recovery-target-settings.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章