postgresql的时间点还原。

实验目的,模拟假设每天凌晨2点做一次数据库的完全备份,但是在第二天上午10点的时候出现故障,凌晨2点到上午10点之间产生的数据不丢失,可以正常恢复 。

先确保主数据库postgresql.conf 配置文件中,开启了归档备份功能

 在主数据库上建测试数据 

新建一个数据库

postgres=# create database test1;
CREATE DATABASE

在备份服务上对主数据库进行完全备份 

[postgres@localhost ~]$pg_basebackup -D /backup/ -h 10.0.0.7 -Ft -Pv -U postgres -p 5432 -R 

在主数据库上继续写数据 

postgres=# create database test1;
CREATE DATABASE
postgres=# \c test1 
You are now connected to database "test1" as user "postgres".
test1=# create table t1(id int);
CREATE TABLE
test1=# insert into t1 values(1);
INSERT 0 1
test1=# 

模拟出现故障,删库!!!!!!!

db1=# drop database test1 ;
DROP DATABASE
db1=# 

发现故障,停止用户访问 

查看日志 事务ID   当前的日志编号 000000010000000000000009    事务ID754

db1=# select pg_walfile_name(pg_current_wal_lsn());
     pg_walfile_name      
--------------------------
 000000010000000000000009
(1 row)

db1=# select txid_current();
 txid_current 
--------------
          754

在主数据库上刷新日志 

db1=# select pg_switch_wal();
 pg_switch_wal 
---------------
 0/90192B0
(1 row)

故障还原

在要还原的服务器上还原数据   停服 清空dada和archive目录  还原刚刚完全备份的数据 

[postgres@localhost ~]$pg_ctl stop
waiting for server to shut down.... done
server stopped
[root@localhost ~]#rm -rf  /pgsql/data/*
[root@localhost ~]#rm -rf  /archive/*
[root@localhost ~]#tar xf /backup/base.tar -C /pgsql/data/
[root@localhost ~]#tar xf /backup/pg_wal.tar -C /archive/

复制主数据库的最新的归档日志到备份数据上

[root@localhost ~]#rsync -a 10.0.0.7:/archive /archive/
root@10.0.0.7's password: 

查看看最新的归档日志 定位到故障语句的lsn编号是753  有drop dir 

pg_waldump /archive/archive/000000010000000000000009 

 修改备份数据库的postgres.conf配置文件

restore_command = 'cp /archive/%f %p'      

recovery_target_xid = '752'     表示指定到故障操作语句之前的一个点的编号故障操作时753

启动数据库 验证数据 

[postgres@localhost ~]$pg_waldump /archive/archive/000000010000000000000009 ^C
[postgres@localhost ~]$pg_ctl start
waiting for server to start....2022-11-20 11:47:55.568 CST [37069] LOG:  redirecting log output to logging collector process
2022-11-20 11:47:55.568 CST [37069] HINT:  Future log output will appear in directory "log".
 done
server started
[postgres@localhost ~]$psql
psql (14.6)
Type "help" for help.

postgres=# lc
postgres-# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 db1       | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 db2       | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 hellodb   | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test1     | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
(7 rows)

postgres-# \c test1 
You are now connected to database "test1" as user "postgres".
test1-# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | t1   | table | postgres
(1 row)

test1-# 

最后恢复数据之后 数据库时只读状态 需要切换状态   才可以正常写数据 

postgres=# select pg_wal_replay_resume();
 pg_wal_replay_resume 
----------------------
 
(1 row)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
PostgreSQL一个开源的关系型数据库管理系统,它提供了基于时间恢复(Point-in-Time Recovery,简称PITR)的功能。 基于时间恢复是指在数据库发生故障或数据丢失的情况下,能够恢复到一个指定的时间之前的数据库状态。这种恢复方法特别适用于意外删除数据、误操作或数据库崩溃等情况。 PostgreSQL实现基于时间恢复的方式是通过使用事务日志(transaction logs)来记录数据库变更。事务日志包含了数据库的每一个更改操作,包括插入、更新和删除等操作。 当需要进行恢复操作时,首先需要使用pg_start_backup()函数创建一个基于时间恢复的起始,然后将数据库中的事务日志归档。通过这个归档的事务日志,可以将数据库恢复到指定时间之前的状态。 具体的恢复操作包括以下步骤: 1. 关闭数据库并创建恢复配置文件,指定恢复的目标时间。 2. 恢复配置文件中指定的时间的事务日志会被用来还原数据库。 3. 将数据库恢复为指定时间之前的状态,包括删除恢复之后的事务日志。 4. 打开数据库,使其可以重新对外提供服务。 值得注意的是,基于时间恢复功能需要提前进行规划和配置。首先需要定期备份数据库并保留足够长的时间,以便在需要时可以进行恢复。其次,需要开启事务日志归档功能,确保数据库的事务日志可以被正确地保留和使用。 总结来说,PostgreSQL提供了基于时间恢复的功能,它通过记录数据库的事务日志来实现。使用这个功能可以在数据库故障或数据丢失的情况下,恢复到指定的时间之前的数据库状态。但是使用前需要进行规划和配置,包括定期备份数据库和开启事务日志归档功能等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值