1 . 事先檢查命令
# 查看數據目錄(找出conf文件位置) postgres=# SHOW data_directory; # 查看歸檔模式情況 postgres=# show archive_mode; archive_mode -------------- off
2 . 啟用歸檔模式
首先建立歸檔目錄
#建立歸檔目錄 mkdir -p /u02/pgsql95log/arch/ # 更改所有者 , 權限給postgres用戶 chown -R postgres.postgres /u02/pgsql95log/arch/
打開並修改postgresql.conf , 並修改三個參數
# 打開歸檔模式 archive_mode = on # 配置歸檔命令 archive_command = 'DATE=`date +%Y%m%d`;DIR="/u02/pgsql95log/arch/$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 /home/postgres/arch/$1 && cp --preserve=timestamps $2 /home/postgres/arch/$1 ; find /home/postgres/arch/ -type f -mtime +7 -exec rm -f {} \; # 以下為archive_command archive_command = '/home/postgres/bin/arch.sh %f %p'
5. 重啟PostgreSQL服務
4 . 驗證歸檔模式啟用情況
查看歸檔情況 >>>
# 查看歸檔模式 postgres=# show archive_mode; # 檢查點 , 刷新臟數據 postgres=# checkpoint # 查看歸檔情況 postgres=# select pg_switch_wal();
查看歸檔日志存放目錄 >>>
[postgres@localhost arch]$ ls -l /u02/pgsql95log/arch total 0 drwx------ 2 postgres postgres 37 Sep 17 15:28 20210917 [postgres@localhost arch]$ ls -l /u02/pgsql95log/arch/20210917/ total 16384 -rw------- 1 postgres postgres 16777216 Sep 17 15:28 000000010000004500000019
