Postgresql - 恢复数据库到指定时间 Point-in-Time Recovery (PITR)

在有些时候需要用备份数据恢复到任意的时间点,该如何操作。

###################################
CentOS 7 + pg 10.4
###################################
1. 初始化数据库参数
# 创建archive目录,并授权到postgres用户
mkdir /data/archive/
chown -R postgres:postgres /data/archive/

# 修改数据库配置文件
vim /usr/local/pgsql/data/postgresql.conf
archive_mode = onarchive_timeout = 300 # 单位是秒,此处以5分钟为限强制归档,仅作测试archive_command = 'cp -i %p /data/archive/%f' # 会将pg_wal中的日志cp过去。wal_level = archive

# 初始化数据
# 创建表所用序列
create sequence seq_test01_id;
# 创建表
create table test01(
id int not null default nextval('seq_test01_id'::regclass) primary key,
col1 varchar(128),
ctime time);

2. 做基础备份
# 开启热备
psql -U postgres -d postgres -c "select pg_start_backup('label');"
# 备份data目录
cd /usr/local/pgsql/data
tar -cvf /data/backup/data.tar *
# 关闭热备
psql -U postgres -d mytest -c "select pg_stop_backup();"

# 切日志
psql -U postgres -d mytest -c "select pg_switch_wal();"

3.
##########################################
# 为表插入数据
create or replace function insert_test01() returns void
AS $$
begin
for i in 1..10 loop
PERFORM pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());
end loop;
end;
$$ LANGUAGE plpgsql;
select insert_test01();
##########################################

# 为表插入数据
insert into test01(col1,ctime) values('aaa', now());
select pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());
select pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());
select pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());
select pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());
select pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());
select pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());
select pg_sleep(2);
insert into test01(col1,ctime) values('aaa', now());

mytest=# select * from test01 ;
id | col1 | ctime
----+------+-----------------
1 | aaa | 05:52:03.675805
2 | aaa | 05:52:05.687527
3 | aaa | 05:52:07.695315
4 | aaa | 05:52:09.702641
5 | aaa | 05:52:11.711283
6 | aaa | 05:52:13.718943
7 | aaa | 05:52:15.726526
8 | aaa | 05:52:19.710031
(8 rows)

# 损坏数据
select now();
2018-06-19 06:03:29.391014-04

delete from test01 ;

4. 恢复
# 停服务
service postgresql stop

# 把之前的备份文件恢复到data目录
cd /usr/local/pgsql/data
rm -rf *
tar xvf data.tar

# 配置xlog文件
rm -rf pg_xlog
mkdir -p pg_xlog/archive_status
chown -R postgres:postgres pg_xlog

# 修改recovery.conf配置,恢复数据库到指定时间点
vim recovery.conf
restore_command = 'cp /data/archive/%f "%p"' # archive 目录
archive_cleanup_command='pg_archivecleanup /data/archive %r'
recovery_target_time='2018-06-19 06:03:05'

# 启动数据库服务
service postgresql start

5. 查看恢复数据
psql -U mytest -d mytest
mytest=# select * from test01 ;
id | col1 | ctime
----+------+-----------------
1 | aaa | 06:02:53.992508
2 | aaa | 06:02:56.111994
3 | aaa | 06:02:58.11978
4 | aaa | 06:03:00.127555
5 | aaa | 06:03:02.134798
6 | aaa | 06:03:04.142521




对于 PostgreSQLPITR (Point-In-Time Recovery),备份恢复的方法如下: 备份: 1. 首先,在 PostgreSQL 中开启 WAL 日志功能,通过修改 postgresql.conf 文件中的 `wal_level` 参数为 `replica` 或 `logical`。 2. 然后,在 pg_hba.conf 文件中添加一个用于备份的用户,并授权该用户访问需要备份数据库。 3. 运行以下命令备份 WAL 日志文件: ``` pg_basebackup -X stream -U backup_user -D /path/to/backup/directory -P ``` 其中,`backup_user` 是在 pg_hba.conf 文件中添加的备份用户,`/path/to/backup/directory` 是备份文件保存的目录。 4. 定期备份 `pg_xlog` 目录下的 WAL 日志文件。 恢复: 1. 首先,停止 PostgreSQL 数据库。 2. 将备份文件复制到 PostgreSQL 数据库数据目录中。 3. 将备份文件中的 `pg_xlog` 目录下的 WAL 日志文件复制到数据库数据目录的 `pg_xlog` 目录下。 4. 启动 PostgreSQL 数据库并执行以下命令: ``` pg_resetxlog -f /path/to/data/directory ``` 其中,`/path/to/data/directory` 是数据库数据目录的路径。 5. 然后,通过以下命令将数据库恢复指定时间点: ``` pg_restore -U postgres -d database_name -t table_name -I backup_file -c -n public -j 4 -v -F c -h localhost -p 5432 --time='YYYY-MM-DD HH24:MI:SS' ``` 其中,`database_name` 是要恢复数据库名称,`table_name` 是要恢复的表名称,`backup_file` 是备份文件的名称,`--time` 参数指定恢复到的时间点,格式为 `YYYY-MM-DD HH24:MI:SS`。 以上就是 PostgreSQL PITR备份恢复方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值