您的位置:首页 > 数据库

PostgreSQL 8.1.22 开启归档模式及在线备份恢复

2017-10-20 13:26 323 查看
以下内容参考自:
http://www.kuqin.com/postgreSQL8.1_doc/backup-online.html https://www.postgresql.org/docs/8.1/static/backup-online.html
数据库版本:

postgres=# select version();

                                                        version                                                         

-------------------------------------------------------------------------------------------------------------------

 PostgreSQL 8.1.22 on x86_64-unknown-linux-gnu, compiled by GCC gcc (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973]

(1 row)

PG 8.1中还没有 wal_level和archive_mode参数,只有archive_command参数。

修改如下参数:

archive_command = 'cp -i %p /usr/local/pgsql/data/archivelog/%f'      

pg_ctl reload使修改生效:

postgres=# pg_ctl reload

验证参数已生效:

postgres=# show archive_command;

               archive_command                

----------------------------------------------

 cp -i %p /usr/local/pgsql/data/archivelog/%f

(1 row)

创建测试表,插入测试数据,模拟WAL日志切换:

postgres=# create table tbl_kenyon(id int,cname varchar(50),remark text);

CREATE TABLE

插入测试数据:

postgres=# insert into tbl_kenyon select generate_series(1,10000000),'kenyon good boy',md5('kenyon good boy');

LOG:  archived transaction log file "000000010000000000000001"

LOG:  archived transaction log file "000000010000000000000002"

LOG:  archived transaction log file "000000010000000000000003"

