前置条件:

postgresql:9.5.7datapath:/postgresql/databackpath:/postgresql/backup


数据准备:

test=# select * from test; id |         name         ----+----------------------  1 | qwe                   2 | asd                 (2 rows)


创建基础备份:

pg_basebackup需要指定一个超级用户或者具有REPLICATION权限的用户

pg_basebackup -h 192.168.56.117 -U repl -W -p 5432 -v -P -Xs -Fp -D /postgresql/backup/ -l backup_test

[postgres@localhost postgresql]$ pg_basebackup -h 192.168.56.117 -U repl -W -p 5432 -v -P -Xs -Fp -D /postgresql/backup/ -l backup_testPassword: pg_basebackup: initiating base backup, waiting for checkpoint to completepg_basebackup: checkpoint completedtransaction log start point: 0/2000028 on timeline 1pg_basebackup: starting background WAL receiver29983/29983 kB (100%), 1/1 tablespace                                         transaction log end point: 0/2000130pg_basebackup: waiting for background process to finish streaming ...pg_basebackup: base backup completed

备份完成后会在备份目录/postgresql/backup下生成原格式文件(-Fp),可以配置为压缩模式(-Ft),并记录备份时段的日志(-Xs),在归档日志目录下,生成备份标记文件:

[postgres@localhost postgresql]$ ll /postgresql/archive/总用量 32772-rw-------. 1 postgres postgres 16777216 9月   8 10:02 000000010000000000000001-rw-------. 1 postgres postgres 16777216 9月   8 10:02 000000010000000000000002-rw-------. 1 postgres postgres      288 9月   8 10:02 000000010000000000000002.00000028.backup

查看备份标记文件,可以获取备份完成时间,对应的日志文件位置等信息:

[postgres@localhost postgresql]$ cat /postgresql/archive/000000010000000000000002.00000028.backup START WAL LOCATION: 0/2000028 (file 000000010000000000000002)STOP WAL LOCATION: 0/2000130 (file 000000010000000000000002)CHECKPOINT LOCATION: 0/2000060BACKUP METHOD: streamedBACKUP FROM: masterSTART TIME: 2020-09-08 10:02:56 CSTLABEL: backup_testSTOP TIME: 2020-09-08 10:02:56 CST


插入增量测试数据:

2020-09-08 10:05:20

psql\c testinsert into test values(3,'zxc');create table test2(id int primary key,age int);


PITR恢复:

postgresql PITR是基于pg_basebackup+wal log实现的。

停止pg服务并清空数据目录:

su - postgrespg_ctl -D /postgresql/data stoprm -rf /postgresql/data/*

将备份目录下的数据文件拷贝至目标数据目录:

cp -r /postgresql/backup/* /postgresql/data

数据目录下创建recover.conf并配置:

restore_command = 'cp /postgresql/archive/%f %p'recovery_target_time = '2020-09-08 10:03:10'

启动数据库服务:

pg_ctl -D /postgresql/data start


数据验证:

test=# select * from test; id |         name         ----+----------------------  1 | qwe                   2 | asd                 (2 rows)
test=# \d test2;Did not find any relation named "test2".

经验证数据已经恢复到2020-09-08 10:03:10的时间点


©著作权归作者所有:来自51CTO博客作者mb5ff980b461ced的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. Redo Log & CacheEngine 性能详解
  2. 高频数据采集请求如何不影响主业务(7)
  3. PowerBI基础知识——显示和导出数据
  4. SQL基础知识V2——约束(语法)
  5. SQL在数据处理过程中,遇到除数为0该怎么办?
  6. PG数据库管理_备份工具的使用
  7. Docker容器实战之数据持久化+网络模式+资源限制
  8. 7:VMware Horizon View 8.0-安装Composer服务器
  9. [灾备]备份类型

随机推荐

  1. [转]android单元测试初探——Instrumenta
  2. Android中的线性布局(LinearLayout)
  3. android apk 程序签名
  4. Android(安卓)简单计算器源码....
  5. Android Developers 系列 01 - Introduct
  6. 重新解压打包android 根文件系统 ramdisk
  7. Android app 升级:android:versionCode和a
  8. 怎样搭建Android开发平台
  9. Windows下Camtasia Studio的安装与卸载
  10. android 开发技巧(5)--使 用 TextSwitche