數據庫的歸檔, 是爲了更好的保證數據庫的數據安全不可或缺的功能, 無論是什麼數據庫都應開啓歸檔模式!
檢查當前數據庫情況
檢查數據庫是否爲歸檔模式
postgres=# \c
You are now connected to database "postgres" as user "postgres".
postgres=# show archive_mode;
archive_mode
--------------
off
(1 row)
歸檔模式未啓動.
開啓數據庫歸檔
修改數據庫參數
PostgreSQL數據庫參數文件:
[postgres@pg01 ~]$ cd /var/lib/pgsql/12/data
[postgres@pg01 data]$ ll postgresql.conf
-rw-------. 1 postgres postgres 26612 Mar 1 16:03 postgresql.conf
vi postgres.conf修改如下:
wal_level = replica # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command = 'test ! -f /var/lib/pgsql/12/arch/%f && cp %p /var/lib/pgsql/12/arch/%f'
## 其中: %p表示wal日誌文件的路徑,%f表示wal日誌文件名稱。
相關參數解釋
wal_level參數控制WAL日誌信息的輸出級別
- minimal 記錄的WAL日誌信息最少, 除了記錄數據庫異常關閉需要恢復時的WAL外, 其他操作信息都不記錄;
- replica 記錄的WAL信息比minimal信息多, 會記錄支持WAL歸檔、複製和備庫中啓用只讀查詢等操作所需的WAL信息;
- logical 記錄的最多, 包含了支持邏輯解析所需的WAL。 幷包含了minimal和replica記錄的信息。
archive_command
可以將WAL歸檔到本級目錄, 也可以歸檔到遠程其他主機上。
max_wal_senders
控制主庫上的最大WAL發送進程數, 通過pg_basebackup命令在主庫上做基準備份時也會消耗WAL進程。此參數不能大於max_connections, 默認值10.
創建歸檔目錄
[postgres@pg01 12]$ mkdir /var/lib/pgsql/12/arch
重啓PG數據庫
使用root用戶重啓PG
[root@pg01 ~]# service postgresql-12 restart
Stopping postgresql-12 service: [ OK ]
Starting postgresql-12 service: [ OK ]
檢查歸檔是否開啓成功
- 查看模式
[postgres@pg01 12]$ psql
psql (12.0)
Type "help" for help.
postgres=# show archive_mode;
archive_mode
--------------
on
(1 row)
- 手動切換日誌
在PG10之前:
postgres=# select pg_switch_xlog();
在PG10之後:
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/130005E0
(1 row)
- 檢查歸檔文件
[root@pg01 12]# cd /var/lib/pgsql/12/arch
[root@pg01 arch]# ll
total 4
drwx------. 2 postgres postgres 4096 Mar 3 02:33 20200303
開啓成功 ~!