LOG:  checkpoints are occurring too frequently (14 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "000000010000000000000004"

LOG:  archived transaction log file "000000010000000000000005"

LOG:  archived transaction log file "000000010000000000000006"

LOG:  checkpoints are occurring too frequently (5 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "000000010000000000000007"

LOG:  archived transaction log file "000000010000000000000008"

LOG:  checkpoints are occurring too frequently (3 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "000000010000000000000009"

LOG:  archived transaction log file "00000001000000000000000A"

LOG:  archived transaction log file "00000001000000000000000B"

LOG:  checkpoints are occurring too frequently (4 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "00000001000000000000000C"

LOG:  archived transaction log file "00000001000000000000000D"

LOG:  archived transaction log file "00000001000000000000000E"

LOG:  archived transaction log file "00000001000000000000000F"

LOG:  checkpoints are occurring too frequently (4 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "000000010000000000000010"

LOG:  archived transaction log file "000000010000000000000011"

LOG:  checkpoints are occurring too frequently (4 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "000000010000000000000012"

LOG:  archived transaction log file "000000010000000000000013"

LOG:  archived transaction log file "000000010000000000000014"

LOG:  checkpoints are occurring too frequently (4 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "000000010000000000000015"

LOG:  archived transaction log file "000000010000000000000016"

LOG:  archived transaction log file "000000010000000000000017"

LOG:  checkpoints are occurring too frequently (4 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "000000010000000000000018"

LOG:  archived transaction log file "000000010000000000000019"

LOG:  archived transaction log file "00000001000000000000001A"

LOG:  checkpoints are occurring too frequently (4 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "00000001000000000000001B"

LOG:  archived transaction log file "00000001000000000000001C"

LOG:  archived transaction log file "00000001000000000000001D"

LOG:  checkpoints are occurring too frequently (4 seconds apart)

HINT:  Consider increasing the configuration parameter "checkpoint_segments".

LOG:  archived transaction log file "00000001000000000000001E"

^CCancel request sent

ERROR:  canceling statement due to user request

ERROR:  canceling statement due to user request

pg_xlog中WAL保留个数:

postgres@linux-o8rc:/usr/local/pgsql/data/pg_xlog> du -sh *

4.0K    000000010000000000000000.003A01A0.backup

17M     00000001000000000000001F

17M     000000010000000000000020

17M     000000010000000000000021

17M     000000010000000000000022

17M     000000010000000000000023

17M     000000010000000000000024

17M     000000010000000000000025

4.0K    archive_status

归档日志大小及数量:

postgres@linux-o8rc:/usr/local/pgsql/data/archivelog> pwd

/usr/local/pgsql/data/archivelog

postgres@linux-o8rc:/usr/local/pgsql/data/archivelog> du -sh *

17M     000000010000000000000000

4.0K    000000010000000000000000.00390098.backup

4.0K    000000010000000000000000.003A01A0.backup

17M     000000010000000000000001

17M     000000010000000000000002

17M     000000010000000000000003

17M     000000010000000000000004

17M     000000010000000000000005

17M     000000010000000000000006

17M     000000010000000000000007

17M     000000010000000000000008

17M     000000010000000000000009

17M     00000001000000000000000A

17M     00000001000000000000000B

17M     00000001000000000000000C

17M     00000001000000000000000D

17M     00000001000000000000000E

17M     00000001000000000000000F

17M     000000010000000000000010

17M     000000010000000000000011

17M     000000010000000000000012

17M     000000010000000000000013

17M     000000010000000000000014

17M     000000010000000000000015

17M     000000010000000000000016

17M     000000010000000000000017

17M     000000010000000000000018

17M     000000010000000000000019

17M     00000001000000000000001A

17M     00000001000000000000001B

17M     00000001000000000000001C

17M     00000001000000000000001D

...

PG8.1中没有pg_switch_xlog()函数和wal_segment_size参数等:

postgres=# select pg_switch_xlog();

ERROR:  function pg_switch_xlog() does not exist

HINT:  No function matches the given name and argument types. You may need to add explicit type casts.

postgres=# show wal_segment_size ;

ERROR:  unrecognized configuration parameter "wal_segment_size"

postgres=# show checkpoint_completion_target;

ERROR:  unrecognized configuration parameter "checkpoint_completion_target"

ERROR:  unrecognized configuration parameter "checkpoint_completion_target"

postgres=# 

postgres=# show checkpoint_segments ;

 checkpoint_segments 

---------------------

 3

(1 row)

开启归档后,使用pg_start_backup()和pg_stop_backup()方式备份数据库;

执行pg_stop_backup()后会在归档目录下生成类似"00000001000000000000001F.009DCDB0.backup"格式的文件名(其中第二部分表示在该 WAL 文件里面的准确位置),它包含 pg_start_backup 的标签字串, 以及备份的起始时间和终止时间,在00000001000000000000001F之前生成的归档文件可以被删除(恢复时也不再需要)。

postgres=# SELECT pg_start_backup('label');

 pg_start_backup 

-----------------

 0/1F9DCDB0

(1 row)

postgres=# SELECT pg_stop_backup();

 pg_stop_backup 

----------------

 0/1F9DCE00

(1 row)

postgres=# LOG:  archived transaction log file "00000001000000000000001F.009DCDB0.backup"

postgres@linux-o8rc:/usr/local/pgsql/data> cd archivelog/

postgres@linux-o8rc:/usr/local/pgsql/data/archivelog> ll

total 508536

-rw------- 1 postgres users 16777216 Oct 19 20:07 000000010000000000000000

-rw------- 1 postgres users      235 Oct 19 19:42 000000010000000000000000.00390098.backup

-rw------- 1 postgres users      235 Oct 19 20:00 000000010000000000000000.003A01A0.backup

-rw------- 1 postgres users 16777216 Oct 19 20:10 000000010000000000000001

-rw------- 1 postgres users 16777216 Oct 19 20:10 000000010000000000000002

-rw------- 1 postgres users 16777216 Oct 19 20:10 000000010000000000000003

-rw------- 1 postgres users 16777216 Oct 19 20:10 000000010000000000000004

-rw------- 1 postgres users 16777216 Oct 19 20:10 000000010000000000000005

-rw------- 1 postgres users 16777216 Oct 19 20:10 000000010000000000000006

-rw------- 1 postgres users 16777216 Oct 19 20:10 000000010000000000000007

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000008

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000009

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000000A

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000000B

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000000C

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000000D

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000000E

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000000F

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000010

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000011

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000012

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000013

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000014

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000015

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000016

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000017

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000018

-rw------- 1 postgres users 16777216 Oct 19 20:11 000000010000000000000019

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000001A

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000001B

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000001C

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000001D

-rw------- 1 postgres users 16777216 Oct 19 20:11 00000001000000000000001E

-rw------- 1 postgres users      241 Oct 19 21:13 00000001000000000000001F.009DCDB0.backup

postgres@linux-o8rc:/usr/local/pgsql/data/archivelog> cat 00000001000000000000001F.009DCDB0.backup 

START WAL LOCATION: 0/1F9DCDB0 (file 00000001000000000000001F)

STOP WAL LOCATION: 0/1F9DCE00 (file 00000001000000000000001F)

CHECKPOINT LOCATION: 0/1F9DCDB0

START TIME: 2017-10-19 21:13:30 CST

LABEL: label

STOP TIME: 2017-10-19 21:13:36 CST

postgres@linux-o8rc:/usr/local/pgsql/data/archivelog> date

Thu Oct 19 21:14:10 CST 2017

postgres@linux-o8rc:/usr/local/pgsql/data/archivelog> 

使用该方式备份后,恢复时需在recovery.conf中添加如下参数设置:

restore_command = 'cp /usr/local/pgsql/data/archivelog/%f %p'

另外结合如下参数进行数据库的恢复:

recovery_target_time (timestamp)

recovery_target_xid (string)

recovery_target_inclusive (boolean)

recovery_target_timeline (string)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: