(一)创建基础备份
1 、配置可以基于时间点的备份与恢复(point-in-time recovery (PITR)):
1.1 postgresql.conf 中配置
wal_level = replica # used to be "hot_standby" in older versions 9.6 default value minimal
max_wal_senders = 10 # at least 2, better at least 2
archive_mode = on
archive_command = 'cp %p /archive/%f' (也可使用rsync , scp ,需确保返回值为0,才被认为是归档成功了)
1.2 pg_hba.conf 的配置
# replication privilege.
local replication postgres trust
host replication postgres 127.0.0.1/32 trust
host replication postgres ::1/128 trust
1.3配置完后可以进行备份数据库
export PGPASSWORD=passowrd
nohup pg_basebackup -D /postgresql/backup -U repuser -h 1922.168.1.1 -p 4000 --checkpoint=fast --wal-method=stream -Ft -Pv -Xf -z -Z5 > basebackup.log 2>&1 &
说明:
1.添加 --wal-method=stream 参数确保创建的备份是自包含的 PostgreSQL 10.0 以后版本这个参数是默认的,9.6 前版本需要加上
-xlog-method=stream .
2.备份恢复时会生产 .backup 文件,这个文件显示了那些日志是在恢复时所需要的
000000010000000000000002.00000028.backup
#means that the replay process starts somewhere within file ...0002 (at position ...28 ). 也就是说可以清量 ...0002 之前的日志
(二)恢复数据
1.新建数据目录,并授权(或者确保已有目录为空)
mkdir -p /postgres/10.3/data/
chmod 0700 /postgres/10.3/data/
2.解压使用pg_basebackup创建的备份:
tar xvzf base.tar.gz -C /postgres/10.3/data/
说明:如果是没有压缩,则直接可以使用
3.创建recovery.conf文件进行配置:
Cp ~/recovery.conf.sample /postgres/10.3/data/recovery.conf
chmod 0600 recovery.conf
select current_timestamp;
vi recovery.conf
a.恢复到最新:
restore_command = 'cp /postgres/10.3/data/archive_wal/%f %p'
recovery_target_timeline = 'latest'
b.恢复到指定的时间点:
restore_command = 'cp /postgres/10.3/data/archive_wal/%f %p'
recovery_target_time = '2020-04-02 15:16:49'
c.基于还原点恢复,创建还原点:
SELECT pg_create_restore_point('restore_point_01');
d.恢复到还原点:
restore_command = 'cp /postgres/10.3/data/archive_wal/%f %p'
recovery_target_name ='restore_point_01'
备注,在做的时候最好切换一个日志:select pg_switch_wal();
启动数据库进行恢复:
pg_ctl start -D /postgres/10.3/data/archive_wal
恢复到时间点后,确保可以的另一方法:
修改postgresql.conf 添加参数:
hot_standby=on
recovery_target_action = 'pause'
查看是否在应用日志
postgres=# \x
Expanded display is on.
postgres=# \df *pause*
List of functions
-[ RECORD 1 ]-------+-------------------------
Schema | pg_catalog
Name | pg_is_wal_replay_paused
Result data type | boolean
Argument data types |
Type | normal
-[ RECORD 2 ]-------+-------------------------
Schema | pg_catalog
Name | pg_wal_replay_pause
Result data type | void
Argument data types |
postgres=# \df *resume*
List of functions
-[ RECORD 1 ]-------+----------------------
Schema | pg_catalog
Name | pg_wal_replay_resume
Result data type | void
Argument data types |
Type | normal
暂停日志应用:SELECT pg_wal_replay_pause()
恢复日志应用:SELECT pg_wal_replay_resume();
备份恢复相关工具与命令:
pg_receivewal ( 9.6 之前为 pg_receivexlog )
pg_archivecleanup 清理日志
pg_stat_archiver 查询那些日志是有在归档时失败
使用 pg_basebackup 做复制
pg_basebackup -D /target
-h master.example.com
--checkpoint=fast
--wal-method=stream -R
注: -R, --write-recovery-conf # write configuration for replication pg_basebackup 自动创建recovery.conf
pg12 后,会自动在postgresql.conf 添加
standby_mode = on primary_conninfo = ' ...'
来源:oschina
链接:https://my.oschina.net/zhiyonghe/blog/4878676