备份
pg_basebackup -D /tmp/pg_backup/ -Ft -Pv -U postgres -h 1.15.57.253 -p5432 -R
-D
空文件,没有该目录会自动创建F
格式化t
打包为tar包Pv
显示备份的详细过程-u
用户-p
端口
备份报错:
在配置文件pg_hba.conf增加一行:
备份流程:
pg_basebackup: initiating base backup, waiting for checkpoint to complete #开始基础备份,等待检查点完成
pg_basebackup: checkpoint completed #已完成检查点
pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1 #预写日志起始于时间点: 0/2000028, 基于时间轴1
pg_basebackup: starting background WAL receiver #启动后台 WAL 接收进程
pg_basebackup: created temporary replication slot "pg_basebackup_24583" #已创建临时复制槽"pg_basebackup_24583"
24598/24598 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/2000138 #预写日志结束点: 0/2000138
pg_basebackup: waiting for background process to finish streaming ... #等待后台进程结束流操作...
pg_basebackup: syncing data to disk ... #同步数据到磁盘
pg_basebackup: base backup completed #基础备份已完成
恢复
[root@VM-0-9-centos data]# rm -rf /pgdata/12/data
[root@VM-0-9-centos data]# rm -rf /pgdata/12/arch
tar -xvf base.tar -C /pgdata/12/data
tar -xvf pg_wal.tar -C /pgdata/12/arch
vi postgresql.auto.conf
追加:
restore_command = 'cp /pgdata/12/arch/%f %p'
recovery_target = 'immediate'
touch /pgdata/12/recovery.signal
恢复成功:
:::tips
注意:data目录需要有700权限。
:::
需要进行操作:
select pg_wal_replay_resume();
恢复到某个时刻
恢复原理:
模拟测试:
此时14点,误删了库:
关库:
pg_ctl -D /pgdata/12/data stop -mi
删除数据:
rm -rf /pgdata/12/data/*
将备份解压到数据目录:
tar -xvf base.tar -C /pgdata/12/data
使用pg_waldump查看需要恢复到哪个事务之前。
恢复到502之前:
编辑配置文件:
postgresql.auto.conf追加:
restore_command = 'cp /pgdata/12/arch/%f %p'
recovery_target_xid='501'
启动数据库:
pg_ctl -D /pgdata/12/data -l logfile start
将恢复停止:
select pg_wal_replay_resume();