PostgreSQL運維—歸檔配置
背景
在PostgreSQL中,WAL日志記錄了數(shù)據庫重要數(shù)據文件的所有變化,你對數(shù)據的操作都會被記錄到其中,WAL日志是保證數(shù)據完整性的一種標準方法,WAL的中心概念是數(shù)據文件(存儲著表和索引)的修改必須在這些動作被日志記錄之后才被寫入,即在描述這些改變的日志記錄被刷到持久存儲以后。
由于PostgreSQL的正常運行,有著一些參數(shù),控制這WAL日志的數(shù)量及大小,使其覆蓋掉老的歷史的WAL日志。
所以我們就可以利用PostgreSQL中的歸檔來對WAL日志進行歸檔備份,以應付數(shù)據丟失情況的不時之需。
相關參數(shù)
#wal_level = replica # minimal, replica, or logical
# (change requires restart)
#archive_mode = off # enables archiving; off, on, or always
# (change requires restart)
#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'
#archive_cleanup_command = '' # command to execute at every restartpoint
wal_level
wal_level 參數(shù)默認值為 replica ,每個層次包括所有更低層次記錄的信息。(級別:minimal<replica<logical)
- minimal —— 不能通過基礎備份和wal日志恢復數(shù)據庫。
- replica —— 該級別支持wal歸檔和復制。
- logical —— 在replica級別的基礎上添加了支持邏輯解碼所需的信息。
archive_mode
- off —— 默認值,不開啟歸檔WAL
- on —— 啟用歸檔WAL,通過設置 archive_command 命令將完成的WAL 段發(fā)送到歸檔目錄。
- always —— 從歸檔中恢復的或者用流復制傳來的文件將被(再次)歸檔
archive_command
調用服務器上本地shell或工具命令,用于歸檔一個完成的WAL文件。該參數(shù)僅在啟用了archive_mode時生效,否則它將會被忽略。
當 archive_mode 被啟用時,archive_command為設置值時,WAL 歸檔的操作會被臨時禁用。
%p:指定的參數(shù)字符串值中的任何%p被替換成要被歸檔的文件的路徑名%f:指定的參數(shù)字符串值中的任何%f只被文件名替換(路徑名是相對于數(shù)據目錄)
archive_cleanup_command
指定一個shell命令,用于清理不需要的已歸檔WAL文件。通常使用pg_archivecleanup模塊清理過時的已歸檔文件。
archive_cleanup_command = 'pg_archivecleanup /mnt/archivedir %r'
配置示例
對postgresql.conf文件編輯,針對以上參數(shù)進行如下設置:
wal_level = replica --啟用replica級別 archive_mode = on --啟用歸檔 archive_command = 'DIR=/pgsql/data/archive/`date +%F`; test ! -d $DIR && mkdir $DIR; test ! -f $DIR/%f && cp %p $DIR/%f' --對歸檔進行按天分類
對于archive_cleanup_command,根據實戰(zhàn)經驗來看,不建議依賴特性類的清理歸檔,個人喜歡腳本話,可控。
效果展示
[postgres@lyp ~]$ cd $PGDATA [postgres@lyp data]$ ls -lrt pg_wal total 81920 -rw-------. 1 postgres postgres 16777216 Jan 9 17:34 000000010000000300000029 -rw-------. 1 postgres postgres 16777216 Jan 9 17:35 00000001000000030000002A -rw-------. 1 postgres postgres 16777216 Jan 9 18:04 00000001000000030000002B -rw-------. 1 postgres postgres 16777216 Jan 9 18:05 00000001000000030000002C drwxrwxr-x. 2 postgres postgres 6 Jan 9 18:09 archive_status -rw-------. 1 postgres postgres 16777216 Jan 9 18:09 000000010000000300000028 [postgres@lyp data]$ ll archive/ total 4 drwx------. 2 postgres postgres 4096 Jan 9 18:05 2022-01-09 [postgres@lyp data]$ ll archive/2022-01-09/ total 212992 -rw-------. 1 postgres postgres 16777216 Jan 9 17:06 00000001000000030000001B -rw-------. 1 postgres postgres 16777216 Jan 9 17:10 00000001000000030000001C -rw-------. 1 postgres postgres 16777216 Jan 9 17:12 00000001000000030000001D -rw-------. 1 postgres postgres 16777216 Jan 9 17:24 00000001000000030000001E -rw-------. 1 postgres postgres 16777216 Jan 9 17:25 00000001000000030000001F -rw-------. 1 postgres postgres 16777216 Jan 9 17:29 000000010000000300000020 -rw-------. 1 postgres postgres 16777216 Jan 9 17:29 000000010000000300000021 -rw-------. 1 postgres postgres 16777216 Jan 9 17:30 000000010000000300000022 -rw-------. 1 postgres postgres 16777216 Jan 9 17:31 000000010000000300000023 -rw-------. 1 postgres postgres 16777216 Jan 9 17:34 000000010000000300000024 -rw-------. 1 postgres postgres 16777216 Jan 9 17:35 000000010000000300000025 -rw-------. 1 postgres postgres 16777216 Jan 9 18:04 000000010000000300000026 -rw-------. 1 postgres p
「喜歡這篇文章,您的關注和贊賞是給作者最好的鼓勵」
關注作者
【版權聲明】本文為墨天輪用戶原創(chuàng)內容,轉載時必須標注文章的來源(墨天輪),文章鏈接,文章作者等基本信息,否則作者和墨天輪有權追究責任。如果您發(fā)現(xiàn)墨天輪中有涉嫌抄襲或者侵權的內容,歡迎發(fā)送郵件至:contact@modb.pro進行舉報,并提供相關證據,一經查實,墨天輪將立刻刪除相關內容。




