Postgre SQL   发布时间:2022-05-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[转]PostgreSQL的热备和恢复大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

POSTGResql的热备和恢复

日期:2006-08-07 作者:Seamus Dean 来自:Chinaunix


为什么要写这篇文章

因为我看了一下,国内所有的POSTGResql教程都没有很详细的介绍该功能,而相反,国内的Oracle文章对这块非常的看重。然,POSTGResql的官方文档有一个章节是介绍这块内容的,但是写得太过‘文学’化。
的确,一个数据库的可靠性和完整性是非常重要的,否则,很难叫人们所接受它。

本文假设读者对POSTGResql已经有基本的认识,如果你对POSTGResql还完全不熟悉的话,建议你先
http://www.POSTGResql.org看看它的
Documentation.



作为最强大的开源数据库,POSTGResql拥有一切商业数据库所拥有的功能,甚至比商业数据库更好。
在以前的版本中,它在数据恢复,可靠性方面做的不太好,但经过最近几年的发展,已经可以和Oracle
媲美了。


在PostgreSQl7的时候就引入了WAL(Write Ahead Logging)的概念,即预写日志,所有对数据库的更改,
在更改之前必须写到该LOG中,这样,就算机器断电,POSTGResql也可以从该LOG中知道数据库在断电前做
了什么操作,已经做到第几步了,这样保证了所有事务的完整性,但POSTGResql7没有提供很好的灾难恢复
机制,一旦数据库崩溃,除非你曾经对数据库作过pg_dump或者file system level BACkup,否则,你的数据
将全部丢失,并且,就算你曾经对数据库做过备份,也只能恢复到你备份的那一刻的数据,这对一个生产数据库
(特别是24*7生产库)来说,是无法容忍的。

@R_772_2657@的推出,使POSTGResql的稳定性和可靠性又迈出了划时代的一步。
除了提供对tablespace的支持外,@R_772_2657@提供了支持时间点的恢复---PITR.
其基本原理和Oracle的热备份完全一样:

首先对数据库在file system level做一个BACkup(POSTGResql是首先用pg_starT_Backup('label')命令,
然后用tar直接tar整个data目录,假设命名为base.tar,然后pg_stop_BACkup();结束热备。
Oracle首先是用alter tablespace xxx begin BACkup,然后直接cp数据文件);

然后,备份相关的配置文件(POSTGResql只需备份POSTGResql.conf,pg_hba.conf,pg_IDent.conf就可以了,其实,
前面的tar已经将这些文件备份了,Oracle需要alter database BACkup control file......);

最后,备份WAL(
可以设置POSTGResql.conf中的archive_command,
该命令可以让@R_772_2657@自动将需要的归档的日志文件备份的其他地方中。
但是注意:如果你是让@R_772_2657@调用archive_command来备份WAL的话,
可能根本就做不到PITR,我做过实验,如果依靠base.tar和archive_command产生的WAL其实只能恢复到最后一个
archive_command保存的WAL的数据,pg_xlog/下面可能还有数据,如果@R_772_2657@的数据目录彻底损坏的话,还是会
丢失数据,所以,我建议,在写数据备份脚本的时候,最好将pg_xlog/下面的WAL也一起备份,见下面的cpArch.sh。
)。

如果数据库崩溃,我们就可以使用热备产生的base.tar和archive_command产生的WAL和我们自己备份的WAL(pg_xlog)来进行数据库的
recovery.

下面举例来说明:
我的POSTGResql运行在:/home/pgsql/下面
数据目录在:/home/pgsql/database/
将热备数据文件备份到/disk3/POSTGResql/base/下面
将WAL备份到/disk3/POSTGResql/archives/下面

POSTGResql.conf中定义了如下的archive_command:
archive_command = 'cp -f %p /disk3/POSTGResql/archives/%f'
该命令会将POSTGResql产生的WAL cp到/disk3/POSTGResql/archives/中。

我的热备脚本如下:

(1)为了使丢失的数据在一分钟之内,在crontab中每分钟将pg_xlog/下面的WAL
BACkup到/disk3/POSTGResql/archives/。

crontab:
*/1 * * * * /home/pgsql/bin/cpArch.sh

cpArch.sh:
#!/bin/sh

cp -f /home/pgsql/database/pg_xlog/[0-9]* /disk3/POSTGResql/archives/



(2)编写热备脚本 hotBACkup.pl (我用perl):
#!/usr/bin/perl

#############################################################
# hotBACkup.pl
# Use to hot BACkup the POSTGResql database.
# Author:Seamus Dean
# Date:2005-04-11
##############################################################
@H_332_5@my($datadir) ="/home/pgsql/database";@H_332_5@my($bindir) ="/home/pgsql/bin";@H_332_5@my($BACkupdir) ="/disk3/POSTGResql/base";@H_332_5@my($receiver) ="
ljh13/@sina.com.cn";

