好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PostgreSQL归档配置及自动清理归档日志的操作

在一般的生产环境中,数据库都需要开启归档模式,那么在pg中如何开启归档模式呢?

pg中的归档配置涉及几个参数如下:

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

# - Archiving -

 

是否开启归档

#archive_mode = off        # enables archiving; off , on , or always

                 # (change requires restart)

 

归档命令,注意 %p %f %% 格式化的含义。

%p 是被归档的redo文件的路径, 

%f 是被归档的redo文档的文件名  

%% 是百分号

#archive_command = ''       # 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'

 

超时强制归档,例:如果10分钟数据库都没有什么活动,一个redo文件没有写完,就不会归档,

但是我们希望数据库至少10分钟要切换一个日志,则可以使用archive_timeout

#archive_timeout = 0      # force a logfile segment switch after this

                 # number of seconds; 0 disables

–归档配置方法为:

 

1、创建归档目录

?

1

pg12@oracle-> mkdir -p $PGDATA/archive/

2、编辑归档脚本

该脚本还可以删除7天内的归档日志。

?

1

2

pg12@oracle-> vi $PGDATA/arch.sh

test ! -f $PGDATA/arch/$1 && cp --preserve=timestamps $2 $PGDATA/arch/$1 ; find /arch/ -type f -mtime +7 -exec rm -f {} \;

3、配置归档相关参数

?

1

2

3

wal_level = replica

archive_mode = on

archive_command = 'arch.sh %f %p'

配置完之后重启数据库服务即可。

?

1

2

pg12@oracle-> ps -ef|grep archiver

pg12 21338 21331 0 20:20 ? 0 postgres: archiver

补充:postgresql归档失败并在日志文件中存在报错

PG运行过程中出现归档失败的情况,并在日志文件中存在报错。

报错如下:

 

?

1

2

3

4

5

6

7

8

9

10

cp: writing `/arch/20171204/000000010000000000000002 ': No space left on device

LOG: archive command failed with exit code 1

DETAIL: The failed archive command was: DATE=`date +%Y%m%d`;DIR="/arch/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp pg_xlog/000000010000000000000002 $DIR/000000010000000000000002

cp: writing `/arch/20171204/000000010000000000000002' : No space left on device

LOG: archive command failed with exit code 1

DETAIL: The failed archive command was: DATE =` date +%Y%m%d`;DIR= "/arch/$DATE" ;(test -d $DIR || mkdir -p $DIR)&& cp pg_xlog/000000010000000000000002 $DIR/000000010000000000000002

cp: writing `/arch/20171204/000000010000000000000002': No space left on device

LOG: archive command failed with exit code 1

DETAIL: The failed archive command was: DATE =` date +%Y%m%d`;DIR= "/arch/$DATE" ;(test -d $DIR || mkdir -p $DIR)&& cp pg_xlog/000000010000000000000002 $DIR/000000010000000000000002

WARNING: archiving transaction log file "000000010000000000000002" failed too many times, will try again later

原因是归档日志所在文件系统/arch空间不足。

通过清理该文件系统下过期的临时文件,或将归档日志存放至更大的系统空间中。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/weixin_39540651/article/details/104014087

查看更多关于PostgreSQL归档配置及自动清理归档日志的操作的详细内容...

  阅读:77次