接着上一篇pg备份文档:https://blog.csdn.net/weixin_41561946/article/details/104183555
六、 pgbackRest恢复
1. 查看现有备份
[[email protected] ~]$ pgbackrest info
stanza: demo
status: ok
cipher: aes-256-cbc
db (current)
wal archive min/max (11-1): 0000000A00000001000000F8/0000000A00000001000000FC
full backup: 20200204-163558F
timestamp start/stop: 2020-02-04 16:35:58 / 2020-02-04 16:46:14
wal start/stop: 0000000A00000001000000F8 / 0000000A00000001000000F8
database size: 3.9GB, backup size: 3.9GB
repository size: 810.3MB, repository backup size: 810.3MB
full backup: 20200205-142255F
timestamp start/stop: 2020-02-05 14:22:55 / 2020-02-05 14:30:50
wal start/stop: 0000000A00000001000000FB / 0000000A00000001000000FB
database size: 3.9GB, backup size: 3.9GB
repository size: 810.3MB, repository backup size: 810.3MB
2. 准备恢复环境
数据目录:/cddb5.0/data2
表空间目录:/cddb5.0/tablespace
关闭数据库
pg_ctl stop –D /cddb5.0/data2
删除data2/和tablespace目录
rm –f /cddb5.0/data2/*
rm –f /cddb5.0/tablespace
3. 执行完全恢复操作
[[email protected] cddb5.0]$ pgbackrest restore --stanza=demo --delta --log-level-console=detail
2020-02-05 14:58:45.779 P00 DETAIL: sync path ‘/cddb5.0/data2/global’
2020-02-05 14:58:46.124 P00 INFO: restore command end: completed successfully (226917ms)
4. 执行部分数据库恢复操作
创建测试数据库
[[email protected] cddb5.0]$ psql -d SWDS -p 7321 -U swds2014
SWDS=# create database test1;
SWDS=# create database test2;
执行增量备份
[[email protected] cddb5.0]$ pgbackrest --stanza=demo --type=incr backup
创建测试表
test1=# create table test1_table(id int);
test1=# insert into test1_table values(1);
test1=# \c test2
test2=# create table test2_table(id int);
test2=# insert into test2_table values(2);
恢复数据库test2
[[email protected] cddb5.0] pgbackrest --stanza=demo --delta --db-include=test2 restore
[[email protected] cddb5.0] psql -d SWDS -p 7321 -U swds2014
psql: FATAL: relation mapping file “base/16391/pg_filenode.map” contains invalid data
[[email protected] cddb5.0]$ psql -d SWDS -p 7321 -U swds2014
psql: FATAL: relation mapping file “base/16391/pg_filenode.map” contains invalid data
test2=# select * from test2_table;
id
2
说明:test1,swds,nyc_data三个数据库无法访问,pg自带的数据库可以访问
5. 基于时间点的恢复操作
准备测试数据
test2=# create table test(message text);
CREATE TABLE
test2=# insert into test values(‘aaaa’);
INSERT 0 1
test2=# select current_timestamp;
current_timestamp
2020-02-05 15:58:01.852901+08
执行差异备份
[[email protected] cddb5.0]$ pgbackrest --stanza=demo --type=diff --log-level-console=detail backup
删除test
test2=# drop table test2_table ;
执行基于时间点的恢复test表
[[email protected] cddb5.0]$ pg_ctl stop -D /cddb5.0/data2/
waiting for server to shut down… done
[[email protected] cddb5.0]$ mv /cddb5.0/data2/recovery.done /cddb5.0/data2/recovery.conf
[[email protected] cddb5.0]$ vi /cddb5.0/data2/recovery.conf
restore_command = ‘pgbackrest --stanza=demo archive-get %f “%p”’
recovery_target_time = ‘2020-02-05 15:58:01.852901+08’
recovery_target_action = ‘promote’
[[email protected] cddb5.0]$ pgbackrest --stanza=demo --delta --type=time --target=“2020-02-05 15:58:01.852901+08” --target-action=promote restore
2020-02-05 16:33:32.955 P00 INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)
2020-02-05 16:33:32.955 P00 DETAIL: sync path ‘/cddb5.0/data2/global’
2020-02-05 16:33:33.307 P00 INFO: restore command end: completed successfully (85946ms)
[[email protected] cddb5.0]$ pg_ctl start -D /cddb5.0/data2/
查看恢复情况
[[email protected] cddb5.0]$ psql –d test2 -U swds2014
test2=# \d
List of relations
Schema | Name | Type | Owner
--------±------------±------±---------
public | test | table | swds2014
public | test2_table | table | swds2014
test2=# select * from test2_table ;
id
2
结果:恢复成功