colorful

zc qq:1337220912

 

pg热备

数据库的PITR是一般数据库都必须满足的技术。其原理是依据之前的物理备份文件加上wal的预写日志模式备份做的恢复。该技术支持8.*及以上版本。下面主要概述PITR的准备和恢复过程。 测试环境

OS 环境:CentOS 6.2
数据库 :PostgreSQL 9.1.9


一、前期工作既要恢复,肯定是需要一个备份基础的,否则再怎么的巧妇也难为无米之炊。
1.修改数据库参数,修改postgresql.conf:

archive_mode = on
archive_timeout = 300   --单位是秒,此处以5分钟为限强制归档,仅作测试
archive_command = 'cp %p /data/pgbackup/archive/%f'  -- 注意/data/pgbackup/archive/目录权限, chmod -R 777 /data/pgbackup/archive/
wal_level = archive

修改完重启下reload,DB

2.基于文件级别的持续备份,
a.基础备份
postgres=# select pg_start_backup('backup_2012_05_20_14:22:10');

b.打包备份pg_data
# cd /data
# tar -cvzf pgdata.tar ./postgres
mv pgdata.tar /data/pgbackup/base/

c.结束基础备份并切换归档

postgres=# select pg_stop_backup();

postgres=# select pg_switch_xlog();
 pg_switch_xlog
----------------
 0/C000020
(1 row)

postgres=# select pg_current_xlog_location();
 pg_current_xlog_location
--------------------------
 0/C000020
(1 row)

postgres=# create table test_1(id int,name varchar(50));
postgres=# insert into test_1 values (1,'kenyon');
INSERT 0 1

此时在pg_data路径下会产生一个label,可以查看内容有checkpoint时间,基础备份的开始和结束时间,以及标签名称等。因为之前已经设置了archive的三个参数,可以在archive的备份路径pg_home/archive下看到归档的文件会定时传过来。

二、恢复过程
停数据库
# pg_stop

假定数据库的崩溃场景,将pgdata数据删除
# rm -rf /database/pgdata

恢复之前备份的tar文件
# tar xvf pgdata.tar

删除pg_xlog文件夹并重建
# rm -rf pg_xlog
# mkdir -p pg_xlog/archive_status

新建recovery.conf文件并修改
# vi /data/postgres/recovery.conf
--新增内容,指定恢复文件和路径,%f,%p见上面说明
restore_command = 'cp /data/pgbackup/archive/%f "%p"'

启动数据库

# pg_start
[postgres@localhost archive]$ psql
spsql (9.1.3)
Type "help" for help.

postgres=# select * from test_1;
 id |  name  
----+--------
  1 | kenyon
(1 rows)

--恢复成功,会恢复到之前接收到的最后一个归档文件。另外recovery.conf会改名变成recovery.done

日志内容:

LOG:  shutting down
LOG:  database system is shut down
LOG:  database system was interrupted; last known up at 2012-05-20 22:23:15 CST
LOG:  starting archive recovery
LOG:  restored log file "000000010000000000000002" from archive
LOG:  redo starts at 0/8000078
LOG:  consistent recovery state reached at 0/C000000
LOG:  restored log file "000000010000000000000003" from archive
LOG:  restored log file "000000010000000000000004" from archive
LOG:  restored log file "000000010000000000000005" from archive
LOG:  restored log file "000000010000000000000006" from archive
LOG:  restored log file "000000010000000000000007" from archive
cp: cannot stat `/home/postgres/archive/000000010000000000000008': No such file or directory
LOG:  could not open file "pg_xlog/000000010000000000000008" (log file 0, segment 8): No such file or directory
LOG:  redo done at 0/1C000078
LOG:  last completed transaction was at log time 2012-05-20 23:01:22.960591+08
LOG:  restored log file "000000010000000000000007" from archive
cp: cannot stat `/home/postgres/archive/00000002.history': No such file or directory
LOG:  selected new timeline ID: 2
cp: cannot stat `/home/postgres/archive/00000001.history': No such file or directory
LOG:  archive recovery complete
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

PS:若要恢复到指定时间,还需要再recovery.conf中设置recovrey_target_time,recovery_target_timeline等参数

总结:pitr技术对于7*24小时支撑是至关重要的,但是如果数据库非常小,增大pg_dump备份的频率可能更方便,但对于大数据库就需要了。

posted on 2013-11-06 17:12 多彩人生 阅读(650) 评论(0)  编辑 收藏 引用 所属分类: postgresql


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