暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL启用归档模式

原创 sg1234 2023-05-18
846



1 . 事先检查命令

# 查看数据目录(找出conf文件位置)
postgres=# SHOW data_directory;

# 查看归档模式情况
postgres=# show archive_mode;
archive_mode
--------------
off


2 . 启用归档模式

首先建立归档目录

#建立归档目录
mkdir -p /soft/postgresql-12.8/archive/

# 更改所有者 , 权限给postgres用户
chown -R postgres.postgres /soft/postgresql-12.8/archive/

  

打开并修改postgresql.conf , 并修改三个参数

# 打开归档模式
archive_mode = on

# 配置归档命令
archive_command = 'DATE=date +%Y%m%d;DIR="/soft/postgresql-12.8/archive/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f'

# 日志等级 10版本以上为reploca(默认值) 可以不用修改
wal_level = archive


简单说明一下archive_command : 

它的值可以是一条shell命令或者一个复杂的shell脚本。

%p : 表示将要归档的wal文件包含完整路径的信息的文件名(就是需要归档的临时文件)

%f : 代表不包含路径信息的wal文件的文件名

%% : 表示%


比如 :

# 创建一个shell
vi /home/postgres/bin/arch.sh

# 以下为shell的内容
# 测试目录 , 复制日志文件 , 并删除7天前的日志文件
test ! -f /soft/postgresql-12.8/archive/$1 && cp --preserve=timestamps $2 /soft/postgresql-12.8/archive/$1 ; find /soft/postgresql-12.8/archive/ -type f -mtime +7 -exec rm -f {} \;

# 以下为archive_command
archive_command = '/home/postgres/bin/arch.sh %f %p'


3.重启PostgreSQL服务

su - postgres
pg_ctl -D /soft/postgresql-12.8/data -l /soft/postgresql-12.8/data/serverlog restart


4.验证归档模式启用情况

4.1 查看归档情况 >>>
# 查看归档模式
postgres=# show archive_mode;

# 检查点 , 刷新脏数据
postgres=# checkpoint

# 查看归档情况
postgres=# select pg_switch_wal();



4.2 查看归档日志存放目录 >>>
[postgres@localhost arch]$ ls -l /soft/postgresql-12.8/archive
total 0
drwx------ 2 postgres postgres 37 Sep 17 15:28 20221126
[postgres@localhost arch]$ ls -l /soft/postgresql-12.8/archive/20221126/
total 16384
-rw------- 1 postgres postgres 16777216 Nov 26 09:30 000000010000001300000055
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论