Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2783406
  • 博文数量: 423
  • 博客积分: 7770
  • 博客等级: 少将
  • 技术积分: 4766
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-09 11:58
个人简介

Oracle/DB2/Postgresql/Mysql/Hadoop/Greenplum/Postgres-xl/Mongodb

文章分类

全部博文(423)

文章存档

2019年(3)

2018年(6)

2017年(27)

2016年(23)

2015年(30)

2014年(16)

2013年(31)

2012年(73)

2011年(45)

2010年(14)

2009年(30)

2008年(30)

2007年(63)

2006年(32)

分类: Mysql/postgreSQL

2013-03-19 19:15:12

一、开启归档日志

[postgres@rhel5-01 ~]$ vi /usr/local/postgresql-9.2.1/data/postgresql.conf
# - Settings -


wal_level = archive                     # minimal, archive, or hot_standby
                                        # (change requires restart)
#fsync = on                             # turns forced synchronization on or off
#synchronous_commit = on                # synchronization level;


# - Archiving -


archive_mode = on               # allows archiving to be done
                                # (change requires restart)
archive_command = ' cp %p /usr/local/postgresql-9.2.1/archive/%f'               # command to use to archive a logfile segment
                                # placeholders: %p = path of file to archive
                                #               %f = file name only
                                # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
#archive_timeout = 0            # force a logfile segment switch after this
                                # number of seconds; 0 disables




#------------------------------------------------------------------------------
# REPLICATION
#------------------------------------------------------------------------------


# - Sending Server(s) -


# Set these on the master and on any standby that will send replication data.


max_wal_senders = 2             # max number of walsender processes
                                # (change requires restart)


二、开启流复制访问


编辑 pg_hba.conf
# replication privilege.
#local   replication     postgres                                trust
 host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust


三、生成热备份


[postgres@rhel5-01 tmp]$ pg_basebackup -D backup -h localhost -U postgres -Ft -z -P 
26711/26711 kB (100%), 2/2 tablespaces
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
[postgres@rhel5-01 tmp]$ 

四、利用热备进行恢复


[root@rhel5-01 postgresql-9.2.1]# mv data data_bak
[root@rhel5-01 postgresql-9.2.1]# 
[root@rhel5-01 backup]# mv /tmp/backup /usr/local/postgresql-9.2.1/data
[root@rhel5-01 data]# cd /usr/local/postgresql-9.2.1/data
[root@rhel5-01 data]# tar -zxvf 16394.tar.gz 
[root@rhel5-01 data]# tar -zxvf base.tar.gz 
[root@rhel5-01 data]# cp /usr/local/postgresql-9.2.1/share/recovery.conf.sample  recovery.conf
[root@rhel5-01 data]#mkdir -p pg_xlog/archive_status/


五、编辑recovery.conf


# NOTE that the basename of %p will be different from %f; do not
# expect them to be interchangeable.
#
restore_command = 'cp /usr/local/postgresql-9.2.1/archive/%f %p' 


六、启动postgreseql


[root@rhel5-01 pg_log]# /etc/init.d/postgresql-9.2 start
[root@rhel5-01 pg_log]# tail -f postgresql-2013-03-01_064501.csv 
2013-03-01 06:45:02.068 CST,,,6207,,512fdded.183f,3,,2013-03-01 06:45:01 CST,,0,LOG,00000,"restored log file ""000000010000000000000017"" from archive",,,,,,,,,""
2013-03-01 06:45:02.433 CST,,,6207,,512fdded.183f,4,,2013-03-01 06:45:01 CST,,0,LOG,00000,"redo starts at 0/17000080",,,,,,,,,""
2013-03-01 06:45:02.434 CST,,,6207,,512fdded.183f,5,,2013-03-01 06:45:01 CST,,0,LOG,00000,"consistent recovery state reached at 0/18000000",,,,,,,,,""
2013-03-01 06:45:03.425 CST,,,6207,,512fdded.183f,6,,2013-03-01 06:45:01 CST,,0,LOG,00000,"restored log file ""000000010000000000000018"" from archive",,,,,,,,,""
2013-03-01 06:45:03.435 CST,,,6207,,512fdded.183f,7,,2013-03-01 06:45:01 CST,,0,LOG,58P01,"could not open file ""pg_xlog/000000010000000000000019"" (log file 0, segment 25): No such file or directory",,,,,,,,,""
2013-03-01 06:45:03.435 CST,,,6207,,512fdded.183f,8,,2013-03-01 06:45:01 CST,,0,LOG,00000,"redo done at 0/18000FF0",,,,,,,,,""
2013-03-01 06:45:03.435 CST,,,6207,,512fdded.183f,9,,2013-03-01 06:45:01 CST,,0,LOG,00000,"last completed transaction was at log time 2013-03-01 06:28:01.44982+08",,,,,,,,,""
2013-03-01 06:45:04.335 CST,,,6207,,512fdded.183f,10,,2013-03-01 06:45:01 CST,,0,LOG,00000,"restored log file ""000000010000000000000018"" from archive",,,,,,,,,""
2013-03-01 06:45:04.358 CST,,,6207,,512fdded.183f,11,,2013-03-01 06:45:01 CST,,0,LOG,00000,"selected new timeline ID: 2",,,,,,,,,""
2013-03-01 06:45:07.774 CST,,,6207,,512fdded.183f,12,,2013-03-01 06:45:01 CST,,0,LOG,00000,"archive recovery complete",,,,,,,,,""
2013-03-01 06:45:10.208 CST,,,6225,,512fddf6.1851,1,,2013-03-01 06:45:10 CST,,0,LOG,00000,"autovacuum launcher started",,,,,,,,,""
2013-03-01 06:45:10.249 CST,,,6205,,512fdded.183d,1,,2013-03-01 06:45:01 CST,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""


七、查看数据库状态
[postgres@rhel5-01 ~]$ psql -U mqis_app mqis
psql (9.2.1)
Type "help" for help.


mqis=> select count (*) from test;
 count 
-------
    41
(1 row)


mqis=> 


至此数据库恢复故障前情况。。(另外,可以编写热备份定期脚本和删除归档日志)




阅读(9976) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

翼展狂龙2015-02-04 16:56:50

内容说明的很细致,不过我是个新手,用的的是Windows系统,archive_command的内容以及后边的部分内容不知道该怎么写,楼主有研究过windows系统下的PostgreSQL的热备吗?