sub begin_BACkup()
{
open(Psql,"|$bindir/psql") or mail_user("begin BACkup error.") && exit(100);
print Psql "@R_616_10288@ct pg_starT_Backup('BACkupNow');/n";
close(Psql);
}

sub end_BACkup()
{
open(Psql,"|$bindir/psql") or mail_user("end BACkup error.") && exit(100);
print Psql "@R_616_10288@ct pg_end_BACkup();/n";
close(Psql);
}


sub do_BACkup()
{
system("/bin/tar cvf base.tar $datadir");
system("/bin/mv -f base.tar $BACkupdir/");
}


sub mail_user()
{
my($msg) =@_;
open(MAIL,"|/bin/mail -s BACkup-result $receiver") or dIE("can not talk to:mail command./n");
print MAIL $msg;
close(MAIL);
}

###################################
# tell psql begin our BACkup
###################################
&begin_BACkup();

###################################
# do tar
###################################
&do_BACkup();

####################################
# tell psql end BACkup
####################################
&end_BACkup();

####################################
# mail the user about the result
####################################
&mail_user("POSTGResql BACkup successfully.");


到这里,备份脚本基本上就完了,你可以将hotBACkup.pl放在crontab中周期性的执行。

就算/home/pgsql/database目录彻底崩溃,我们可以像下面这样迅速恢复到1分钟内的数据:
#cp /disk3/POSTGResql/base/base.tar ./
#tar xvf base.tar
#cd database/
#vi recovery.conf

输入如下内容:
restore_command='cp /disk3/POSTGResql/archives/%f "%p"'

然后将/home/pgsql/database/pg_xlog/下面的WAL清空。

启动POSTGResql,我们可以看到如下的LOG信息:
LOG: Could not create IPv6 socket: Address family not supported by protocol
LOG: database system was interrupted at 2005-04-11 23:13:28 PDT
LOG: starTing archive recovery
LOG: restore_command = "cp /disk3/POSTGResql/archives/%f "%p""
cp: cAnnot stat `/disk3/POSTGResql/archives/00000001.history': No such file or directory
LOG: restored log file "00000001000000000000002E.008EFCAC.BACkup" from archive
LOG: restored log file "00000001000000000000002E" from archive
LOG: checkpoint record is at 0/2E8EFCAC
LOG: redo record is at 0/2E8EFCAc; undo record is at 0/0; shutdown falSE
LOG: next transaction ID: 5271; next OID: 6351357
LOG: automatic recovery in progress
LOG: redo starts at 0/2E8EFCE8
LOG: restored log file "00000001000000000000002F" from archive
LOG: restored log file "000000010000000000000030" from archive
LOG: restored log file "000000010000000000000031" from archive
LOG: restored log file "000000010000000000000032" from archive
LOG: restored log file "000000010000000000000033" from archive
LOG: restored log file "000000010000000000000034" from archive
LOG: restored log file "000000010000000000000035" from archive
LOG: restored log file "000000010000000000000036" from archive
LOG: restored log file "000000010000000000000037" from archive
LOG: restored log file "000000010000000000000038" from archive
LOG: restored log file "000000010000000000000039" from archive
LOG: restored log file "00000001000000000000003A" from archive
LOG: restored log file "00000001000000000000003B" from archive
LOG: restored log file "00000001000000000000003C" from archive
LOG: restored log file "00000001000000000000003D" from archive
LOG: restored log file "00000001000000000000003E" from archive
LOG: restored log file "00000001000000000000003F" from archive
LOG: restored log file "000000010000000000000040" from archive
LOG: restored log file "000000010000000000000041" from archive
LOG: restored log file "000000010000000000000042" from archive
LOG: restored log file "000000010000000000000043" from archive
LOG: restored log file "000000010000000000000044" from archive
LOG: restored log file "000000010000000000000045" from archive
LOG: restored log file "000000010000000000000046" from archive
LOG: restored log file "000000010000000000000047" from archive
LOG: restored log file "000000010000000000000048" from archive
LOG: restored log file "000000010000000000000049" from archive
LOG: restored log file "00000001000000000000004A" from archive
LOG: restored log file "00000001000000000000004B" from archive
LOG: restored log file "00000001000000000000004C" from archive
LOG: record with zero length at 0/4C2BABE4
LOG: redo done at 0/4C2BABA8
LOG: restored log file "00000001000000000000004C" from archive
LOG: archive recovery complete
LOG: database system is ready


显示数据已经成功恢复。
/home/pgsql/database/下面的recovery.conf会变为:recovery.done.


结论:
@R_772_2657@的PITR已经做得非常的成功,完全有可能替代Oracle,sqlserver
而成为企业的首选。所以,我们玩POSTGResql的兄弟们,一定要对它有信心!

来源:http://linux.chinaunix.net/docs/2006-08-07/2396.shtml

大佬总结

以上是大佬教程为你收集整理的[转]PostgreSQL的热备和恢复全部内容,希望文章能够帮你解决[转]PostgreSQL的热备和恢复所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:而相反